How to Set "Android:Layout_Below" at Runtime Programmatically

Android How to change layout_below to TextView programmatically

I see 2 ways.

First way, you need to create new layout with same name but in folder layout-land, copy all content here and remove layout_below rule.
In this case, you just declare another layout, which not contains layout_below rule

See this screenshot:

2 layout for different orientation

This means, that you defined different layouts for different orientation.
in layout/my_layout.xml:

<TextView
android:id="@+id/aaa"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/xxx" />

in layout-land/my_layout.xml:

<TextView
android:id="@+id/aaa"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

For more information, see this link:

Table 2. Configuration qualifier names, row with name "Screen orientation"

Second way, in your java code(onCreate() method for example):

if(Activity.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE){
RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) yourView.getLayoutParams();
lp.addRule(RelativeLayout.BELOW, 0);
yourView.setLayoutParams(lp);
}

EDITED

Programmatically add view one below other in relative layout

Important: Remember to set the ID for each view.

RelativeLayout layout = new RelativeLayout(this);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
layout.setLayoutParams(layoutParams);

RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams params3 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams params4 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

TextView tv1 = new TextView(this);
tv1.setId(1);
tv1.setText("textView1");

TextView tv2 = new TextView(this);
params2.addRule(RelativeLayout.RIGHT_OF, tv1.getId());
tv2.setId(2);
tv2.setText("textView2");

TextView tv3 = new TextView(this);
params3.addRule(RelativeLayout.BELOW, tv1.getId());
tv3.setId(3);
tv3.setText("textView3");

TextView tv4 = new TextView(this);
params4.addRule(RelativeLayout.RIGHT_OF, tv3.getId());
params4.addRule(RelativeLayout.ALIGN_BOTTOM, tv3.getId());
tv4.setId(4);
tv4.setText("textView4");

layout.addView(tv1, params1);
layout.addView(tv2, params2);
layout.addView(tv3, params3);
layout.addView(tv4, params4);

How to edit TextView parameters programmatically?

you are overriding the relativeParams with the linearParams. set margins to relativeParms variable iteself and then setLayoutParams to newActivity like below:

    relativeParams.addRule(RelativeLayout.BELOW, timeSlots[startTimeHour + 1]);
relativeParams.setMargins(0, startTimeMin-3, 0, 0);
newActivity.setLayoutParams(relativeParams);
// Add to the screen
RelativeLayout schedule = (RelativeLayout) findViewById(R.id.scheduleView);
schedule.addView(newActivity);

Removing RelativeLayout.END_OF which is added programmatically

I finally found the way to fix that. I should create a new layout parameters for each view using new RelativeLayout.LayoutParams().

Trying to set LayoutWidth Programmatically

I resolved the problem.I need to put ScrollView Instead of Relative layout.

Because the relative layout i want to set; is the child of Scroll View in my XML.

ScrollView.LayoutParams params = new ScrollView.LayoutParams(width,LayoutParams.WRAP_CONTENT);
rl.setLayoutParams(params);


Related Topics



Leave a reply



Submit