Android loadout/layout-land onConfigurationChanged - android

I have a DialogFragment in my Android app, and I need to set android:configChanges="orientation|screenSize, I want to load different layout for my DialogFragment in portrait or landscape, but now Android will not automatically do this for me, I think I need to update the layout in onConfigurationChanged , but I don't know how to do it, any sample code will be better, thank you!

Another most common solution to dealing with orientation changes by setting the android:configChanges flag on your Activity in AndroidManifest.xml. Using this attribute your Activities won’t be recreated and all your views and data will still be there after orientation change.
try this code and changed your configuration :
<activity
android:name="com.example.test.activity.MainActivity"
android:configChanges="orientation|screenSize|keyboardHidden"/>
This attribute informs to Android system that you are going to handle orientation and screenSize changes for this Activity. So instead of destroying and recreating your Activity, Android will just rotate the screen and invoke one of the lifecycle callback method which is onConfigurationChanged(Configuration).
try this

I finnally find my solution, but I think it maybe only work for my situation, my situation is I want to show a imageView in portrait and not show in landscape, so I set the visibility as visiable or gone in function onConfigurationChanged' and it works

Related

My app is not working properly when screen is rotated

I am creating an app on android studio. The app is working fine but whenever I rotate the screen, the layout is changing, the items on the screen are getting overlapped and sometimes the app restarts.
What are the possible solutions? Is preventing the screen from rotation a good option? Please help. Thanks in advance.
You need to set orientation in AndroidMenifest.xml.Use keyboardHidden attribute if you are getting any issue in keyboard.
You can set you activity's screen orientation to portrait, by this your activity will never rotate, but if you want to support landscape version then you have to create a landscape layout as well and put the layout in layout-landscape folder, but you have to take care of your data also.
Choice is yours
You can prevent your activity to recreate when orientation changes. Just add this in your activity tag in Manifest file.
<activity
android:name=".YOUR_ACTIVITY_NAME"
android:configChanges="keyboardHidden|orientation|screenSize" />
Android Activites start from the beginning when you rotate a device. So your Activity Lifecycle methods like
onCreate(), onStart(), onResume()
will be called again.
There are two possible solutions, the first one is by Rob here.
<activity
android:name=".ACTIVITY"
android:configChanges="keyboardHidden|orientation|screenSize" />
This will handle the orientation changes itself.
Another solution is to create different layout types in different layout folders. The folders will be
layout-land, layout-sw600dp-land(for 7inch tablets) and layout-sw720dp-land(for 10inch tablets)
So when you rotate a device, the layouts in this folders will be automatically inflated by the system. There will be cases like when you want to preserve the current state of the activity, for this refer to this link
No, preventing the orientation to be landscape is not a good solution. Many tablets are landscape-only, and your app won't display correctly on those.
The correct thing to do is to save your state in onSaveInstanceState() and then read it in onCreate() when the activity is re-created (the savedInstanceState variable will not be null). For this to work, the data that you are saving must be Serializable or Parcelable.
If you want to save non-serializable data or you have something running in the background, you can use a Fragment with setRetainInstance(true); and no layout. See this link.
The view will be created again, yes, but you will set the saved values so that it will look as it was before the rotation.

When would one justify overriding orientation handling

When would one justify adding this to manifest.xml?
android:configChanges="orientation|keyboardHidden|screenSize"
Thankyou
Yes its required based on your requirement. I had an app in which we had to show videos on orientation change the video would restart hence i had to overide the onConfigureation() method and handle it, As i said it depends on your requirement
Adding this to the manifest means that you will control what happens when orientation changes, keyboard is minimized, screen size changes. If you do not add this then the onCreate gets called again on these events.
But say you have a different layout for landscape and portrait. Then you would like to call onCreate again to render the landscape layout.

Android onConfigurationChanged() only in certain cases

I have stumbled on a problem, that, when the screen turns off while being in LANDSCAPE orientation, certain devices "rotate" the app back to PORTRAIT position (because the lockscreen is PORTRAIT only or something like that). I did a little research before posting this, and most popular work-around is to modify app's configuration change process to prevent activity being recreated after the configuration has changed.
But disabling activity recreation is not a solution for me, because my app supports both orientations with sepparate layout's etc.
So i would like to find out, is it possible to disable the configuration change only in special cases (Screen turned off and orientation is landscape)? Or is the right way to override onConfigurationChanged() then manually manage activity recreation inside that function (i guess simply setting different layout resources when orientation is changed simply wont cut it)?
Or is the right way to override onConfigurationChanged() then manually manage activity recreation inside that function?
Yes to an extent.
You cannot set the configChanges attribute programmatically. I guess it's to do with the way an Activity is created. They're created from the XML first and then the overridden methods in your activity implementation are invoked. There's nothing in the API that lets you change the configChanges attribute.
Now in your case it doesn't sound like you need to. If you support both orientations, then if the user locks the device and it rotates back why does it matter? From a UX perspective we know it's in portrait mode again. So should your app when it opens back up.

Avoid reload view after orientation change

I am new to android development . I have been puzzled by this problem in recent times .
Every time orientation changes my view gets reloaded. I want to avoid this. This is happening on change from landscape to portrait and vice versa.
Put below line in your manifest in activity:
android:configChanges="orientation|screenSize"
So, your activity should look like:
<activity
android:configChanges="orientation|screenSize"
android:name=".YourActivity"/>
This way prevent destroying activity on orientation changes.
The reason why Android is reloading the view after the orientation has changed is that this view is most probably another view than the one that was displayed before the orientation change happened, e.g. with new layout, font sizes, imageviews or other resources. The Android system is able to automatically load suitable resources for different screen dimensions (e.g. height, width, density ... ) in case of an orientation change.
For this to happen, Android first calls the activity's onDestroy() method, followed by a call to its onCreate() method to enable the activity to reload resources and activity state.
If for some reason you do not want this behaviour, you can follow the above advice and add an android:configChanges attribute to your activity element. In this case Android calls the activity's onConfigurationChanged() method instead of onDestroy() and onCreate(). onConfigurationChanged() is the method to place your code to adapt to the new orientation, if necessary. If not, it can also be left empty.
For further reading, see http://developer.android.com/guide/topics/resources/runtime-changes.html

Change Layout in Landscape with out destroying and recreating Activity

Hi Everyone iam new to android and stuck with the orientation problem i need to display separate layout in landscape and portrait which i designed separately and placed in layout-large and layout-large-land folders now i need to change layout when device is rotated to landscape with out destroying and recreating the Activity
please help me get out of this problem
Thanks in Advance
my advice as a long time Android programmer is:
Don't do it!
Let the activity be destroyed and re-built with the correct layout.
Just search and research on all the several methods of keeping the data during orientation changes and apply them to your specific case. Below a few to illustrate:
the onCreate(Bundle) receives that bundle that contains information saved during onSavedInstances(Bundle);
User a fragment without a UI (do not call onCreateView) and set it to be retained across rotation with setRetainInstance(true) and use it to remember the data
use the Loader pattern to automatically receive the data it was generated on the previous activity
Replace
layout-large-land
with
layout-land-large
Prevent activity from recreating/destroying
Add attribute android:configChanges="orientation" to the activity declaration in the AndroidManifest.xml file.
The purpose of the android:configChanges attribute is to prevent an activity from being recreated when it's really required.
Let me know if it works for you..
Try this,
Add this code in your mainfest.xml each and every activity.
android:ConfigChanges="keyboardHidden|orientation"

Categories

Resources