• 6
name

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191

Backtrace:

File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

name Punditsdkoslkdosdkoskdo

Android Toolbar moves up when keyboard appears

I'm creating a chat based UI screen where I have toolbar and recyclerview for chat messages, and reply msg layout.

Whenever edittext get focus It moves up the toolbar. Instead I would like to resize the recyclerview.

some of the stackoverflow answers suggest to place a empty scrollview below the toolbar, but It didn't work.

        <activity
            android:name=".PostMessageActivity"
            android:label="@string/title_activity_post_message"
            android:windowSoftInputMode="stateVisible|adjustResize"
            >
        </activity>

I am setting the windowSoftInputMode to stateVisible|adjustPan in the manifest file.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.pasonet.yokibu.PostMessageActivity"
>
<include
    android:id="@+id/toolbar_home"
    layout="@layout/toolbar_home"
    android:elevation="5dp"
    />

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">
</ScrollView>


<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/toolbar_home"
    android:orientation="vertical"
    android:layout_above="@+id/add_post_layout"
    >
    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/post_msg_recyclerview"
        >
    </android.support.v7.widget.RecyclerView>


</LinearLayout>

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:padding="5dp"
    android:id="@+id/add_post_layout"
    android:background="#ffffff"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:elevation="5dp"
    android:layout_margin="0pt"
   >

    <EditText
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:id="@+id/messageText"
        android:layout_gravity="bottom"
        android:layout_weight="1"
        android:maxLines="4"
        android:scrollbars="vertical"
        android:background="@color/trasnperant"
        android:hint="Type your message"
        android:layout_marginBottom="4dp"
        />
    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_send_black_36dp"
        android:id="@+id/sendButton"
        android:background="@drawable/abc_btn_default_mtrl_shape"
        android:onClick="addPost"
        />
</LinearLayout>

enter image description here

The problem was that I was using <item name="android:windowTranslucentStatus">true</item> in styles.xml

To enable adjustResize add android:fitsSystemWindows="true" in your activity's parent layout

  • 19
Reply Report
      • 1
    • does not work for me, In general when you have a bg inside this screen is hard to manage, just adjustPan not rezize your background. With Background and status bar fixed , this does not work. Im searching here to fix this , but im not found anything yet.
      • 2
    • After adding this code my screen is adjusted perfectly, but i'm getting a white patch i.e something like margin bottom in my toobar.. How to avoid that.?

If you don't want your Toolbar to be pushed up, you shouldn't be using adjustPan. Using adjustResize without any other things (without adding any additional Views) should be enough.

  • 10
Reply Report
      • 1
    • Thanks for your time. I've tried the adjustResize also it didn't work. Am I missing something else?
    • Therefore there must be some issue with your layout. And to be honest I don't really get the layout file you provided. Why is your RecyclerView wrapped inside a LinearLayout? Also, since you've decided to use a RelativeLayout as a root, try setting android:layout_alignParentTop="true for your Toolbar.

In my case, my default application was using Coordinator Layout for the root element. Even doing what was described above wasn't solving my problem. Then I did the following:

  1. Changed the root layout from Coordinator Layout to RelativeLayout
  2. Included android:fitsSystemWindows="true" to the root layout
  3. Included android:windowSoftInputMode="adjustResize|stateAlwaysHidden" to the activity manifest tag.
  • 8
Reply Report

enter image description here

Here is the solution for this fix

  • android:windowSoftInputMode="adjustResize" in Manifest File
  • Make a class "CommentKeyBoardFix.Java" and copy and paste the below code.

    public class CommentKeyBoardFix
    {
    private View mChildOfContent;
    private int usableHeightPrevious;
    private FrameLayout.LayoutParams frameLayoutParams;
    private Rect contentAreaOfWindowBounds = new Rect();
    
    public CommentKeyBoardFix(Activity activity)
    {
        FrameLayout content = activity.findViewById(android.R.id.content);
        mChildOfContent = content.getChildAt(0);
        mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener(this::possiblyResizeChildOfContent);
        frameLayoutParams = (FrameLayout.LayoutParams) mChildOfContent.getLayoutParams();
    }
    
    private void possiblyResizeChildOfContent()
    {
        int usableHeightNow = computeUsableHeight();
        if (usableHeightNow != usableHeightPrevious)
        {
            int heightDifference = 0;
            if (heightDifference > (usableHeightNow /4))
            {
                // keyboard probably just became visible
                frameLayoutParams.height = usableHeightNow - heightDifference;
            }
            else
            {
                // keyboard probably just became hidden
                frameLayoutParams.height = usableHeightNow;
            }
            mChildOfContent.layout(contentAreaOfWindowBounds.left, contentAreaOfWindowBounds.top, contentAreaOfWindowBounds.right, contentAreaOfWindowBounds.bottom);
            mChildOfContent.requestLayout();
            usableHeightPrevious = usableHeightNow;
        }
    }
    
    private int computeUsableHeight()
    {
        mChildOfContent.getWindowVisibleDisplayFrame(contentAreaOfWindowBounds);
        return contentAreaOfWindowBounds.height();
    }
    }
    

    And then call this class in your Activity or Fragment; for Kotlin:

    setContentView(R.layout.your_comments_layout)
    CommentKeyBoardFix(this) 
    

    or for Java:

    new CommentKeyBoardFix(this)
    
  • 3
Reply Report

I tried many ways to keep the toolbar fixed , while not resizing or compressing the layout by android:windowSoftInputMode="adjustResize" , that is not a proper .

That is only possible by Coordinator layout ,

<android.support.design.widget.CoordinatorLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.design.widget.CollapsingToolbarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.v7.widget.Toolbar 
           xmlns:android="http://schemas.android.com/apk/res/android"
           xmlns:ripple="http://schemas.android.com/apk/res-auto"
           android:id="@+id/tv_toolbar"
           android:layout_width="match_parent"
           android:layout_height="50dp"
           android:background="#fff"
           ripple:contentInsetStart="0dp">
        </android.support.v7.widget.Toolbar>

    </android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>

<ScrollView
    android:id="@+id/coordinate_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:marginTop="56dp">
</ScrollView>

Keep the structure as

 Coordinator layout
  |-->Toolbar    (android:id="@+id/toolbar")
  |-->ScrollView (android:layout_below="@id/toolbar")
   |-->Child
   |-->Child
   |-->Child
  • 0
Reply Report

If NOTHING work, be aware that libraries can manipulate soft input, for example com.arlib.floatingsearchview.

enter image description here

What happen is, for example you have list view with search view, when you show the fragment with floatingsearchview its set soft input to SOFT_INPUT_ADJUST_PAN(you may not care about it in fragment with list view), but if the next fragment will be item details with many edit texts it will push the toolbar up.

In my case I cannot replace this library so i set SOFT_INPUT_ADJUST_RESIZE in the base fragment

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
}
  • 0
Reply Report

I use a CoordinatorLayout as root Layout for the toolbar. I had the same problem but solved it by setting the toolbar to:

app:layout_scrollFlags="enterAlways" 

instead of:

app:layout_scrollFlags="scroll|enterAlways"

and adding:

android:windowSoftInputMode="adjustNothing"

in the activity in the manifest.

  • -1
Reply Report