Tyson
Tyson

Reputation: 747

Resize views on Soft Keyboard launch

I am trying to create a chat application. The chat layout currently looks like this

No Keyboard

The corresponding layout file is

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

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.nirmal.chatadapter.MainActivity">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/colorPrimary">
        <de.hdodenhof.circleimageview.CircleImageView
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:id="@+id/profile_image"
            android:layout_width="40dp"
            android:layout_height="wrap_content"
            android:src="@drawable/iconuser"
            app:civ_border_width="2dp"
            app:civ_border_color="@color/White"/>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginStart="@dimen/marginLeftForToolbar"
            android:orientation="vertical">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="@color/White"
                android:text="UserName Here"
                android:layout_marginTop="@dimen/marginTopBottomForBubble"
                android:id="@+id/user_name"
                android:textSize="@dimen/textMedium"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="@color/White"
                android:text="User details Here"
                android:layout_marginTop="@dimen/marginTopBottomForBubble"
                android:id="@+id/user_details"
                android:textSize="@dimen/textSmall"/>
        </LinearLayout>

    </android.support.v7.widget.Toolbar>

    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/toolbar"
        android:layout_marginTop="@dimen/recyclerViewMargin"
        android:isScrollContainer="true"
        android:id="@+id/chatRecycler"
        android:layout_above="@+id/chatBoxAndSend">
    </android.support.v7.widget.RecyclerView>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:orientation="horizontal"
        android:layout_marginBottom="5dp"
        android:id="@+id/chatBoxAndSend">
        <EditText
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:minWidth="50dp"
            android:maxWidth="50dp"
            android:id="@+id/chatBox"
            android:paddingStart="@dimen/cardViewPadding"
            android:background="@drawable/rounded_chat_box"
            android:layout_margin="@dimen/marginForChatBox"
            />
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.2"
            android:background="@drawable/oval_button"
            android:minWidth="50dp"
            android:maxWidth="50dp"
            android:text="Send"
            android:imeOptions="flagNoExtractUi"
            android:id="@+id/chatSendButton"
            android:layout_margin="@dimen/marginForChatBox"
            android:textColor="@color/Black"
            />
        <!--<ImageView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.1"
            android:id="@+id/send_button"
            android:src="@drawable/send"
            android:background="@drawable/oval_button"
            android:layout_marginBottom="@dimen/marginForChatBox"
            android:layout_marginTop="@dimen/marginForChatBox"
            android:layout_marginEnd="@dimen/marginForChatBox"
            />
        <de.hdodenhof.circleimageview.CircleImageView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.1"
            android:id="@+id/send_button"
            android:src="@drawable/send"
            app:civ_border_width="2dip"
            android:layout_marginEnd="@dimen/marginForChatBox"
            app:civ_border_color="@color/greenChatColor"/>-->


    </LinearLayout>
</RelativeLayout>
</ScrollView>

When I open my soft keyboard the layout looks likes this

with keyboard

I have added this in my manifest file

 android:windowSoftInputMode="adjustPan|stateHidden"

The output is not what I expected it to be. I want the recyclerview to dynamically resize in the center of the screen and the Toolbar and the edittext to stay fully visible.

As you can see the toolbar has been pushed up and is not visible even when scrolled. The edittext is hidden partially by the suggestions given by the keyboard. Some of the posts I saw asked me to add a scrollview which I did and still the same result. How can I solve this problem?

Upvotes: 0

Views: 510

Answers (2)

Aneh Thakur
Aneh Thakur

Reputation: 1100

If you want to resize RecyclerView on SoftInput open or close then you can use below single line code

((LinearLayoutManager)myRecyclerView.getLayoutManager()).setStackFromEnd(true);

For more read this post: https://trinitytuts.com/tips/resize-recyclerview-on-android-softinput-keyboard-appear/

Hope this code helps you

Upvotes: 1

Uriel Frankel
Uriel Frankel

Reputation: 14622

The flag adjustPan is wrong. Use the adjustResize instead:

 android:windowSoftInputMode="adjustResize|stateHidden"

What adjustPan does is equivalent to enlarging a picture more than the screen and then panning - i.e. moving the picture so you see different parts of it. (like a map in google map for instance).

Upvotes: 0

Related Questions