Reputation: 1074
Is it somehow possible to set the orientation of the app depending on which orientation was used when the app was started?
Example
By that i mean lets say user started my app in Landscape. O would like it to save that state and dont let the user change the orientation by turrning the phone ( I want to lock it, until user re-runs application and from there app agains checks if the user started it in landscape or portrait )
CODE (so far)
Configuration newConfig = getResources().getConfiguration();
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
// set background for landscape through app
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
// set background for portrait through app
}
This was something i found here on SO. Could i do it like this? Put this in my first activity and then use the orientation state from here for the rest of the activites.
Upvotes: 0
Views: 140
Reputation: 1710
You can use SharedPreferences to store a value that will be saved even if the app is closed.
Just make a class SaveOrientation :
public class SaveOrientation {
static final String ORIENTATION = "orientation";
static SharedPreferences getSharedPreferences(Context ctx) {
return PreferenceManager.getDefaultSharedPreferences(ctx);
}
// value will be between 0 and 1
public static void setOrientation(Context ctx, int value) {
SharedPreferences.Editor editor = getSharedPreferences(ctx).edit();
editor.putInt(ORIENTATION, value);
editor.commit();
}
public static String getOrientation(Context ctx) {
return getSharedPreferences(ctx).getString(ORIENTATION, "");
}
}
And then in your MainActivity :
int portrait = 0;
int landscape = 1;
.
.
.
// in your onCreate :
Configuration newConfig = getResources().getConfiguration();
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
SaveOrientation.setOrientation(this,portrait);
}else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
SaveOrientation.setOrientation(this,landscape);
}
applyOrientation();
.
.
.
// method applyOrientation
private void applyOrientation(){
int orientation = SaveOrientation.getOrientation(this);
if(orientation == portrait){
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}else if (orientation == landscape){
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
Upvotes: 2
Reputation: 928
You can implement it by using ActivityInfo class.
ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
Check ActivityInfo here
Button buttonSetPortrait = (Button)findViewById(R.id.setPortrait);
Button buttonSetLandscape = (Button)findViewById(R.id.setLandscape);
buttonSetPortrait.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
});
buttonSetLandscape.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
});
http://android-er.blogspot.in/2011/08/set-screen-orientation-programmatically.html
In case anyone would like to obtain meaningful orientation description (like that passed to onConfigurationChanged(..) with those reverseLandscape, sensorLandscape and so on), simply use getRequestedOrientation()
Upvotes: 1