Change visibility of xml layout with change of device orientation

2

I need to change the visibility of a layout in xml when the orientation of the device is from portrait to landscape. I want to add 2 columns when performing this action. I used the code android:visibility="gone" to hide, but I can not find a way to change to android:visibility="visibility" when the orientation is changed.

Follow the full code below.

<LinearLayout
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:layout_weight="1"
  android:orientation="vertical"
  android:visibility="gone">

  <Button
      android:id="@+id/btnPer"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:layout_weight="1"
      android:background="@drawable/button_green"
      android:text="@string/Btn_Per"
      android:textColor="@color/gray"
      android:textSize="18sp" />

  <Button
      android:id="@+id/btnQuad"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:layout_weight="1"
      android:background="@drawable/button_green"
      android:text="@string/Btn_Quad"
      android:textColor="@color/gray"
      android:textSize="18sp" />

  <Button
      android:id="@+id/btnRaiz"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:layout_weight="1"
      android:background="@drawable/button_green"
      android:text="@string/Btn_Raiz"
      android:textColor="@color/gray"
      android:textSize="18sp" />

  <Button
      android:id="@+id/btnUm"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:layout_weight="1"
      android:background="@drawable/button_green"
      android:text="@string/Btn_Um"
      android:textColor="@color/gray"
      android:textSize="18sp" />

  <Button
      android:id="@+id/btnMod"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:layout_weight="1"
      android:background="@drawable/button_green"
      android:text="@string/Btn_Mod"
      android:textColor="@color/gray"
      android:textSize="18sp" />

</LinearLayout>
    
asked by anonymous 25.11.2017 / 16:32

2 answers

0

Add android:configChanges="orientation" to your activity manifest like this:

<activity android:name=".HelloAndroid"
android:label="@string/app_name"
android:configChanges="orientation">

Then override onConfigurationChanged like this:

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);,
        int orientation=newConfig.orientation;

        switch(orientation) {

        case Configuration.ORIENTATION_LANDSCAPE:
         //faça alguma coisa quando mudar pra landscape

        break;

       case Configuration.ORIENTATION_PORTRAIT:
             //faça alguma coisa quando mudar pra potrait

       break;

    }
    
25.11.2017 / 16:39
0

The recommended approach to dealing with this need is to build a different layout for each device orientation.

Android allows you to use configuration qualifiers to name resource folders so you can select the features to use according to the device configuration.

Create two folders within the /res folder, one call layout-land , and another call layout-port . At the first place the layout wherever it appears when the device is in the landscape position and the second layout portraint .

Android will automatically choose the layout to use in each of the settings.

    
25.11.2017 / 16:49