Vic Vic - 2 months ago 7
Android Question

Android app crashes when changed to landscape orientation

I am developing a simple android app with a TextView and ImageView on the launching page. When I rotate my phone to landscape mode, the app crashes. Though I see that the app is not killed, it just goes away and I can see that it is still running in Android Studio.
Following is my code:
Content_main.xml -landscape mode

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.viral.myapplication2.MainActivity"
tools:showIn="@layout/activity_main">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:id="@+id/textView2" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/ganpati_bappa_moraya"
android:id="@+id/textView"
tools:context=".Blink"
android:textAlignment="center"
android:textSize="@dimen/abc_action_button_min_width_overflow_material"
android:layout_alignRight="@+id/imageView"
android:layout_alignEnd="@+id/imageView"
android:layout_alignLeft="@+id/imageView"
android:layout_alignStart="@+id/imageView" />
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/imageView"
android:layout_centerHorizontal="true"
android:src="@mipmap/god_pic"
android:layout_below="@+id/textView" />
</RelativeLayout>


Content_main.xml - for normal mode - portrait

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.viral.myapplication2.MainActivity"
tools:showIn="@layout/activity_main">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:id="@+id/textView2" />

<TextView
android:layout_width="400dp"
android:layout_height="100dp"
android:text="Ganpati Bappa Moraya !!!"
android:id="@+id/textView"
android:layout_below="@+id/textView2"
android:layout_centerHorizontal="true"
tools:context=".Blink"
android:layout_marginTop="84dp"
android:textAlignment="center"
android:textSize="@dimen/abc_action_button_min_width_overflow_material" />

<ImageView
android:layout_width="500dp"
android:layout_height="200dp"
android:id="@+id/imageView"
android:layout_below="@+id/textView"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:src="@mipmap/god_pic" />
</RelativeLayout>


Android_Manifest file-

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.viral.myapplication2">

<application
android:allowBackup="true"
android:icon="@mipmap/god_pic"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:configChanges="keyboardHidden|orientation"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>

Answer

Modify AndroidManifest.xml file to this

android:configChanges="keyboardHidden|orientation|screenSize".

screenSize property will take care about screen orientation change and activity life cycle will not call again.

Comments