Make fragments full screen programmatically - android

I have a ViewPager and FragmentPagerAdapter set up in one activity with 3 fragments. All fragments are loaded simultaneously and are kept in memory using
mPager.setOffscreenPageLimit(2);
One of these fragments hosts a camera preview that I would like to make full screen, with no status bar or action bar. The other fragments require that the action bar be displayed.
How would I make only the camera preview fragment full screen while keeping the other two fragments normal? I have tried using themes, which means breaking the action bar.
Programmatically calling the following doesn't help, because it forces the whole activity to be full screen (and thus the other two fragments along with it):
requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);
I also tried implementing ViewPager.OnPageChangeListener:
public void onPageSelected(int position) {
if(position == 1)
getActionBar().hide();
else
getActionBar().show();
}
This does hide the action bar, but it doesn't hide the system status bar (I'm talking about where the notifications, status icons, and time are), and the action bar hide/show animation is also very choppy when swiping.
For an example of what I want to do exactly, see Snapchat - they manage to pull off the swiping between camera and other fragments perfectly.
Edit:
So I changed my onPageSelected to this:
public void onPageSelected(int position) {
if(position == 1) {
MainActivity.this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
MainActivity.this.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
} else {
MainActivity.this.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
MainActivity.this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
}
}
This enables me to change whether the status bar is displayed or not. However, it causes a large amount of jank. Same problem with the action bar.
I notice that in Snapchat the status bar slides up and down after the tab change is complete. I'm not sure how to implement this, so any advice regarding this aspect would be appreciated.

I figured out how to do this, though it's definitely a hack.
The trick is not to use the Android action bar, it's to make your own in your layout files.
I defined two more RelativeLayouts in my code:
<RelativeLayout
android:id="#+id/padding"
android:layout_width="fill_parent"
android:layout_height="25dip"
android:layout_alignParentTop="true"
android:background="#color/abs__background_holo_dark" />
<RelativeLayout
android:id="#+id/fake_action_bar"
android:layout_width="fill_parent"
android:layout_height="48.0dip"
android:layout_below="#id/padding" >
<!-- stuff here -->
</RelativeLayout>
The top RelativeLayout is padding for the Android status bar. I set it to 25dip, but it may vary - it would probably be best to set this value programmatically. Also you need to set it to a black color so that it matches the status bar color and makes the UI look more natural.
The bottom RelativeLayout is for your fake action bar. You need to stuff everything your action bar needs (title text, buttons, etc.) in this RelativeLayout. I set it to 48dip, but again, this will vary and you should probably set it programmatically.
In the Activity hosting your Fragments, ViewPager and FragmentPagerAdapter, you need to set the flags telling Android to make the status bar an overlay:
WindowManager.LayoutParams params = getWindow().getAttributes();
params.flags |= WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS;
Now we need to make the status bar hide and show when we switch fragments. Have your activity implement ViewPager.OnPageChangeListener and then use the following code:
#Override
public void onPageScrollStateChanged(int state) {
switch(state) {
case ViewPager.SCROLL_STATE_IDLE:
if(mPosition == 1) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
} else {
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
}
}
}
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
mPosition = position;
}
We keep track of the page number using onPageSelected and use it in onPageScrollStateChanged to dynamically hide/show the status bar depending on which Fragment is currently being displayed. You can't do this in onPageSelected because this will cause the status bar to redisplay halfway through a swipe.
You also need to make sure to add appropriate code for your fake action bar - for buttons, this would be OnClickListener.

First, in the activity/fragment you want to hide the status-bar:
Make a public static of the fragment.
public static Fragment QSFrag;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
QSFrag = this;
}
Then in your onPageSelected:
public void onPageSelected(int position) {
// TODO Auto-generated method stub
if(position == 1){
mActionBar.hide();
YourActivityWithNoStatusOrActionBar.QSFrag.getActivity().getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
}else{
mActionBar.show();
// mActionBar.setSelectedNavigationItem(position); I use this, not sure u need/want it.
}
}
I've just tested it, and it works. But it doesn't look awesome when you leave the fragment with no status-bar. :p

You can use this code in onCreate:
setTheme(android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
if (getSupportActionBar() != null) {
getSupportActionBar().hide();
}

Related

How to disable all clickable items in AppBarLayout?

I'm trying to disable all clickable items in the app bar layout when the opaque background appears after clicking the floating action button. But I also need to make sure that all the floating action buttons are all still clickable. I'm thinking maybe I can disable all items in the app bar programmatically?
How to achieve this?
UPDATE
Code to set fab visibility and animation. When the Fabs displayed, the tabs and toolbar finally disabled and unclickable. But i want to make my Fabs still clickable. How can i do this? Please advice. Thank you!
public void fabVisibility(){
if (isOpen){
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
fabActivity.startAnimation(fabClose);
textViewActivities.startAnimation(fabClose);
fabPost.startAnimation(fabClose);
textViewPosts.startAnimation(fabClose);
fabMedia.startAnimation(fabClose);
textViewMedia.startAnimation(fabClose);
fabPlus.startAnimation(fabRotateAntiClockwise);
fabActivity.setClickable(false);
fabPost.setClickable(false);
fabMedia.setClickable(false);
shadowView.setVisibility(View.INVISIBLE);
isOpen = false;
}else {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE, WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
fabActivity.startAnimation(fabOpen);
textViewActivities.startAnimation(fabOpen);
fabPost.startAnimation(fabOpen);
textViewPosts.startAnimation(fabOpen);
fabMedia.startAnimation(fabOpen);
textViewMedia.startAnimation(fabOpen);
fabPlus.startAnimation(fabRotateClockwise);
fabPlus.setEnabled(true);
fabActivity.setClickable(true);
fabActivity.setEnabled(true);
fabPost.setClickable(true);
fabPost.setEnabled(true);
fabMedia.setClickable(true);
fabMedia.setEnabled(true);
shadowView.setVisibility(View.VISIBLE);
isOpen = true;
}
}
To disable the user interaction you just need to add the following code
getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
To get user interaction back you just need to add the following code
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
To disable action bar (app bar) buttons on clicking FAB icon, you could set a flag, let's say DisableAppBarButton.
Now call invalidateOptionsMenu() which will trigger onCreateOptionsMenu and will regenerate your menu.
Modify your onCreateOptionsMenu to disable the buttons.
if (DisableAppBarButton) {
menu.someItem(R.id.yourItem).setEnabled(false);
} else {
menu.someItem(R.id.yourItem).setEnabled(true);
}
Two options:
1 - You can set a view group to be disabled by recursively setting all of its children disabled. Example:
public static setEnabled(View view, boolean enabled) {
view.setEnabled(enabled);
if (view instanceof ViewGroup) {
ViewGroup viewGroup = (ViewGroup) view;
for (int i = 0; i < viewGroup.getChildCount(); i++) {
setEnabled(viewGroup.getChild(i), enabled);
}
}
}
Then in your code in response to the FAB showing or hiding:
ViewUtil.setEnabled(mAppBarLayout, true /* or false */);
2 - You can make the opaque background focusable and clickable to intercept clicks while it's overlayed over the app bar, while the FAB buttons that float above that background would still receive clicks.
Hope that helps!

Status bar background stays on screen when entering immersive mode?

I've been searching everywhere but I'm at a loss about that : trying to activate immersive mode on a project;
Nearly everything works fine, except the background of my status bar always stays there, spoiling the immersion...
I have included a screenshot of the screen before and after activating the immersive mode, and set the "colorPrimaryDark" to full green for max contrast :
screenshots showing the background of the status bar when nothing should be there
The code I used and reinserted in a blank project to isolate this problem comes straight from the google dev examples, in my MainActivity, I have :
private final String TAG = "DEBUG::" + this.getClass().getSimpleName();
private final int INITIAL_HIDE_DELAY = 1500;
private View decorView;
private Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//setting needed decorView for fullscreen behavior
decorView = getWindow().getDecorView();
decorView.setOnSystemUiVisibilityChangeListener(onSystemUiVisibilityChangeListener);
}
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
Log.i(TAG, "onWindowFocusChanged::hasFocus = " + hasFocus);
if (hasFocus) {// When the window gains focus, hide the system UI.
delayedHide(INITIAL_HIDE_DELAY);
} else {// When the window loses focus, cancel any pending hide action.
mHideHandler.removeMessages(0);
}
}
private void hideSystemUI() {
Log.i(TAG, "hideSystemUI");
int uiOptions = View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
uiOptions |= View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION;
uiOptions |= View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
uiOptions |= View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
uiOptions |= View.SYSTEM_UI_FLAG_FULLSCREEN;
uiOptions |= View.SYSTEM_UI_FLAG_LOW_PROFILE;
uiOptions |= View.SYSTEM_UI_FLAG_IMMERSIVE;
decorView.setSystemUiVisibility(uiOptions);
}
private final Handler mHideHandler = new Handler() {
#Override
public void handleMessage(Message msg) {
hideSystemUI();
}
};
private void delayedHide(int delayMillis) {
Log.i(TAG, "delayedHide");
mHideHandler.removeMessages(0);
mHideHandler.sendEmptyMessageDelayed(0, delayMillis);
}
private View.OnSystemUiVisibilityChangeListener onSystemUiVisibilityChangeListener =
new View.OnSystemUiVisibilityChangeListener() {
#Override
public void onSystemUiVisibilityChange(int visibility) {
if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
// The system bars are visible
getSupportActionBar().show();
delayedHide(INITIAL_HIDE_DELAY);
} else {
// The system bars are NOT visible
getSupportActionBar().hide();
}
}
};
I wonder if my problem might come from layout or style files, but those are raw from project generation...
I hope someone out there can point me to where I failed!
Thanks in advance!
EDIT : I found that removing : android:fitsSystemWindows="true" from my activity's layout file allows a real fullscreen mode, but then, my ActionBar is partly hidden behind the StatusBar -when showing. Could it be that when I set my getSupportActionBar().show(); in onSystemUiVisibilityChangeListener, it gets drawn too soon?
EDIT 2 : How I understand this so far is that I only have 2 choices regarding the position/size of my content (action bar included) :
top of the screen, which will show the actionBar partially hidden by the statusbar,
or below the statusBar's bottom, which will leave me with a "hole" when the statusBar is hidden -_-
I am now looking for a solution to animate the ActionBar off-screen/on-screen by myself inside my onSystemUiVisibilityChangeListener method, but can't find a way to grab its View to do so, solutions posted there https://stackoverflow.com/a/21125631/6463888 seem out of date...
I met the same problem today. But I didn't solve this issue by setSystemUiVisibility. I solve it using following method:
hide:enter full screen
mActivity.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
show:normal display
mActivity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
So,
the way I solved this may seem a bit stretched, but I'm only a beginner, so feel free to comment!
I don't use getSupportActionBar().show(); or getSupportActionBar().hide(); anymore, but I managed to grab the View that contains the ActionBar which is an AppBarLayout, and I animated this View instead. So I call a small function animateActionBarInOrOut to animate it on or off screen inside onSystemUiVisibilityChange :
private void animateActionBarInOrOut(boolean appears){
Log.i(TAG, "animateActionBarInOrOut::actual position = " + toolbar.getY());
if(appears){
toolbar.animate().translationY(48).alpha(1); // move it out of the screen
}else{
toolbar.animate().translationY(-48).alpha(0); // move it out of the screen
}
}
Although this is not exactly an answer to the initial question, it works as a solution to the problem, one just has to move the content accordingly...
Hi your problem is caused by StatusBar nature, it has a separate layout from your main content and you need to set his color manually. For example when you call onSystemUiVisibilityChange() you can take the color of your background and, after set the color of StatusBar with that color. This is a workaround to avoid 2 different background colors.

Toolbar overlay hidden under system UI in YouTube player fullscreen

I'm using a YouTubePlayerFragment in my activity, and attempting to overlay the player with an app bar (aka action bar) when the player is in fullscreen. I'm following the guidelines and example in the YouTube Player API "Overlay ActionBar Demo" sample application and YouTubePlayerFragment documentation (more detail below).
All of this worked fine when I was extending from Activity and using the core ActionBar. But when I switch to using AppCompatActivity with the support Toolbar, a few issues arise:
The Toolbar is laid out under the status bar and navigation bar
The player no longer plays when the Toolbar is on top of it
It seems as though the player fullscreen mode used to treat the action bar as part of the system UI (along with the status bar and navigation bar), in terms of positioning and overlay, but no longer does so with the Toolbar.
Any thoughts on why this is happening or how I can use the Toolbar to properly overlay the YouTube player in fullscreen mode? I realize the Toolbar is just another view and I could probably force it to resize and reposition under the status bar, and I could set up a listener for system UI changes and show and hide my Toolbar accordingly, but I'm hoping there's a cleaner fix that I'm missing.
Here's a screenshot:
More detail: I was able to reproduce this behavior in the "Overlay ActionBar Demo" sample app (ActionBarDemoActivity), with the following changes:
Extend AppCompatActivity
Change import from android.app.ActionBar to android.support.v7.app.ActionBar
Add Toolbar to layout
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<view
class="com.examples.youtubeapidemo.ActionBarDemoActivity$ActionBarPaddedFrameLayout"
android:id="#+id/view_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
...
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="#color/material_deep_teal_500" />
</FrameLayout>
Change the theme to Theme.AppCompat.Light.NoActionBar and add windowActionBarOverlay
<style name="OverlayActionBarTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowActionBarOverlay">true</item>
<item name="windowActionBarOverlay">true</item>
</style>
Set the Toolbar as the action bar in onCreate()
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Note that the sample app also does the following (I didn't change these):
Implement YouTubePlayer.OnFullscreenListener
Extend the layout and set padding when not in fullscreen so that content displays below Toolbar
#Override
public void onFullscreen(boolean fullscreen) {
viewContainer.setEnablePadding(!fullscreen);
...
}
public static final class ActionBarPaddedFrameLayout extends FrameLayout {
public void setEnablePadding(boolean enable) {
paddingEnabled = enable;
requestLayout();
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int topPadding =
paddingEnabled && actionBar != null && actionBar.isShowing() ? actionBar.getHeight() : 0;
setPadding(0, topPadding, 0, 0);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
Set YouTube player fullscreen control flag
player.addFullscreenControlFlag(YouTubePlayer.FULLSCREEN_FLAG_CUSTOM_LAYOUT);
Handle config changes
<activity
...
android:configChanges="keyboardHidden|orientation|screenSize"
...
</activity>
I suspect this is a bug. I filed a new bug report for the layout problem, and added a comment to an existing report about the support ActionBar/Toolbar being treated as an illegal overlay.
Toolbar overlay hidden under system UI in YouTube player fullscreen
With AppCompat theme on older devices, ActionBar is detected as an illegal overlay
In the meantime, as a workaround I'm doing the following:
Layout issue
As Troy suggested, I added android:fitsSystemWindows="true" to the Toolbar XML. This fixed the layout in fullscreen, but caused padding to be added to the Toolbar when leaving fullscreen (screenshot). I added logic to the onFullscreen(boolean) method to remove the Toolbar padding (toolbar.setPadding(0, 0, 0, 0)) but this unfortunately still shows white space in place of the padding, which disappears when the layout is next redrawn (e.g., when clicking an item, starting the player, etc). I'm still a little stumped on how to properly set the layout/padding, but the current workaround will do for now.
Illegal overlay issue
Given that showing the Toolbar will pause the YouTube player in fullscreen, I've added logic to only show the Toolbar when the player is already paused. This isn't ideal, as it forces the user to pause the player to see the Toolbar, but it's the best option I can think of.
In onFullscreen(boolean), if we enter fullscreen and the player is playing, the Toolbar is hidden.
public void onFullscreen(boolean isFullscreen) {
mFullscreen = isFullscreen;
if (isFullscreen && youTubePlayer.isPlaying()) {
toolbar.setVisibility(View.GONE);
}
else {
toolbar.setVisibility(View.VISIBLE);
}
}
When setting up the player (in onInitializationSuccess()), I added a listener to show and hide the Toolbar on play and pause:
youTubePlayer.setPlaybackEventListener(new YouTubePlayer.PlaybackEventListener() {
...
#Override
public void onPlaying() {
if (mFullscreen) {
toolbar.setVisibility(View.GONE);
}
}
#Override
public void onPaused() {
if (mFullscreen) {
toolbar.setVisibility(View.VISIBLE);
}
}
});
This code will solved your issue.
To hide navigation bar and toolbar
public void fullScreenCall() {
if(Build.VERSION.SDK_INT > 11 && Build.VERSION.SDK_INT < 19) { //lower api
View v = getActivity().getWindow().getDecorView();
v.setSystemUiVisibility(View.GONE);
} else if(Build.VERSION.SDK_INT >= 19) {
//for new api versions.
View decorView = getActivity().getWindow().getDecorView();
int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_FULLSCREEN;
decorView.setSystemUiVisibility(uiOptions);
}
}
to back normal mode
public void normalScreenCall() {
if(Build.VERSION.SDK_INT > 11 && Build.VERSION.SDK_INT < 19) { // lower api
View v = getActivity().getWindow().getDecorView();
v.setSystemUiVisibility(View.VISIBLE);
} else if(Build.VERSION.SDK_INT >= 19) {
//for new api versions.
View decorView = getActivity().getWindow().getDecorView();
decorView.setSystemUiVisibility(0);
}
}
Try adding android:fitsSystemWindows="true" to your toolbar in your XML file.

Put text in "TOP BAR" 2 lines + image

I'm trying put text in to bar, I only put (buttons), I don't know if I can put text 2 lines + image.
Here are references as changing the color:
https://developer.android.com/training/basics/actionbar/styling.html
in this other picture is displayed, as you can put more text in the top bar
http://www.whatsapp.com/img/v3/es/s1-chat.png
Simply create your own view and set it for your ActionBar: setCustomView().
You cannot achieve what you want with the standard layout. For plenty of examples look here:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar actionBar = getActionBar();
// add the custom view to the action bar
actionBar.setCustomView(R.layout.actionbar_view);
EditText search = (EditText) actionBar.getCustomView().findViewById(R.id.searchfield);
search.setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId,
KeyEvent event) {
Toast.makeText(MainActivity.this, "Search triggered",
Toast.LENGTH_LONG).show();
return false;
}
});
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM
| ActionBar.DISPLAY_SHOW_HOME);
}
}
Source: vogella.com
I don't think you can do it.
I'd advise you just to create a layout that would act like a top bar in your application.
This way you would be able to put there anything you want.
If you have more than one activity that needs to have this top bar, then use fragments instead of activities (to avoid your custom top bar from being animated when new activities are being opened).

Hide the navigation buttons causes a move in the surfaceView and bad response in rapid touch

I'm develop a video Streming app. I want to hide navigation bottons but SurfaceView position is translated from actual center to the new center.
I read about Using Immersive Full-Screen Mode but i want app work in android 4.0+. And Immersive is only for 4.4.
Furthermore, if i touch rapidly, hidding don't work fine. If I use Visibility.GONE versus Visibility.SYSTEM_UI_FLAG_HIDE_NAVIGATION hidding work fine (rapidly touch), but re-center problem persists.
Re-center movements problem occurs both with GONE and SYSTEM_UI_FLAG_HIDE_NAVIGATION.
SurfaceView is anchored to center:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#color/Black">
<SurfaceView
android:id="#+id/videoSurface"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
<LinearLayout android:orientation="horizontal"
android:id="#+id/channelList"
android:background="#2000"
android:paddingLeft="5.0dip" android:paddingTop="5.0dip" android:paddingBottom="5.0dip" android:layout_width="wrap_content" android:layout_height="80.0dip">
<!-- Inserted dynamically -->
</LinearLayout>
</RelativeLayout>
The Java Activity relevant Code:
public class VideoActivity extends Activity {
...
//Prepare Screen (FULLSCREEN)
requestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().setFormat(PixelFormat.TRANSLUCENT);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
...
mVideoSurfaceView = (SurfaceView) findViewById(R.id.videoSurface);
mVideoSurfaceView.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View paramAnonymousView, MotionEvent paramAnonymousMotionEvent) {
Log.d("UiVisibility", "onTouch");
VideoActivity.this.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
return false;
}
});
// Synchronize the other elements (from android docs)
getWindow().getDecorView().setOnSystemUiVisibilityChangeListener (new View.OnSystemUiVisibilityChangeListener() {
#Override
public void onSystemUiVisibilityChange(int visibility) {
Log.d("UiVisibility", "onSystemUiVisibilityChange - bool " + String.valueOf((visibility & View.GONE) == 0));
// 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_HIDE_NAVIGATION) == 0) {
// TODO: The system bars are visible. Make any desired
// adjustments to your UI, such as showing the action bar or
// other navigational controls.
VideoActivity.this.findViewById(R.id.channelScroll).setVisibility(View.VISIBLE);
getActionBar().show();
} 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.
VideoActivity.this.findViewById(R.id.channelScroll).setVisibility(View.GONE);
getActionBar().hide();
}
}
});
}
I have solved the issue:
In Activity I define:
private static int LAYOUT_FLAGS= View.SYSTEM_UI_FLAG_LAYOUT_STABLE
|View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
|View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION;
private static int HIDE_FLAGS=LAYOUT_FLAGS |View.SYSTEM_UI_FLAG_FULLSCREEN
|View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
LAYOUT_FLAGS sets the layout as if the status bar and navigation bar are hidden. And makes the inner layout will not move when the bars are hidden or shown.
HIDE_FLAGS Makes effective the hiding.
In onCreate()
...
//Prepare Screen (FULLSCREEN)
requestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().setFormat(PixelFormat.TRANSLUCENT);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
...
//onClick - Hide System UI (Only for hidden, show events are handler by the system)
mVideoSurfaceView = (SurfaceView) findViewById(R.id.videoSurface);
mVideoSurfaceView.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.d("UiVisibility", "onClick");
hideUI();
}
});
//Synchronize my bars with System UI hidding
getWindow().getDecorView().setOnSystemUiVisibilityChangeListener (new View.OnSystemUiVisibilityChangeListener() {
#Override
public void onSystemUiVisibilityChange(int visibility) {
Log.d("UiVisibility", "onSystemUiVisibilityChange - bool " + String.valueOf((visibility & View.SYSTEM_UI_FLAG_HIDE_NAVIGATION) == 0));
// 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_HIDE_NAVIGATION) == 0) {
// The system bars are visible. Make any desired
// adjustments to your UI
VideoActivity.this.findViewById(R.id.channelScroll).setVisibility(View.VISIBLE);
//getActionBar().show();
mIsListChannelVisible=true;
} else {
// The system bars are NOT visible. Make any desired
// adjustments to your UI.
VideoActivity.this.findViewById(R.id.channelScroll).setVisibility(View.GONE);
//getActionBar().hide();
mIsListChannelVisible=false;
}
}
});
Show and hide methods:
// Hide need a delay to wait nav bar transition is completed. (fix: too fast click fails)
private void hideUI(){
mVideoSurfaceView.getHandler().postDelayed(new Runnable(){
#Override
public void run() {
VideoActivity.this.getWindow().getDecorView().setSystemUiVisibility(HIDE_FLAGS);
}
},600);
}
private void showUI(){
VideoActivity.this.getWindow().getDecorView().setSystemUiVisibility(0);
}
My custom bar is covered by the navigation bar, to fix it:
In myLayout.xml set fitSystemWindows=true
<HorizontalScrollView
android:fitsSystemWindows="true"
android:id="#+id/channelScroll"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true" >
It works perfect.

Categories

Resources