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">
Related
I found out, that I can hide the virtual buttons of an android phone with:
this.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
But once the user makes them visible by swiping from the edge, they stay visible.
How can I hide them again after a moment ?
Thanks in advance!
Edit:
I start a new activity.
When this new activity is started I call this in the onCreate-Method to hide the actionbar and the virtual buttons:
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
this.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
Edit:
#Override
public void onSystemUiVisibilityChange(int visibility) {
if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN);
} else {
}
}
This is what your doing:
https://developer.android.com/training/system-ui/navigation.html
This is what you also want to be doing:
https://developer.android.com/training/system-ui/visibility.html
So basically, when you hide an item, you need to listen (check) whether it's been changed (Turned On/Off) and from there re-enable it. I would copy/paste the code, but it's in those links.
HomeActivity.java
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
Button btnopen = (Button)findViewById(R.id.btnWindowAnimation);
btnopen.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent slideactivity = new Intent(HomeActivity.this, CartActivity.class);
Bundle bndlanimation =
ActivityOptions.makeCustomAnimation(getApplicationContext(), R.anim.animation,R.anim.animation2).toBundle();
startActivity(slideactivity, bndlanimation);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.layout.home, menu);
return true;
}
HomeActivity has a button and it will help to slide the 1st screen and bring the second screen.
animation.xml
android:fromXDelta="100%p"
android:toXDelta="25%p"
android:duration="5000"
This animation code is helping me to slide my screen after pressing the button. As shown in this video
https://www.youtube.com/watch?v=EzgGGWpRES0
I want to stop the new screen at the middle without covering the 1st screen completely.
Any suggestions? I have used another activity called cart( i haven't used fragments here)
Sounds like you want a NavigationDrawer.
You can find out more information on the Android developer site:
https://developer.android.com/design/patterns/navigation-drawer.html
https://developer.android.com/training/implementing-navigation/nav-drawer.htmlA
Your Requirement is to partially Display an Overlapping Activity on an Existing Activity. This is Possible with Sliding Menu Library (Which does not mean to technically create a floating Activity, but to create a Floating View integrated within the Sliding Menu that will be displayed partially on top on an Activity).
In case, if you are regarding this as some sort of Default Menu then please take a look at this Image
If this is your actual Requirement, then here are the details for you.
Sliding Menu Library is an OpenSource Library, you can use it for displaying a partially visible Sliding Menu on top of an Activity.
Here is the link of Sliding Menu Lib :
https://github.com/jfeinstein10/SlidingMenu
You can navigate to the example directory present in this Project for the demonstration of it's Usage.
I hope thie helps :)
Based on the accepted answer here, I added this code to an Activity as what I hoped to be the first step in changing the assigned Layout for an Activity:
#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();
setContentView(R.layout.activity_settings_landscape);
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
setContentView(R.layout.activity_settings);
}
}
...and modified the Activity's element in AndroidManifest.xml. e.g., changing it from this:
<activity
android:name="hhs.app.SettingsActivity"
android:label="#string/title_activity_settings" >
</activity>
...becomes this:
<activity
android:name="hhs.app.SettingsActivity"
android:configChanges="orientation|keyboardHidden"
android:label="#string/title_activity_settings" >
</activity>
(I added the "configChanges" jazz).
But, although mashing keypad.9 does indeed flip the orientation in the emulator, no toast is being raised (the breakpoint on the "if" line of onConfigurationChanged() is not being hit). What am I doing wrong or omitting?
Note: It made no difference when I removed "|keyboardHidden" from the "configChanges" line in AndroidManifest (it was just a hunch - I wondered if it meant "pay no attention to an orientation change made via the keyboard").
UPDATE
I tried araut's suggestion, and also the one from Jared Burrows here, but neither one works for me.
Cold this be simply an emulator problem? I have no device to test it with yet, so I can't empirically answer that question myself.
UPDATE 2
Based on what I read here, I thought maybe adding some OrientationEventListener jazz would solve this dilemma, but no!
This is the code I added (shown in context; what's new is the OrientationEventListener stuff):
public class SettingsActivity extends ActionBarActivity {
private OrientationEventListener mOrientationListener;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
. . .
mOrientationListener = new OrientationEventListener(getApplicationContext()) {
#Override
public void onOrientationChanged(int orientation) {
Toast tostadaOrient = Toast.makeText(SettingsActivity.this,
orientation, Toast.LENGTH_SHORT);
tostadaOrient.setGravity(Gravity.CENTER, 0, 0);
tostadaOrient.show();
}
};
mOrientationListener.enable();
}
...but I still get no reaction (no toasts are raised from here or the onConfigurationChanged() event).
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.
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 :)