Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Push up content except some view when keyboard shown

I have a layout that contains 5 EditText and a Button and a TextView at bottom. Now when I press an EditText then the keyboard will shown and all my View is push up.

Now I don't want to push my TextView and Button to above keyboard, just only want to push up all EditText inside ScrollView to above keyboard.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="#ff0"
        >

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

            <EditText
                android:layout_marginTop="30dp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="EditText 1"

                />

            <EditText
                android:layout_marginTop="30dp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="EditText 2"
                />

            <EditText
                android:layout_marginTop="30dp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="EditText 3"
                />

            <EditText
                android:layout_marginTop="30dp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="EditText 4"
                />

            <EditText
                android:layout_marginTop="30dp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="EditText 5"
                android:inputType="textNoSuggestions"
                />

        </LinearLayout>

    </ScrollView>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:text="Button"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#000"
        android:text="I don't want to push this TextView and Button to above keyboard when keyboard is shown. Just obly want to push the ScrollView that contain all EditText"
        />
</LinearLayout>

enter image description here

I have an idea is. When I will listener when keyboard show and hide. When keyboard show I will set the bottom margin of ScrollView = keyboard height, when keyboard hide I will set this margin = 0.
Is there any way easier to handle my case? Any help or suggestion would be great appreciated.

UPDATE
If I use windowSoftInputMode=adjustPan => not all EditText is push up to above keyboard
If I use windowSoftInputMode=adjustResize => Button, TextView and all EditText is push up to above keyboard

like image 614
Linh Avatar asked Jan 11 '17 04:01

Linh


People also ask

How do I prevent the soft keyboard from pushing my view up in fragment?

Setting android:isScrollContainer = "false" inside the ScrollView worked for me. According to the documentation, settings "isScrollContainer" to true means that the scroll view can be resized to shrink its overall window so that there is space for an input method.

How do you move the layout up when the soft keyboard is shown Android?

Sometimes, you need to change the layout when the soft keyboard appeared on the screen. You can fix this by adding a line of code into the AndroidManifest. xml file within the relevant activity section.


2 Answers

Won't work- there is no reliable API to detect when the keyboard is shown. You'll see "solutions" on this site, but they have false positives and negatives. But it seems like the best thing for you is to set the softInputMode to adjustPan. This will make the OS scroll the entire screen by the minimum amount needed to make the cursor visible above the keyboard. (the entire app going above the keyboard is due to the mode adjustResize).

like image 120
Gabe Sechan Avatar answered Oct 23 '22 22:10

Gabe Sechan


  1. Tested on all Nexus mobile and Tablet devices. It works fine. I am using your layout xml code and have added only id's to views.

  2. I am using the below library in my production application and it is very promising. To achieve the same and add the below line in app gradle: 'net.yslibrary.keyboardvisibilityevent:keyboardvisibilityevent:2.0.1' Here is the link for the same.

  3. Refer to the below gist for the rest of the code:Manifest code, Activity code, Activity layout code.

The above solution migh not work 20% of time as per my experience. For a few samsung devices, above code is not working as expected. If I have time will try to resolve it using Constraintlayout and adjustPan.

like image 7
Anurag Singh Avatar answered Oct 23 '22 21:10

Anurag Singh