Skip to content Skip to sidebar Skip to footer

Android Device Onconfigurationchanged Event Does Not Handling Orientation

i have an activity and on startup of the activity i need to change the orientation in lanscape and then later on i want to handle both orientation changes as user rotates device ,

Solution 1:

Using setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) in onCreate will fix your orientation permanently to landscape and will avoid any change in orientation. That is why your orientation gets fixed and doesn't respond to the rotations.

Here is an alternative you can use :-

1> Create two views in your layout. i.e one for landscape and one for portrait views.Lets say activity_hls_land and activity_hls_port.

2> Use setContentView(R.layout.activity_hls) in onConfigurationChanged(Configuration newConfig) instead of setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) or setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT)

Here is the sample code:-

publicclassMainActivityextendsActivity {
boolean isLaunched=true;
    @OverridepublicvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        if(isLaunched){
        Toast.makeText(this, "1 st launch " , Toast.LENGTH_SHORT).show(); 
        setContentView(R.layout.activity_hls_land);
        }

    }

    @OverridepublicvoidonConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);

    if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT)
    {
        Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
        setContentView(R.layout.activity_hls_port);
    }
     if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE)
    {
        Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
        setContentView(R.layout.activity_hls_land );
    } 
    }

}

and in the manifest add android:configChanges="orientation" in activity :-

<activityandroid:name=".MainActivity"android:label="@string/title_activity_main"android:configChanges="orientation"
            ><intent-filter><actionandroid:name="android.intent.action.MAIN" /><categoryandroid:name="android.intent.category.LAUNCHER" /></intent-filter></activity>

Sample layout for activity_hls_port :-

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/hello_world"tools:context=".MainActivity" /><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/hello_world"tools:context=".MainActivity" /><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/hello_world"tools:context=".MainActivity" /></LinearLayout>

Sample for landscape mode:-

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="vertical"android:rotation="90"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/hello_world"tools:context=".MainActivity" /><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/hello_world"tools:context=".MainActivity" /><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/hello_world"tools:context=".MainActivity" /></LinearLayout>

Solution 2:

I'm not clear about your requirement. If you don't mind restarting your activity, you can just skip overriding onConfigurationChanged. System will handle Orientation changes for you. In case you don't want it to be restarted on orientation changes, just mention <activity android:configChanges="keyboardHidden|orientation|screenSize"/>, and override onConfigurationChanged and call setContentView()

public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    setContentview(R.layout.activity_hls);
    initializeViews();//here you can initialize all your memberVariables using findViewbyID()
}

Post a Comment for "Android Device Onconfigurationchanged Event Does Not Handling Orientation"