I am working on a project. where i used screen orientation landscape and portrait. I also use android:configChanges="keyboardHidden|orientation|screenSize" so that activity not refresh when switch orientation .
Now app is unable to switch from layout-port->layout-land xml layout as i use configChanges = "orientation" .
I am able switch from Landscape-> portrait or portrait->Landscape but it show the layout-port xml from both orientation rather than switch from layout-port->layout-land or
layout-land->layout-port.
In AndroidManifest file add only android:configChanges="orientation" and remove the other two for the activity you want to handle this orientation
add this in ur activity
inside of oncreate()
if(getResources().getConfiguration().orientation ==Configuration.ORIENTATION_LANDSCAPE)
setContentView(R.layout.land);
else if(getResources().getConfiguration().orientation ==Configuration.ORIENTATION_PORTRAIT)
setContentView(R.layout.port);
#Override
public void onConfigurationChanged(Configuration config) {
super.onConfigurationChanged(config);
if (config.orientation == Configuration.ORIENTATION_LANDSCAPE) {
setContentView(R.layout.land);
} else if (config.orientation == Configuration.ORIENTATION_PORTRAIT){
setContentView(R.layout.port);
}
}
try this
change android:configChanges="keyboardHidden|orientation|screenSize"
to android:configChanges="orientation"
and on your activity, override onConfigurationChanged methode
like Manivannan describes in his response
#Override
public void onConfigurationChanged(Configuration config) {
super.onConfigurationChanged(config);
if (config.orientation == Configuration.ORIENTATION_LANDSCAPE) {
//set your landscape layout
} else if (config.orientation == Configuration.ORIENTATION_PORTRAIT){
//the same for portrait layout
}
}
that's it.
Related
Reading this so question one can see that toolbar uses different heights on landscape vs portrait mode.
But when android:configChanges="orientation|screenSize" is specified, toolbar height remains static according to the orientation in which the activity was first created and won't change when rotating the device.
How can I work this around? I'm thinking that either I should resize the toolbar inside onConfigurationChanged() method or I should destroy and recreate the toolbar and let it get its default height from scratch. I actually like this 2nd way better but I don't know how to do any of these so any suggestions are welcome.
to get rotation event:
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
}
}
to resize layout:
First you have to declare the layout u wanna resize:
RelativeLayout rl = (RelativeLayout) findViewById(R.id.yourId);
And resize:
rl.getLayoutParams().height = 100; // replace 100 with your dimensions
rl.getLayoutParams().width = 100;
I have a activity in which I load a fragment. Lets say I have two sections inside fragment layout(section A and B) , but while I switch to landscape then I have to show only section A . How will i know the orientation change inside fragment.
you can override onConfigurationChanged to be notified when the orientation changed add code below in your fragment class:
#Override public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if(newConfig.orientation == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE){
//landscape
}else{
//portrait
}
}
When the orientation of the screen is changed the Fragment will be destroyed and created again, so onDestroy() and onCreate() are called. in your onCreate() or onCreateView() (or wherever you need to) you can check for the current screen rotation with the following code:
int screenRotation = ((WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getRotation();
if(screenRotation == Surface.ROTATION_90 || screenRotation == Surface.ROTATION_270) {
//LANDSCAPE
} else {
//PORTRAIT
}
For this functionality android provides Sliding pane layout SlidingPaneLayout
and watch this tutorials also official google developers tutorial https://www.youtube.com/watch?v=Jl3-lzlzOJI&t=1007s
I have a videoview which play live streaming links fine in potrait mode with some channels listing below the videoview.
My problem is that i want to rotate my videoview to landscape mode (fullscreen and without showing the channel lists in the bottom) without buffering again or loading the stream again.
Thanks.
Add android:configChanges="orientation" to Activity tag on AndroidManifest.xml
it will stop the calling of onCreate() while rotation and prevent your video from rebuffering.
and use the following code in your activity.
//this is called when the screen rotates.
// (onCreate is no longer called when screen rotates due to manifest, see: android:configChanges)
#Override
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
int orientation = newConfig.orientation;
if (orientation == Configuration.ORIENTATION_PORTRAIT)
{
Log.d("tag", "Portrait");//Do your layout changes here
}
else if (orientation == Configuration.ORIENTATION_LANDSCAPE)
{
Log.d("tag", "Landscape");//Do your layout changes here
}
}
Mark as up if it works for you.
I am developing an app in which I need to provide different activity's background image on different orientation. So I've approached in this way:
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
setLanguage();
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
// set background for landscape
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
// set background for portrait
}
}
And
android:configChanges="locale|orientation|screenSize"
and in onCreate() I set the background image for portrait assuming user will launch the app staying in portrait mode.
Everything works fine as when users change their mode, the corresponding background is set and so on.
But if a user starts this app when the phone is in landscape mode, as It is shown the portrait image at the first launch as I assumed before user will launch app in portrait mode.
So how can I solve this problem? In one sentence, what is the best way to set different background image for different orientation ? am I in a right track?
In onCreate of your activity you can check for the current orientation using the this
Configuration newConfig = getResources().getConfiguration();
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
// set background for landscape
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
// set background for portrait
}
In one sentence, what is the best way to set different background image for different orientation ?
Step #1: Delete everything you've done, most notably the whole android:configChanges stuff. Ignore the background image for now, and get the rest of your configuration change logic working.
Step #2: Create -land versions of the requisite resource directories, for whatever densities of this image that you have (e.g., res/drawable-land-hdpi/ to match your res/drawable-hdpi/)
Step #3: Move the landscape versions into the -land directories, naming them the same as their portrait equivalents (e.g., res/drawable-hdpi/background.png and res/drawable-land-hdpi/background.png)
Step #4: Just refer to common resource name in your android:background attribute (e.g., #drawable/background)
This way:
You stick to better configuration-change behavior, and
Android will give you the correct background at the correct time
You can check for orientation in onCreate
WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
int orientation = display.getRotation();
if (orientation == Surface.ROTATION_0 || orientation == Surface.ROTATION_180)
{
// Portrait
}
else
{
// landscape
}
check orientation changes after setContentView method
int orientation = getResources().getConfiguration().orientation;
if (orientation == 1){
utility.toast("portrait");
}else {
utility.toast("landscape");
}
I tried to set two splash screen for landscape and Portrait. For that, I wrote below code in my activity class
if (getResources().getConfiguration().orientation == 2) {
super.setIntegerProperty("splashscreen", R.drawable.equipsplashscreen);
Log.d("Orientation", "Landscape");
}
else {
super.setIntegerProperty("splashscreen", R.drawable.splashscreen);
Log.d("Orientation", "Portrait");
}
screens are displayed according to the orientation, but the problem is.. screens are not changed while loading(i.e I launched my app in portrait mode, while loading the splash screen I changed the orientation from portrait to landscape, at that time my landscape image was not loading.. only portrait image changed as landscape style ).
I am using cordova-1.8.0 and jquery-1.7.1
How can I do this.. Thanks in Advance..
In Android we have to override the onConfigurationChanged method like this
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
setContentView(R.layout.splashscreen);
init();
} else if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
setContentView(R.layout.splashscreen);
init();
}
}
And adding one tag in Manifest like this
<activity
android:name=".YourActivity"
android:configChanges="orientation|keyboardHidden"
android:windowSoftInputMode="stateAlwaysHidden|adjustResize|adjustPan" />
Now check the requirements in phonegap because i don't know phone gap I give you the tip what had done in android.