Android Device onConfigurationChanged event does not handling orientation
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:-
public class MainActivity extends Activity {
boolean isLaunched=true;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(isLaunched){
Toast.makeText(this, "1 st launch " , Toast.LENGTH_SHORT).show();
setContentView(R.layout.activity_hls_land);
}
}
@Override
public void onConfigurationChanged(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 :-
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main"
android:configChanges="orientation"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Sample layout for activity_hls_port :-
<LinearLayout xmlns: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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
tools:context=".MainActivity" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
tools:context=".MainActivity" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
tools:context=".MainActivity" />
</LinearLayout>
Sample for landscape mode:-
<LinearLayout xmlns: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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
tools:context=".MainActivity" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
tools:context=".MainActivity" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
tools:context=".MainActivity" />
</LinearLayout>
Related videos on Youtube
Munazza
I am a passionate programmer. Want to learn new things and enjoy my work :)
Updated on September 16, 2022Comments
-
Munazza over 1 year
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 , but it once changes the orientation and later on does not change the orientation. here is my code please help
public class Hls extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_hls); setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); } @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); Log.v("new orientation", "yes"); if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) { Toast.makeText(this, "portrait", Toast.LENGTH_LONG).show(); setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); } if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) { Toast.makeText(this, "landscape", Toast.LENGTH_LONG).show(); setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); } }
-
Munazza over 11 yearsThis is okay if i just want to handle the activity runtime orientation changes but i want to set the configuration as landscape through code once the activity is started up and then want to handle the configuration changes on rotating the device . The problem occurs when i change the orientation by calling setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); this sets the orientation to lanscape but then i can not go back to portrait mode even when i rotate device on portrait .
-
Munazza over 11 yearsok thnx plz tell me that how i would design the layout for landscape i mean what would be difference b/w port and land layouts ..my portlay is <RelativeLayout xmlns:android="schemas.android.com/apk/res/android" xmlns:tools="schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent"> <VideoView android:id="@+id/player" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/button1" android:layout_centerHorizontal="true" /> </RelativeLayout>
-
Code Word over 11 yearsHi ! There isn't any difference as such. All you need to do is to create the layout with a landscape orientation.
-
Munazza over 11 yearshow to specify landscape orientation mode in the layout file ?
-
Code Word over 11 yearsIt is just the appearance of landscape mode. You need to arrange the views in your layout as if they are in landscape mode.
-
Code Word over 11 yearsI have added sample layouts. Try it.