Change orientation without loading again - java

I want to change the orientation, but i have two different xml, one for portrait and the other one for landscape. The same information appear in the screen, except that it's moved to use some space. If i do :
android:configChanges="keyboardHidden|orientation"
It will only change from portrait to landscape in the same xml.
How can i change that ?

Set your view as a class variable:
private TextView yourView;
Add the following to the onCreate() method:
if (savedInstanceState != null) {
((TextView)findViewById(R.id.yourview)).restoreState(savedInstanceState);
}
And add an override on the saving of the instance state to save the state
#Override
protected void onSaveInstanceState(Bundle outState) {
yourView.saveState(outState);
}
Finally:
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
You should be able to manage config changes with this...

Override the onConfigurationChanged() method in your activity. Check the current orientation and set appropriate content view using setContentView().
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (newConfig.getConfiguration() == Configuration.ORIENTATION_LANDSCAPE) {
setContentView(landscape_layout);
} else {
setContentView(portrait_layout);
}
initViews();
}

In your Activity, override this method:
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
This will be triggered when orientation is changed. You can find out in what orientation you are using the parameter.

That's not how android works. If you want a different layout for portrait/landscape you must let the system handle the configuration change for you and load the appropriate layout xml.

Put this in your manifest
android:configChanges="orientation|keyboardHidden|screenSize|uiMode"
It solves mine and your problem :)

Related

Screen rotation adapter notifyDataSetChanged not working

I have a problem with updating RecyclerView when rotating the screen. By clicking on fab button I'm showing dialog fragment with input fields. Then when I'm rotating the screen and saving data my RecyclerView is not updating. I'm setting adapter in onCreate method. I should rotate once more in order to see fresh data. How can solve this issue.
Thanks in advance.
dataSource = new MyBusinessDataSource(this);
try
{
dataSource.openForRead();
}
catch (SQLException sqlEx)
{
Toast.makeText(MyBusinessMainActivity.this, R.string.sqlite_exception, Toast.LENGTH_SHORT).show();
return;
}
categories = dataSource.getAllCategories();
adapter = new CategoriesAdapter(this, categories);
rv_list_cats.setAdapter(adapter);
dataSource.close();
You need to override your onConfigurationChanged method in your activity or fragment and update your data there. Like.
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if(adapter!=null)
{
categories = dataSource.getAllCategories();
adapter.notifyDataSetChanged();
}
}
Also if you are not using the ScreenConfig property in your manifest use that looks like this.
android:configChanges="orientation|screenSize"

Don't restart application on orientation change

I'm new to android development and have been making simple apps and in all of them have been making a method called void logic() that I have called at the end of my ViewDidLoad method. logic() contains all of the functionality that my app has. The issue with this is that whenever the screen changes orientation the ViewDidLoad method is re-called and then my app starts as if the application is being opened for the first time. Does anyone know how to do this? This is similar to this question Don't reload application when orientation changes however I do want ViewDidLoad to be re-called when the orientation is changed so that the view is recreated with the new orientation. However I don't want the application to be restarted. Thanks.
You can use :
#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();
}
}
or
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
And your activity manifest :
<activity android:name=".yourname"
android:configChanges="orientation|keyboardHidden">
</activity>

Saving the activity state when rotating the screen

I'm working on an android app, right now I limited the user to only use horizontal view for all activities.
I want to be able to give the user the option to rotate the screen, but when I do that, the activity starts from the beginning instead of just staying the same.
Any idea how to save the state when rotating the screen?
Firstly, you have to override onConfigurationChanged(Configuration) within your Activity.
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();
}
}
You also have to edit the appropriate element in your manifest file to include the android:configChanges Just see the code below:
<activity android:name=".MyActivity"
android:configChanges="orientation|keyboardHidden"
android:label="#string/app_name">
I also like Marko's idea but it's simply not efficient. This way, we don't have to call onCreate/onStartup/etc. all the time we do a simple rotate. There is no need to "rebuild" the infrastructure from scratch (e.g. getting Views,..)
Each time you rotate the device, onCreate method is being called again. You can save the values by overriding onSavedInstanceState and get them back in onRestoreInstanceState or in onCreate method. For example lets save boolean(you can save whatever you want):
save the value:
public void onSaveInstanceState(Bundle outState) {
outState.putBoolean("booleanValue", true);
}
restore the value (you can call this in onCreate as well):
protected void onRestoreInstanceState(Bundle savedInstanceState) {
if (savedInstanceState != null && savedInstanceState.containsKey("booleanValue")) {
boolean myBoolean = savedInstanceState.getBoolean("booleanValue");
}
super.onRestoreInstanceState(savedInstanceState);
}
<activity (...)
android:configChanges="orientation|keyboard"
(...)>
Adding this to your manifest does not allow Android to restart the activity after chaging the layout.

Hiding title bar / notification bar when device is oriented to landscape

I want my title bar to be hidden when I turn my device to landscape. I have seen the XML option to hide the title bar, and the following example of doing it programmatically:
//Remove title bar
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
//Remove notification bar
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
BUT, I am using configChanges parameter for orientation and screenSize, so my activity is not re-created again when it is orienting to landscape (I'm doing this for some reasons). So I cannot use the above methods as these calls need to be made before setContentView().
So any ideas? Thank you.
was searching for an answer, ended up here, put the pieces together and here they are:
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
else {
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
Assuming you have defined configChangesfor the Activity in the manifest then you can achieve your issue overriding onConfigurationChanged method:
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if(newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
getActionBar().hide();
} else {
getActionBar().show();
}
}
You will have to use getSupportActionBar() instead of getActionBar() if using the support library.
first check your device orientation by using following code
The current configuration, as used to determine which resources to retrieve etc, as available from the Resources' Configuration object as:
getResources().getConfiguration().orientation
Then do the necessary coding related to hide title bar and notification bar.
#Override
public void onConfigurationChanged(Configuration config) {
super.onConfigurationChanged(config);
if(config.orientation == Configuration.ORIENTATION_LANDSCAPE) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
getActionBar().hide();
} else {
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
getActionBar().show();
}
}
You can use this I have tried by my self.
In your "AndroidManifest.xml", in the activity tag you can specify "NoTitleBar" in the theme property so that it will always hide the title:
<activity
android:name="com.my_package.MyActivity"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen"
android:configChanges="navigation|orientation|screenLayout|layoutDirection">

How to register a listener for orientation change?

My android app will run at background and I need to be notify immediately when orientation change. eg. When user rotate the phone from portrait to landscape (or landscape to portrait), the system should be tell my app that the orientation has changed.
Thanks.
This method is called when screen rotated
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
//Do stuff here
}
You do not have to explicitly set the Orientation listener.
onConfigurationChanged will be called by the system.
#Override
public void onConfigurationChanged(Configuration myConfig) {
super.onConfigurationChanged(myConfig);
int orient = getResources().getConfiguration().orientation;
switch(orient) {
case Configuration.ORIENTATION_LANDSCAPE:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
break;
case Configuration.ORIENTATION_PORTRAIT:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
break;
default:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
}
}
But you can implement onConfigurationChanged in daemon.
here : Service.

Categories

Resources