Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

StatusBar shows when keyboard shows up

My activity use CollapsingToolbarLayout and i remove successfully the StatusBar doing this:

View decorView = getWindow().getDecorView();
// Hide the status bar.
int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
decorView.setSystemUiVisibility(uiOptions);

The problem is that when the softkeyboard pops up, the StatusBar appear again, consuming precious space.

How to keep it hide permanently on the ActivityView?

If i set it to FullScreen, it works but i loose the SOFT_INPUT_ADJUST_RESIZE parameter. The main window scrolls when softkeyboard appears, instead of resize as needed.

Update:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:fitsSystemWindows="true"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/my_appar"
        android:fitsSystemWindows="false"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:background="@color/white">

        <android.support.design.widget.CollapsingToolbarLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:toolbarId="@+id/collapsetoolbar"
            app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed"
            app:layout_scrollInterpolator="@android:anim/decelerate_interpolator"
            app:contentScrim="?attr/colorPrimary">

            <android.support.v7.widget.Toolbar
                android:id="@+id/my_toolbar"
                android:elevation="4dp"
                android:layout_height="wrap_content"
                android:layout_width="match_parent">
            </android.support.v7.widget.Toolbar>

        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>
    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/activity_id"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="... .MyActivity"
        android:orientation="vertical"
        >

        <FrameLayout
            android:id="@+id/yt_frame"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <fragment
            ...
        </FrameLayout>

        <com.byte_artisan.mchat.compoundViews.chatview.ChatView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@id/fragment1"
            android:id="@+id/chat_view_id">
        </com.byte_artisan.mchat.compoundViews.chatview.ChatView>
    </RelativeLayout>
</android.support.design.widget.CoordinatorLayout>

The code:

 protected void onCreate(Bundle savedInstanceState) {        
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_mchat);

     this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE );
..
}


@Override
protected void onResume() {
    super.onResume();

    View decorView = getWindow().getDecorView();
    // Hide the status bar.
    int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
    decorView.setSystemUiVisibility(uiOptions);
}

The activity is using NoActionBar scheme(declared on the manifest).

like image 504
MiguelSlv Avatar asked Jan 12 '17 11:01

MiguelSlv


2 Answers

Add this line in your styles activity style

<item name="android:windowFullscreen">true</item>

Unless you wrap your activity content with ScrollView you cant use SOFT_INPUT_ADJUST_RESIZE in a full screen activity

For that to work you need to wrap your layout content inside ScrollView

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <!-- everything you already have -->

</LinearLayout>

tell me if it works..

like image 77
Jayanth Avatar answered Oct 21 '22 22:10

Jayanth


Points to be noted :-

1) Once UI flags have been cleared (for example, by navigating away from the activity), your app needs to reset them if you want to hide the bars again. See Responding to UI Visibility Changes for a discussion of how to listen for UI visibility changes so that your app can respond accordingly.

2) Where you set the UI flags makes a difference. If you hide the system bars in your activity's onCreate() method and the user presses Home, the system bars will reappear. When the user reopens the activity, onCreate() won't get called, so the system bars will remain visible. If you want system UI changes to persist as the user navigates in and out of your activity, set UI flags in onResume() or onWindowFocusChanged().

3) The method setSystemUiVisibility() only has an effect if the view you call it from is visible.

4) Navigating away from the view causes flags set with setSystemUiVisibility() to be cleared.

You should add this code on create

View decorView = 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.
// again hide it
        } 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.
        }
    }
});
like image 26
Quick learner Avatar answered Oct 21 '22 22:10

Quick learner