I want to alter something, for example a variable value, when the user rotates the screen.
I know I can have the orientation using something like:
this
but I don't want to define if portrait do this, if landscape do this.
I want to just catch the current orientation and start doing the things I want.
You have to Override the onConfigurationChanged method in your activity and check the current orientation and do whatever you want like this
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Check orientation
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
DoSomethingForLandscapeOrientation();();
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
DoSomethingForPortraitOrientation();
}
}
private void DoSomethingForLandscapeOrientation(){
Log.i("Method For","Landscape");
}
private void DoSomethingForPortraitOrientation(){
Log.i("Method For","Portrait");
}
Related
In my app I have MainActivty which should always be viewed in protrait. If I turn the device to landscape I switch to TrendsActivity (to show some graphs). This is done by the folliwing code in MainActivty:
#Override
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE)
{
Intent intent = new Intent(this, TrendsActivity.class);
startActivity(intent);
}
}
Then when I switch back to portait i simply call this in TrendsActivity:
#Override
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT)
{
finish();
}
}
This finishes the activity an automaticly moves back to MainActivity. So far so good. But if I press the back button while in TrendsActivity I come back to MainActivity in landscape mode, and I don't want that. I tried to call the following code in MainActivity:
#Override
protected void onRestart()
{
super.onRestart();
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
}
This does not work, because now the onConfigurationChanged() if MainActivity is never called...
What to do?
Don't you think you might be achieving a much cleaner design by simply specifying an alternate xml for landscape mode and letting your MainActivity behave accordingly? Even without this, launching another activity on orientation change isn't going to be what most users would expect.
use this in
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
in onResume() of MainActivty
You can try to set your orientation in android manifest only, try below code
android:screenOrientation="portrait"
so exactly your code in manifest looks something like
<activity
android:name=".yourActivityName"
android:screenOrientation="portrait"
</activity>
You should add this to the AndroidManifest:
android:configChanges="orientation"
this way you tell Android to call onConfigurationChanged when there is a change in the orientation.
I solved this by changing the code in my MainActivity to just change the layout XML like fremmedehenvendelser suggested (Tusen takk!)
#Override
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE)
{
//setTheme(android.R.style.Theme_NoTitleBar_Fullscreen);
setContentView(R.layout.activity_trends);
}
else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT)
{
setContentView(R.layout.activity_main);
}
}
Next problem is to set the theme to make this layout show in fullscreen. The code which is commented out does not work... Before I had this in my manifest for TrendsActivity
android:theme="#android:style/Theme.NoTitleBar.Fullscreen"
I have code to set a different layout for portrait and landscape. Is there a way to change the layout while the activity is running, if the user flips the device?
Code to set layout
void SetView()
{
int w=getWindowManager().getDefaultDisplay().getWidth();
int h=getWindowManager().getDefaultDisplay().getHeight();
if (w>h)
setContentView(R.layout.mainHor);
else
setContentView(R.layout.mainVer);
}
Make two new folders in res called layout-land and layout-port. Put your landscape layout in layout-land and your portrait in layout-port. Make sure the xml files have the same name.
Also, as was mentioned in the comments, making just the layout-land folder is good enough as long as you also have a default xml for the Activity in the layout folder. If for any reason Android can't find the layout needed in the alternative layout-[orientation] folder, it will take what it finds in layout.
make two folder under res folder with name layout and layout-land. Put seprate layouts for portrait and landscape.
and then override onConfigurationChanged
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
setContentView(R.layout.homepage_screen);
}
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
setContentView(R.layout.mainHor);
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
setContentView(R.layout.mainVer);
}
}
Do this
#Override
public void onConfigurationChanged(Configuration configuration) {
super.onConfigurationChanged(configuration);
if (configuration.orientation == Configuration.ORIENTATION_LANDSCAPE) {
setContentView(R.layout.landscape_layout);
} else if (configuration.orientation == Configuration.ORIENTATION_PORTRAIT){
setContentView(R.layout.portrait_layout);
}
}
I wonder how can I display the same design when I turn the view to landscape view can anyone help me with this. I tried this code but it didn't do anything
the manifest
android:configChanges="keyboardHidden|orientation"
#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();
}
}
#Override
public void setContentView(int layoutResID)
{
super.setContentView(layoutResID);
}
Did you put the android:configChanges flag inside an Activity deceleration in your Manifest?
Try:
<activity android:name="myActivity" android:label="#string/app_name"
android:configChanges="orientation">
</activity>
If you don't want any layout change when your device rotates, this should suffice, and android will just recalculate and redraw the view again.
If you do want the layout to be changed though, you'll need to load a different layout on your onConfigurationChanged function:
#Override
public void onConfigurationChanged(Configuration newConfig)
{
if (newConfig.orientation == android.content.res.Configuration.ORIENTATION_PORTRAIT)
{
setContentView(R.layout.portarit_view);
}
else if (newConfig.orientation == android.content.res.Configuration.ORIENTATION_LANDSCAPE)
{
setContentView(R.layout.landscape_view);
}
}
BTW, you can have a decent preview of how it will look like in your layout xml "Graphical Layout" tab.
If the user is putting data in edittext fields and orientation get changed, the onConfigChange method is called and new layout is populated,with empty edittext fields, i want to retain the data in edittext fields entered by user befor orientation change.
Any kind of help will highly appreciated.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
int orientation_default = getResources().getConfiguration().orientation;
switch (orientation_default) {
case Configuration.ORIENTATION_PORTRAIT: {
setContentView(R.layout.registration);
break;
}
case Configuration.ORIENTATION_LANDSCAPE: {
setContentView(R.layout.registration_horizontal);
break;
}
}
findViewById(R.id.backtohomepage).setOnClickListener(this);
findViewById(R.id.backtologinpage).setOnClickListener(this);
findViewById(R.id.btn_notregyetsubmit).setOnClickListener(this);
findViewById(R.id.termsandconditions_id).setOnClickListener(this);
findViewById(R.id.btn_notregyetsubmit).setOnClickListener(this);
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
setContentView(R.layout.registration);
findViewById(R.id.termsandconditions_id).setOnClickListener(this);
findViewById(R.id.backtohomepage).setOnClickListener(this);
findViewById(R.id.backtologinpage).setOnClickListener(this);
} else if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
setContentView(R.layout.registration_horizontal);
}
findViewById(R.id.backtologinpage).setOnClickListener(this);
findViewById(R.id.btn_notregyetsubmit).setOnClickListener(this);
findViewById(R.id.termsandconditions_id).setOnClickListener(this);
findViewById(R.id.btn_notregyetsubmit).setOnClickListener(this);
};
Put android:configChanges="orientation" for that activity node in the manifest.
EDIT:
Check this question : Activity restart on rotation Android
<activity
android:name="com.Dashboard"
android:configChanges="keyboardHidden|orientation"
>
</activity>
paste this in your manifest...
http://developer.android.com/guide/topics/resources/runtime-changes.html .. go through this link..
I have activity with android:configChanges="orientation|keyboardHidden"
I am trying to change image in ImageView when the device orientation changes with:
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// refresh the instructions image
ImageView instructions = (ImageView) findViewById(R.id.img_instructions);
instructions.setImageResource(R.drawable.img_instructions);
}
This only works the first time when the phone is rotated but not after that.
Can someone pls tell me why this happens?
Peceps' own answer IMO is the correct one since it does not depend on giving different names to the drawables. It seems that the resource id is cached but the drawable is not, so we can supply the drawable to the ImageView instead of the resource id, making the solution a little more elegant (avoiding the try/catch block):
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// refresh the instructions image
ImageView instructions = (ImageView) findViewById(R.id.instructions);
instructions.setImageDrawable(
getResources().getDrawable(R.drawable.img_instructions));
}
I think you trying to do something like this
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// refresh the instructions image
ImageView instructions = (ImageView) findViewById(R.id.img_instructions);
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
instructions.setImageResource(R.drawable.img_instructions_land);
} else {
instructions.setImageResource(R.drawable.img_instructions_port);
}
}
use this you can multiple time change orientation it's work fine
public class VersionActivity extends Activity {
ImageView img;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
img = (ImageView) findViewById(R.id.imageView1);
}
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
img.setImageResource(R.drawable.splash);
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
img.setImageResource(R.drawable.icon);
}
}
}
for more information goto How to detect orientation change in layout in Android?
Thanks, both answers are correct and work if I am using different image name for portrait and landscape.
To use the same name this works:
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// refresh the instructions image
ImageView instructions = (ImageView) findViewById(R.id.instructions);
// prevent caching
try {
instructions.setImageResource(0);
} catch (Throwable e) {
// ignore
}
instructions.setImageResource(R.drawable.img_instructions);
}