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:
- Why is the layout not being read if I use
android:configChanges
? - 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.