Change visibility of xml layout with the change of orientation of the device

Asked

Viewed 155 times

2

I need to change the visibility of an xml layout when the device orientation is from Portrait (standing) to Landscape (lying down). I intend to add 2 columns when performing this action. I used the code android:visibility="gone" to hide, but I’m not finding a way to change to android:visibility="visibility" when the Guideline is amended.

Follow 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>

2 answers

0


Add android:configChanges="orientation" in its Activity manifest thus:

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

Then override the onConfigurationChanged thus:

    @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;

    }

0

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

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

Create two folder inside the folder /res, a call layout-land and another call layout-port.
At first place the layout which is displayed when the device is in position Landscape and on Monday layout to the position portraint.

Android will automatically choose the layout to be used in each of the configurations.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.