Last active
July 3, 2018 17:35
-
-
Save timrae/178cdc310cfa36a2f91e to your computer and use it in GitHub Desktop.
Immersive fullscreen example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.mikepenz.materialdrawer.app; | |
import android.graphics.Color; | |
import android.os.Bundle; | |
import android.os.Handler; | |
import android.os.Message; | |
import android.support.v7.app.AppCompatActivity; | |
import android.support.v7.widget.Toolbar; | |
import android.view.GestureDetector; | |
import android.view.MenuItem; | |
import android.view.MotionEvent; | |
import android.view.View; | |
import com.mikepenz.fontawesome_typeface_library.FontAwesome; | |
import com.mikepenz.materialdrawer.Drawer; | |
import com.mikepenz.materialdrawer.DrawerBuilder; | |
import com.mikepenz.materialdrawer.model.PrimaryDrawerItem; | |
import com.mikepenz.materialdrawer.model.SecondaryDrawerItem; | |
import com.mikepenz.materialdrawer.model.SectionDrawerItem; | |
public class FullscreenDrawerActivity extends AppCompatActivity { | |
private static final int PROFILE_SETTING = 1; | |
//save our header or result | |
private Drawer result = null; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_sample_fullscreen_dark_toolbar); | |
// Handle Toolbar | |
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); | |
setSupportActionBar(toolbar); | |
toolbar.setBackgroundColor(Color.BLACK); | |
toolbar.getBackground().setAlpha(90); | |
getSupportActionBar().setTitle(R.string.drawer_item_fullscreen_drawer); | |
//Create the drawer | |
result = new DrawerBuilder() | |
.withActivity(this) | |
.withToolbar(toolbar) | |
.withFullscreen(true) | |
.addDrawerItems( | |
new PrimaryDrawerItem().withName(R.string.drawer_item_home).withIcon(FontAwesome.Icon.faw_home).withIdentifier(1), | |
new PrimaryDrawerItem().withName(R.string.drawer_item_free_play).withIcon(FontAwesome.Icon.faw_gamepad), | |
new PrimaryDrawerItem().withName(R.string.drawer_item_custom).withIcon(FontAwesome.Icon.faw_eye), | |
//add some more items to get a scrolling list | |
new SectionDrawerItem().withName(R.string.drawer_item_section_header), | |
new SecondaryDrawerItem().withName(R.string.drawer_item_settings).withIcon(FontAwesome.Icon.faw_cog), | |
new SecondaryDrawerItem().withName(R.string.drawer_item_help).withIcon(FontAwesome.Icon.faw_question).withEnabled(false), | |
new SecondaryDrawerItem().withName(R.string.drawer_item_open_source).withIcon(FontAwesome.Icon.faw_github), | |
new SecondaryDrawerItem().withName(R.string.drawer_item_contact).withIcon(FontAwesome.Icon.faw_bullhorn), | |
new SectionDrawerItem().withName(R.string.drawer_item_section_header), | |
new PrimaryDrawerItem().withName(R.string.drawer_item_custom).withIcon(FontAwesome.Icon.faw_eye), | |
new PrimaryDrawerItem().withName(R.string.drawer_item_custom).withIcon(FontAwesome.Icon.faw_eye), | |
new PrimaryDrawerItem().withName(R.string.drawer_item_custom).withIcon(FontAwesome.Icon.faw_eye), | |
new PrimaryDrawerItem().withName(R.string.drawer_item_custom).withIcon(FontAwesome.Icon.faw_eye), | |
new PrimaryDrawerItem().withName(R.string.drawer_item_custom).withIcon(FontAwesome.Icon.faw_eye), | |
new PrimaryDrawerItem().withName(R.string.drawer_item_custom).withIcon(FontAwesome.Icon.faw_eye), | |
new PrimaryDrawerItem().withName(R.string.drawer_item_custom).withIcon(FontAwesome.Icon.faw_eye), | |
new PrimaryDrawerItem().withName(R.string.drawer_item_custom).withIcon(FontAwesome.Icon.faw_eye) | |
) | |
.withSavedInstance(savedInstanceState) | |
.build(); | |
// Set immersive mode | |
getWindow().getDecorView().setOnSystemUiVisibilityChangeListener( | |
new View.OnSystemUiVisibilityChangeListener() { | |
@Override | |
public void onSystemUiVisibilityChange(int flags) { | |
boolean visible = (flags & View.SYSTEM_UI_FLAG_HIDE_NAVIGATION) == 0; | |
if (visible) { | |
getSupportActionBar().show(); | |
} else { | |
getSupportActionBar().hide(); | |
} | |
} | |
}); | |
View contentView = findViewById(R.id.frame_container); | |
contentView.setClickable(true); | |
final GestureDetector clickDetector = new GestureDetector(this, | |
new GestureDetector.SimpleOnGestureListener() { | |
@Override | |
public boolean onSingleTapUp(MotionEvent e) { | |
boolean visible = (getWindow().getDecorView().getSystemUiVisibility() | |
& View.SYSTEM_UI_FLAG_HIDE_NAVIGATION) == 0; | |
if (visible) { | |
hideSystemUI(); | |
} else { | |
showSystemUI(); | |
} | |
return true; | |
} | |
}); | |
contentView.setOnTouchListener(new View.OnTouchListener() { | |
@Override | |
public boolean onTouch(View view, MotionEvent motionEvent) { | |
return clickDetector.onTouchEvent(motionEvent); | |
} | |
}); | |
hideSystemUI(); | |
/* | |
//USE THIS CODE TO GET A FULL TRANSPARENT STATUS BAR | |
//YOU HAVE TO UNCOMMENT THE setWindowFlag too. | |
if (Build.VERSION.SDK_INT >= 19 && Build.VERSION.SDK_INT < 21) { | |
setWindowFlag(this, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, true); | |
} | |
if (Build.VERSION.SDK_INT >= 19) { | |
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN); | |
} | |
if (Build.VERSION.SDK_INT >= 21) { | |
setWindowFlag(this, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, false); | |
getWindow().setStatusBarColor(Color.TRANSPARENT); | |
} | |
*/ | |
} | |
/* | |
public static void setWindowFlag(Activity activity, final int bits, boolean on) { | |
Window win = activity.getWindow(); | |
WindowManager.LayoutParams winParams = win.getAttributes(); | |
if (on) { | |
winParams.flags |= bits; | |
} else { | |
winParams.flags &= ~bits; | |
} | |
win.setAttributes(winParams); | |
} | |
*/ | |
@Override | |
public void onWindowFocusChanged(boolean hasFocus) { | |
super.onWindowFocusChanged(hasFocus); | |
// When the window loses focus (e.g. the action overflow is shown), | |
// cancel any pending hide action. When the window gains focus, | |
// hide the system UI. | |
if (hasFocus) { | |
delayedHide(300); | |
} else { | |
mHideHandler.removeMessages(0); | |
} | |
} | |
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_LOW_PROFILE | |
| View.SYSTEM_UI_FLAG_IMMERSIVE); | |
} | |
private void showSystemUI() { | |
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | |
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | |
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN); | |
} | |
private final Handler mHideHandler = new Handler() { | |
@Override | |
public void handleMessage(Message msg) { | |
hideSystemUI(); | |
} | |
}; | |
private void delayedHide(int delayMillis) { | |
mHideHandler.removeMessages(0); | |
mHideHandler.sendEmptyMessageDelayed(0, delayMillis); | |
} | |
@Override | |
protected void onSaveInstanceState(Bundle outState) { | |
//add the values which need to be saved from the drawer to the bundle | |
outState = result.saveInstanceState(outState); | |
super.onSaveInstanceState(outState); | |
} | |
@Override | |
public boolean onOptionsItemSelected(MenuItem item) { | |
//handle the click on the back arrow click | |
switch (item.getItemId()) { | |
case android.R.id.home: | |
onBackPressed(); | |
return true; | |
default: | |
return super.onOptionsItemSelected(item); | |
} | |
} | |
@Override | |
public void onBackPressed() { | |
//handle the back press :D close the drawer first and if the drawer is closed close the activity | |
if (result != null && result.isDrawerOpen()) { | |
result.closeDrawer(); | |
} else { | |
super.onBackPressed(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment