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

How to remove or add layout_below in the TextView, programmatically?

Narendra Singh
  • 3,990
  • 5
  • 37
  • 78

1 Answers1

2

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

Aleksandr
  • 4,906
  • 4
  • 32
  • 47
  • 1
    I did not understand the last line `yourView.setLayoutParams(p);` what is `p` and where is the `aaa` and `xxx`? – Hasan A Yousef Feb 04 '17 at 09:13
  • 1
    @HasanAYousef, not `p`, but `lp`. Regarding `xxx` - https://developer.android.com/reference/android/widget/RelativeLayout.LayoutParams.html#attr_android:layout_below – Aleksandr Feb 05 '17 at 09:06