I've read up on a lot of questions about immersive sticky mode on Android, but I cannot find a specific answer for my question.
I have read onBackPressed function not working under IMMERSIVE STICKY mode, but my app goes back successfully when an ImageButton is pressed.
I want to remain in immersive sticky mode for the whole duration of the app. There is no problem when I launch a new activity, with and without a shared element transition. However, when I go back, the system bars sometimes appears then soon slide out of view again. This happens especially when I stay on an activity for a while.
This transition is called from _TrickPage.class:
public void setBorroRingsOnClickListener(View view){
dBHelper.close();
Intent intent = new Intent(this, _DominoScene.class);
startActivity(intent);
}
And I call finish() to get back to _TrickPage.class from _Domino.class:
findViewById(R.id.picture).setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
dBHelper.close();
finish();
}
});
Another transition I use is a shared element transition. The following is an inner class of ButtonAdapter, which extends BaseAdapter, an object in my _Submenu Activity:
class MyOnClickListener implements View.OnClickListener {
private final int position;
public MyOnClickListener(int position){
this.position = position;
}
#Override
public void onClick(View v) {
...
//link clicked button to _TrickPage button for shared transition
v.setTransitionName("trick");
//make and start shared transition to _TrickPage
ActivityOptions options = ActivityOptions.makeSceneTransitionAnimation(
(Activity)mContext,
new Pair<View, String>(v, "trick")
);
Intent intent = new Intent(v.getContext(), _TrickPage.class);
v.getContext().startActivity(intent, options.toBundle());
}
}
Finally, to get back to _Submenu from _TrickPage, I call finishaftertransition():
picture.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dBHelper.close();
hideMainContent();
finishAfterTransition();
}
});
I set the UI flags in the onCreate() and onResume() methods of the Activities involved: _Submenu, _TrickPage, and _DominoScene. I also have the following in each activity:
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
//if (hasFocus)
getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
if (hasFocus && playAnimations) {
//showMainContent();
playAnimations = false;
}
}
This is my first time asking a question on Stack Overflow, so please ask for any other parts of my code if you think there could be a problem there. Thanks!
Call this method on your onCreate method from every Activity(right after your setContentView method). And also on the onWindowsFocusChanged
public void goImmersive()
{
getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
}
Related
My app has 3 activities(activity_main,activity_one,activity_two).
I want to full screen only the first activity so I searched and I found out I got to use this:
View decorView = getWindow().getDecorView();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
decorView.setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
}
When I go to the other activities that are not full screen and press the back button and go back to main activity the home button back button is there hiding the bottom of my main activity.How do I solve this?
Also if possible I would like to know how can I make my activity full screen but keep the action bar.
edit:
Im asking for a full screen on one activity and not the other two(except if the action bar stays).If i go on activity_two (which is not full screen) i have the home button back button etc .If i press the back button and go back to the main activity the buttons stay there which i don't want to (not only because i want my main full screen but because the buttons from activity two stay and hide the bottom of my main activity)
As onWindowsFocusChanged() is what you need so try this:1. First, Declare this variable as global in your MainActivity
private int currentApiVersion;
2. then paste this code in onCreate() of that activity.
currentApiVersion = Build.VERSION.SDK_INT;
final int flags = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
if (currentApiVersion >= Build.VERSION_CODES.KITKAT) {
getWindow().getDecorView().setSystemUiVisibility(flags);
final View decorView = getWindow().getDecorView();
decorView.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {
#Override
public void onSystemUiVisibilityChange(int visibility) {
if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
decorView.setSystemUiVisibility(flags);
}
}
});
}
3. Now paste this method in your MainActivity class.
#SuppressLint("NewApi")
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (currentApiVersion >= Build.VERSION_CODES.KITKAT && hasFocus) {
getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
}
}
Now this should work, give it a try.
You can do this in two ways,
Programmatically -- Set FullScreen flag to window of activity inside onCreate() method (just after or before setContentView()). Whom you want to be fullscreen. For example,
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
Window w = getWindow();
w.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
Or you can set theme in your AndroidManifest.xml as,
<activity
android:name=".YourActivityName"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen"/>
Or If you are using AppCompatActivity then you can set as,
<activity
android:name=".YourActivityName"
android:theme="#android:style/Theme.AppCompat.Light.NoActionBar"/>
For more info about the window flags check this link https://developer.android.com/reference/android/view/WindowManager.LayoutParams.html
I want to turn off keys(home, back, recent apps) lights on the activity. Here is my code.
Settings.System.putInt(getApplicationContext().getContentResolver(), "button_key_light", 0);
It works well on Samsung phone, but it doesn't work on other devices such as Google Pixel 2.
Please help me.
You can try this-:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if(keyCode == KeyEvent.KEYCODE_BACK)
{
Log.d("Test", "Back button pressed!");
}
else if(keyCode == KeyEvent.KEYCODE_HOME)
{
Log.d("Test", "Home button pressed!");
}
return super.onKeyDown(keyCode, event);
}
I have solved this problem by using Immersive Full-Screen Mode.
https://github.com/superdev0714/DisableHome.java/blob/master/DisableHome.java
I solved myself. It works well.
private void disableHomebutton() {
final int uiOptions = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // hide nav bar
| View.SYSTEM_UI_FLAG_FULLSCREEN // hide status bar
| View.SYSTEM_UI_FLAG_IMMERSIVE;
mainView.setSystemUiVisibility(uiOptions);
mainView.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {
#Override
public void onSystemUiVisibilityChange(int visibility) {
mainView.setSystemUiVisibility(uiOptions);
}
});
mHomeKeyLocker.lock(this);
}
Libgdx shows these buttons by default. Is it possible to hide them and show them only when user slides with finger down(I think this is how unity has done it)?
Instead of overriding android methods you can enable option in AndroidApplicationConfiguration
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
config.useImmersiveMode = true; // libgdx will handle it
initialize(new Main(), config);
}
To hide virtual Android buttons (on the phones that do not have physical buttons :) ) you have to set the application to use the Immersive Full-Screen mode.
You can achieve this by setting proper flags from android.view.View method by adding this code in your AndroidLauncher class
#TargetApi(19)
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (hasFocus) {
getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |
View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION |
View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |
View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
}
}
Take a look at this tutorial to get more information.
My app runs in fullscreen (immersive mode).
The status bar is hidden at the top.
On some Samsung phones, there is a stylus pen. If the pen is removed, the status bar re-appears and won't go away.
I tried adding a listener for this event but it doesn't get called.
View decorView = getActivity().getWindow().getDecorView();
decorView.setOnSystemUiVisibilityChangeListener
(new View.OnSystemUiVisibilityChangeListener()
{
#Override
public void onSystemUiVisibilityChange(int visibility) {
// Note that system bars will only be "visible" if none of the
// LOW_PROFILE, HIDE_NAVIGATION, or FULLSCREEN flags are set.
if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
// TODO: The system bars are visible. Make any desired
// adjustments to your UI, such as showing the action bar or
// other navigational controls.
System.out.print("Change event");
} else {
// TODO: The system bars are NOT visible. Make any desired
// adjustments to your UI, such as hiding the action bar or
// other navigational controls.
System.out.print("Change event");
}
}
});
Do you know how I can know if the status bar has re-appeared?
In my activity I do this to get it re-hide the status bar:
#Override
public void onWindowFocusChanged(boolean hasFocus)
{
System.out.println("Gotcha");
super.onWindowFocusChanged(hasFocus);
final Handler handler = new Handler();
handler.postDelayed(new Runnable()
{
#Override
public void run() {
System.out.println("Hiding");
_root.setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
}
}, 6000);
}
Following the tutorials on this page(written in 2009) don't work any more. http://www.androidsnippets.com/how-to-make-an-activity-fullscreen
public class FullScreen extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);
}
}
Similarily, trying to change the theme in android.xml has no effect:android:theme="#android:style/Theme.NoTitleBar.Fullscreen"
Here is a screenshot:
After scouring the web for a few hours, i cant find any articles that can help me.
So how, in version 1.5.4 of libgdx, can i get an immersive full screen?
----EDIT----
Here is the now fully working code:
public class AndroidLauncher extends AndroidApplication {
#Override
protected void onCreate (Bundle savedInstanceState) {
hideSystemUi();
super.onCreate(savedInstanceState);
AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
config.hideStatusBar=true;
config.useImmersiveMode=true;
initialize(new game(), config);
}
private void hideSystemUi() {
getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_IMMERSIVE);
}
}
Try this in the android activity:
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (hasFocus) {
getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |
View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION |
View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |
View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
}
}
Note this will only work on API level 19 or above.
Sadly I can't comment due to my current reputation,
however I have found that adding:
config.hideStatusBar = true;
config.useImmersiveMode = true;
in the AndroidLauncher class is enough to show the app in immersive however the app will start in standard mode and then update to immersive mode, more info on the Android immersive mode:
When you use the SYSTEM_UI_FLAG_IMMERSIVE_STICKY flag, an inward swipe in the system bars areas causes the bars to temporarily appear in a semi-transparent state, but no flags are cleared, and your system UI visibility change listeners are not triggered. The bars automatically hide again after a short delay, or if the user interacts with the middle of the screen.
Android sdk page
So my conclusion would be that config.useImmersiveMode use the sticky flag.
On a side note,
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
has to be added to the OnWindowFocusChanged method when targeting an api lvl less than 11