I have a ListActivity which I want to show when the phone's orientation is portrait and I have another normal Activity which I want to show when the user rotates the phone to landscape mode.
Some code:
public class ListActivity extends ListActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (this.getResources (). getConfiguration (). orientation == Configuration.ORIENTATION_PORTRAIT) {
//Do stuff
}else if (this.getResources (). getConfiguration (). orientation == Configuration.ORIENTATION_LANDSCAPE) {
Intent myIntent = new Intent(getApplicationContext(), AnotherActivity.class);
startActivity(myIntent);
}
}
}
This approach works, but has some problems. Is there a more correct way? The problem with this method is that when you rotate your phone and press the back button, the screen just becomes black since the phone is not rotated. So should I implement the rotation in another way, or modify the back button in some way so that the user cant return to the previous Activity?
It is a normal way. But it is long - 5 sec of waiting.
There is another way:
register orientation change as a configuration change
A.
activity android:name=".SomeActivity"
android:label="#string/app_name"
android:configChanges="orientation"
And process it as a config change.
B.
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
setContentView(R.layout.main);
SetupActivity();
}
Related
I wanna know how I can avoid splash screen to launch when my app is in recents.
I want the app to open the activity where user was in it before when he/she clicks back button of device and opens
app from recent screen.
This is a splash screen activity :
public class MainActivity extends AppCompatActivity {
//variables
private static int SPLASH_SCREEN = 1000;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent intent = new Intent(MainActivity.this , Main_Page.class);
startActivity(intent);
finish();
}
},SPLASH_SCREEN);
}
In the manifest, specify for the activity:
android:noHistory="true"
I found the solution to my problem.
This happens because when I click back button of device, according to
activity lifecycle , ondestroyview() will be called. so when
I returned from recents screen , app started again.
what I've done is that in the next activity which appears after Splash
Screen , I used onBackPressed like this :
#Override
public void onBackPressed() {
moveTaskToBack(true);
}
I'm allowing all possible orientation for my app both for portrait and landscape (small-normal-large-xlarge) but after testing it on a small screen, I just didn't like how it appears so, what I'm trying to do is disable landscape for small layouts. Is there a way to do this ?
All I found was changes to do on manifest file but I believe that by reconfiguring the manifest I will apply the changes to all layouts.
The easiest way is to put this in the onCreate() method of all your Activities (better yet, put it in a BaseActivity class and extend all your Activities from it)
#Override
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
if (isLargeDevice(getBaseContext())) {
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
} else {
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
}
You can use this method to detect if the device is a phone or tablet:
private boolean isLargeDevice(Context context) {
int screenLayout = context.getResources().getConfiguration().screenLayout;
screenLayout &= Configuration.SCREENLAYOUT_SIZE_MASK;
switch (screenLayout) {
case Configuration.SCREENLAYOUT_SIZE_SMALL:
case Configuration.SCREENLAYOUT_SIZE_NORMAL:
return false;
case Configuration.SCREENLAYOUT_SIZE_LARGE:
case Configuration.SCREENLAYOUT_SIZE_XLARGE:
return true;
default:
return false;
}
}
Check this link you can check the type of device and set orientation as needed
Android: allow portrait and landscape for tablets, but force portrait on phone?
You can programatically handle runtime configuration changes like that
in your manifest:
<activity android:name=".MyActivity"
android:configChanges="orientation|keyboardHidden"
android:label="#string/app_name">
in your activity
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
///check the screen size and change it to potrait
}
}
or check this answer to see how to check the screen size and change it
for example 480 screen size devices:Apply in oncreate method:
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
if(width==480){
if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
} else setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
In my application, I have two activities. First is a splash screen, which simply shows the application name and few other info.
Upon clicking on the splash screen activity, I'm loading the main activity. My app works fine, but I'm facing a small issue. If I press back button from my main activity, control is going to splash screen activity. But I don't want to show the splash screen activity again, I want to avoid splash screen activity when pressing Back button.
Is it possible? If so how?
In your AndroidManifest.xml file, add android:noHistory="true" attribute in your splash screen <activity>.
As I understand, you want the splash activity to not show after changing activity. You should note activities save On Stack and with starting new activity push on it and with finish you pop on top stack. I think that if you the call finish() method your problem fix as in your splash screen activity where you call StartActivity insert finish() after
public void onClick(View v) {
Intent intent = new Intent(Main.this, Splash.class);
startActivity(intent);
finish();
}
Hope to be useful :)
You can just call
finish();
In your Splash screen when you jump to the second screen.
In addition to the above answers, you should note
that:
1: by calling the finish() method, the Splash activity
will close after execution, meaning that it will not be
available in the stack.
#Override
protected void onCreate(Bundle saveInstsnceState){
super.onCreate( saveInstanceState);
\\ do something here
Intent intentSplash = new Intent(SplashActivity.this, NextActivity.class);
StartActivity(intentSplash);
finish ();
}
You will achieve your aim using the method above
but...
2: If you want to prevent your users from force
exiting the app (pressing back button) while the
splash activity in still ongoing which is the best
practice in android, then you need to call the
onBackPressed () method.
Class NoBackSplash{
#Override
protected void onCreate(Bundle saveInstsnceState){
super.onCreate( saveInstanceState);
\\ do something here
Intent intentSplash = new Intent(SplashActivity.this, NextActivity.class);
StartActivity(intentSplash);
finish ();
}
#Override
public void OnBackPressed(){
};
}
With this OnBackPressed() method, your splash activity will not be force to exit no matter how hard the user try.
I understand, you want the splash activity to not show when you click on back button. First of all you should know that all the activities on android are in form of STACK. So what we need we just end the splash activity after it execute. we can do this by calling finish() method in android studio. here is the solution:
Intent intent = new Intent(MainActivity.this, home.class);
startActivity(intent);
finish();
public class Splash extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
Handler hd = new Handler();
hd.postDelayed(new Runnable() {
#Override
public void run() {
Intent i = new Intent(Splash.this,MainActivity.class);
startActivity(i);
#by calling finish() method,splash activity will close after execution
finish();
}
},3000);
}}
How do you detect that the user has changed the orientation of the display screen from portrait to landscape and vice versa?
The Android application must adjust to the new orientation. The system should be sending a change of orientation message to all open applications so that they can adjust. I could not find anything about this in the docs? Sorry if i copy these statement from somewhere else but i'm kinna new ina android/java, can someone guide me along?
Its just one of the Preference screen in multiple tabs i have, but i just want mere change of screen orientation when the phone position changes...
public class Tab2Activity extends PreferenceActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.tab2);
}
}
Android will automatically recreate (destroy and then create) your activity when the orientation changes. If you want a special layout for landscape, you can create a special resource file in the layout-land folder, which will be loaded after the activity is recreated.
On the other hand, if you want to override the default behavior so that you can do something custom when the orientation changes, you can override the onConfigurationChanged method like so:
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if(newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
//do something if portrait...
}
}
http://developer.android.com/reference/android/app/Activity.html#onConfigurationChanged%28android.content.res.Configuration%29
EDIT: in code (add this to your Activity):
#Override
public void onConfigurationChanged(Configuration _newConfig){
super.onConfigurationChanged(_newConfig);
int height = getWindowManager().getDefaultDisplay().getHeight();
int width = getWindowManager().getDefaultDisplay().getWidth();
if(width > height){
// landscape
}else{
// portrait
}
}
does anybody knows how to launch a new activity with rotation animation?
I'll try to explain what i want to do:
For instance i've looked for android app exemple in skd sample "apidemos" and i've found a class named com.exemple.android.apis.animation.Rotate3dAnimation.java and com.exemple.android.apis.animation.Transition3d.java. These classes allow me to switch between image with rotation effect.
That I would like to know if there is a way to do the same but instead of image, i will be activity (whith new layout).
The window manager doesn't support 3d transformations at this point; since each activity is a window, animations between activities are window animations, so they are limited to what the window manager supports.
This is how we can accomplish this.
Suppose we want to switch from Activity A to B. First we will animate activity A then we will start activity B in overridden function "onAnimationFinished". This will ensure that activity B is started only after animation for activity A has finished off.
// we will only animate activity A here.
// The activity B will be animated from its onResume() - be sure to implement it.
final Intent intent = new Intent(getApplicationContext(), B.class);
// disable default animation for new intent
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
//Animate A
ActivitySwitcher.animationOut(findViewById(R.id.A), getWindowManager(), new ActivitySwitcher.AnimationFinishedListener() {
#Override
public void onAnimationFinished() {
// Start activity B
startActivity(intent);
}
});
Now override "onResume" function for activity B
#Override
protected void onResume() {
// animateIn this activity
ActivitySwitcher.animationIn(findViewById(R.id.help_top), getWindowManager());
super.onResume();
}
You can see here for working example
http://blog.robert-heim.de/karriere/android-startactivity-rotate-3d-animation-activityswitcher/comment-page-1/#comment-12025