Mazin Al-Bahrani
Mazin Al-Bahrani

Reputation: 75

Landscape Orientation - How to preserve data and layout attributes?

Hello Stack Overflow community,

I have been trying to allow my device to keep the activity from wiping data every-time I change orientation from portrait to landscape.

I found that I can do so by adding android:configChanges="orientation" in the activity field in my AndroidManifest.xml as can be seen below:

<activity android:name=".MainActivity"  android:configChanges="orientation|screenLayout|screenSize" >
...
</activity>

However, by doing so, it does not consider the layout changes which I made in my /res/layout-land/main.activity.xml

If I delete the android:configChanges modifications made, it applies all changes made in res/layout-land/activity_main.xml

I would like clarification on:

  1. Why is the layout not being read if I use android:configChanges?
  2. Is there a way I can make the orientation not delete the data created while maintaining the layout attributes?

My full XML code for the landscape view is as below:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:layout_marginBottom="80dp">

                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:orientation="vertical">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_horizontal"
                        android:text="@string/text_team_a"
                        android:textSize="28sp" />

                    <TextView
                        android:id="@+id/team_a"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_horizontal"
                        android:text="@string/text_0"
                        android:textSize="46sp" />

                    <Button
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="32dp"
                        android:layout_marginRight="32dp"
                        android:onClick="addOneTeamA"
                        android:text="@string/bt_score_plus_1" />


                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_horizontal"
                        android:text="@string/text_fouls"
                        android:textSize="28sp" />

                    <TextView
                        android:id="@+id/team_a_foul"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_horizontal"
                        android:text="@string/text_0"
                        android:textSize="46sp" />

                    <Button
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="32dp"
                        android:layout_marginRight="32dp"
                        android:onClick="addOneFoulTeamA"
                        android:text="@string/bt_foul_plus_1" />


                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_horizontal"
                        android:text="@string/text_penalty"
                        android:textSize="28sp" />

                    <TextView
                        android:id="@+id/team_a_penalty"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_horizontal"
                        android:text="@string/text_0"
                        android:textSize="46sp" />

                    <Button
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="32dp"
                        android:layout_marginRight="32dp"
                        android:onClick="addOnePenaltyTeamA"
                        android:text="@string/bt_penalty_plus_1" />


                </LinearLayout>

                <View
                    android:layout_width="1dp"
                    android:layout_height="match_parent"
                    android:layout_marginTop="8dp"
                    android:background="@android:color/darker_gray" />

                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:orientation="vertical">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_horizontal"
                        android:text="@string/text_team_b"
                        android:textSize="28sp" />

                    <TextView
                        android:id="@+id/team_b"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_horizontal"
                        android:text="@string/text_0"
                        android:textSize="46sp" />

                    <Button
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="32dp"
                        android:layout_marginRight="32dp"
                        android:onClick="addOneTeamB"
                        android:text="@string/bt_score_plus_1" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_horizontal"
                        android:text="@string/text_fouls"
                        android:textSize="28sp" />

                    <TextView
                        android:id="@+id/team_b_foul"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_horizontal"
                        android:text="@string/text_0"
                        android:textSize="46sp" />

                    <Button
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="32dp"
                        android:layout_marginRight="32dp"
                        android:onClick="addOneFoulTeamB"
                        android:text="@string/bt_foul_plus_1" />


                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_horizontal"
                        android:text="@string/text_penalty"
                        android:textSize="28sp" />

                    <TextView
                        android:id="@+id/team_b_penalty"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_horizontal"
                        android:text="@string/text_0"
                        android:textSize="46sp" />

                    <Button
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="32dp"
                        android:layout_marginRight="32dp"
                        android:onClick="addOnePenaltyTeamB"
                        android:text="@string/bt_penalty_plus_1" />

                </LinearLayout>


            </LinearLayout>


            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_marginLeft="16dp"
                android:layout_marginRight="16dp"
                android:layout_marginBottom="8dp"
                android:onClick="resetDetails"
                android:text="@string/bt_reset" />


        </RelativeLayout>

    </LinearLayout>

</ScrollView>

EDIT

If possible, would like to have a XML solution without the need to tinker on the Java code.

Upvotes: 1

Views: 634

Answers (2)

Mazin Al-Bahrani
Mazin Al-Bahrani

Reputation: 75

After further testing, it seems the issue is not in the android:configChanges but the location of the opening tag of RelativeLayout which caused all this issue. I just had to change it to the below and it works:

My XML layout was fixed by doing the following:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    tools:context=".MainActivity">

    <LinearLayout ...>

            <LinearLayout...>

                <LinearLayout
                    ...
                </LinearLayout>

                <View ... />

                <LinearLayout
                  ...
                </LinearLayout>


            </LinearLayout>


        <RelativeLayout...>

            <Button .../>

        </RelativeLayout>

    </LinearLayout>

</ScrollView>

Below is the detailed solution with all code changes done, including my XML file:

Update the code in AndroidManifest.xml file to include the additional code of android:configChanges as shown below in the activity tag:

<activity android:name=".MainActivity"  android:configChanges="orientation|screenLayout|screenSize" >
...
</activity>

Then I had to go to my activity_main.xml and change the location of the RelativeLayout opening tag as it has been the cause of having some buttons to be hidden when orientation from portrait to landscape was invoked.

The old code format:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    tools:context=".MainActivity">

    <LinearLayout ...>

        <RelativeLayout...> <-- remove it from here

            <LinearLayout...>

                <LinearLayout
                    ...
                </LinearLayout>

                <View ... />

                <LinearLayout
                  ...
                </LinearLayout>


            </LinearLayout>


            <Button .../>

        </RelativeLayout>

    </LinearLayout>

</ScrollView>

My XML layout was fixed by doing the following:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    tools:context=".MainActivity">

    <LinearLayout ...>

            <LinearLayout...>

                <LinearLayout
                    ...
                </LinearLayout>

                <View ... />

                <LinearLayout
                  ...
                </LinearLayout>


            </LinearLayout>


        <RelativeLayout...> <-- add it here

            <Button .../>

        </RelativeLayout>

    </LinearLayout>

</ScrollView>

The full working XML code is below:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:orientation="vertical">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:text="@string/text_team_a"
                    android:textSize="28sp" />

                <TextView
                    android:id="@+id/team_a"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:text="@string/text_0"
                    android:textSize="46sp" />

                <Button
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="16dp"
                    android:layout_marginRight="16dp"
                    android:onClick="addOneTeamA"
                    android:text="@string/bt_score_plus_1" />


                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:text="@string/text_fouls"
                    android:textSize="28sp" />

                <TextView
                    android:id="@+id/team_a_foul"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:text="@string/text_0"
                    android:textSize="46sp" />

                <Button
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="16dp"
                    android:layout_marginRight="16dp"
                    android:onClick="addOneFoulTeamA"
                    android:text="@string/bt_foul_plus_1" />


                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:text="@string/text_penalty"
                    android:textSize="28sp" />

                <TextView
                    android:id="@+id/team_a_penalty"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:text="@string/text_0"
                    android:textSize="46sp" />

                <Button
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="16dp"
                    android:layout_marginRight="16dp"
                    android:onClick="addOnePenaltyTeamA"
                    android:text="@string/bt_penalty_plus_1" />


            </LinearLayout>

            <View
                android:layout_width="1dp"
                android:layout_height="match_parent"
                android:layout_marginTop="8dp"
                android:background="@android:color/darker_gray" />

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:orientation="vertical">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:text="@string/text_team_b"
                    android:textSize="28sp" />

                <TextView
                    android:id="@+id/team_b"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:text="@string/text_0"
                    android:textSize="46sp" />

                <Button
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="16dp"
                    android:layout_marginRight="16dp"
                    android:onClick="addOneTeamB"
                    android:text="@string/bt_score_plus_1" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:text="@string/text_fouls"
                    android:textSize="28sp" />

                <TextView
                    android:id="@+id/team_b_foul"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:text="@string/text_0"
                    android:textSize="46sp" />

                <Button
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="16dp"
                    android:layout_marginRight="16dp"
                    android:onClick="addOneFoulTeamB"
                    android:text="@string/bt_foul_plus_1" />


                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:text="@string/text_penalty"
                    android:textSize="28sp" />

                <TextView
                    android:id="@+id/team_b_penalty"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:text="@string/text_0"
                    android:textSize="46sp" />

                <Button
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="16dp"
                    android:layout_marginRight="16dp"
                    android:onClick="addOnePenaltyTeamB"
                    android:text="@string/bt_penalty_plus_1" />

            </LinearLayout>


        </LinearLayout>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_marginLeft="16dp"
                android:layout_marginRight="16dp"
                android:layout_marginBottom="8dp"
                android:onClick="resetDetails"
                android:text="@string/bt_reset" />


        </RelativeLayout>

    </LinearLayout>

</ScrollView>

Upvotes: 0

Tejas Dhawale
Tejas Dhawale

Reputation: 433

you can use savedInstanceState or android have introduced ViewModel to manage UI-related data in a lifecycle conscious way.

for more info Android ViewModel

Can also refer to this answer

Upvotes: 1

Related Questions