Change activity when orientation changes - android

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"

Related

do something on orientation change

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");
}

Lock screen in portrait on configuration change

I have a simple activity with a boolean. I want it to change from portrait to landscape if and only if that boolean is false (only if the screen changes orientation, of course).
I tried this:
#Override
public void onConfigurationChanged(final Configuration newConfig) {
super.onConfigurationChanged((listening) ? getListeningConfiguration(newConfig)
: newConfig);
setContentView(R.layout.medidor);
preconfigureLayout();
}
public Configuration getListeningConfiguration(final Configuration c) {
c.orientation = Configuration.ORIENTATION_PORTRAIT;
return c;
}
But it simply won't work.
How can I force an activity to remain in portrait state unless I tell it it can change?
In manifest file in activity tag add
android:configChanges="orientation|keyboardHidden"
android:screenOrientation="portrait"
That will keep your activity in portrait mode. then use your code for orientation change.
Just call this to keep it in portrait:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
That should do the trick

change layout of the activity at run time in android

i am developing one android application,there i need to change layout of activity at run time
i want to know about ,how to change a layout of an activity as view mode change from portrait to landscape or visa-verse
please anyone tell me what is feasible and efficient solution for above problem.
Thanks in Advance
Best approach is to have a folder for landscape orientation also, you will name it like
layout-land
you will put another main.xml inside that folder, android framework will automatically select that layout for landscape orientation.
More info here from android guide for supporting multiple screen
Code for detecting orientation change
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if(newConfig.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_NO) {
SharedVariables.isScreenOrientPotrail=false;
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
else if(newConfig.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_YES){
SharedVariables.isScreenOrientPotrail=true;
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
}
LayoutInflater inflater = context.getLayoutInflater();
View rowView = inflater.inflate(R.layout.your_dyanmic_layoutid, null, true);
add rowview to your mainlayout.
i am pasting code here..try this..
public void onConfigurationChanged(Configuration orient)
{
super.onConfigurationChanged(orient);
// Checks the orientation of the screen
if (orient.orientation == Configuration.ORIENTATION_LANDSCAPE)
{
//code here for LANDSCAPE..
));
}
else if (orient.orientation == Configuration.ORIENTATION_PORTRAIT)
{
//code here for PORTRAIT ..
}
}
CAll Method;
#Override
public void onCreate(Bundle savedInstanceState)
{
onConfigurationChanged(getResources().getConfiguration());
}

Android some buttons do not show when turned to landscape

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.

Save Data of previous layout on orientation change while implementing onConfigChange()

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..

Categories

Resources