0

Keyboard pushes the view up

Whenever the keyboard is activated, the three buttons OPEN, SAVE, CLEAR (Shown in the above screenshot) go up. Is there any way to prevent them from going up?

Here's my activity_main.xml codes:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:fitsSystemWindows="true"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="p32929.cgpacalculator.Activities.MainActivity">

    <TextView
        android:layout_width="match_parent"

        android:layout_height="wrap_content"
        android:text="@string/input"
        android:textAlignment="center"
        android:textSize="15sp"
        android:textStyle="normal|bold" />


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:weightSum="100">

        <EditText
            android:id="@+id/mainCredit"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="50"
            android:hint="Credit"
            android:inputType="numberDecimal"
            android:maxLines="1"
            android:nextFocusDown="@+id/mainGpa"
            android:textAlignment="center"
            android:textColor="#000000"
            android:textColorHint="@color/hint" />

        <EditText
            android:id="@+id/mainGpa"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="50"
            android:hint="GPA"
            android:imeOptions="actionDone"
            android:inputType="numberDecimal"
            android:textAlignment="center"
            android:textColor="#000000"
            android:textColorHint="@color/hint" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:paddingBottom="7dp"
        android:weightSum="10">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="3"
            android:gravity="center"
            android:text="CGPA = "
            android:textAlignment="center"
            android:textColor="#000000"
            android:textSize="24sp" />

        <TextView
            android:id="@+id/mainCGPA"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="4"
            android:gravity="center"
            android:text="0.00"
            android:textAlignment="center"
            android:textColor="#000000"
            android:textSize="24sp" />

        <Button
            android:id="@+id/MainAdd"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="3"
            android:gravity="center"
            android:onClick="Clicked"
            android:text="Add" />

    </LinearLayout>

    <TextView
        android:id="@+id/infotext"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAlignment="center"
        android:textColor="#5c5c5c"
        android:textStyle="normal|bold" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="3dp"
        android:layout_marginLeft="3dp"
        android:layout_marginRight="3dp"
        android:layout_marginTop="3dp"
        android:weightSum="100">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="25"
            android:text="Serial"
            android:textAlignment="center"
            android:textColor="@color/text"
            android:textSize="14sp"
            android:textStyle="normal|bold" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="5dp"
            android:layout_weight="25"
            android:text="Credit(s)"
            android:textAlignment="center"
            android:textColor="@color/text"
            android:textStyle="normal|bold" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="13dp"
            android:layout_weight="25"
            android:text="GPA"
            android:textAlignment="center"
            android:textColor="@color/text"
            android:textStyle="normal|bold" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="5dp"
            android:layout_weight="25"
            android:text="Total"
            android:textAlignment="center"
            android:textColor="@color/text"
            android:textStyle="normal|bold" />
    </LinearLayout>

    <ListView
        android:id="@+id/listview"
        android:layout_width="match_parent"
        android:layout_height="388dp"
        android:layout_weight="3.58"
        android:choiceMode="multipleChoice"
        android:paddingTop="3dp">


    </ListView>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:weightSum="9">

        <Button
            android:id="@+id/openData"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="3"
            android:onClick="openData"
            android:text="Open" />

        <Button
            android:id="@+id/saveData"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="3"
            android:onClick="saveData"
            android:text="Save" />

        <Button
            android:id="@+id/clearData"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="3"
            android:onClick="clearData"
            android:text="Clear" />

    </LinearLayout>


</LinearLayout>

I've tried all the solutions given here: Android: How do I prevent the soft keyboard from pushing my view up?

But it's still the same.

Community
  • 1
  • 1
p32929
  • 99
  • 2
  • 11

2 Answers2

1

remove android:fitsSystemWindows="true" from your xml or change it to 'false'

Nilesh Awari
  • 104
  • 4
  • @p32929 From the layout xml parent element. For you, it is the ```LinerarLayout``` with ```android:id="@+id/activity_main"``` – Ifta Nov 29 '19 at 06:34
0

This questions has been asked a lot and there are many answers but..

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).

quoting @Gabe Sechan from the latest bounty question on the related subject


But still sometimes I use below method as an alternative.

In your OnCreate initialize your rootView(parent view in your XML) and your footerView

call this method from your OnCreate- once the keyboard is up you can change visibility of views that you don't need to show! When keyboard is down display them again.

 public void testKeyBoardUp(){
        rootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                Rect r = new Rect();
                rootView.getWindowVisibleDisplayFrame(r);
                int heightDiff = rel.getRootView().getHeight() - (r.bottom - r.top);

                if (heightDiff > 100) { // if more than 100 pixels, its probably a keyboard...
                    //ok now we know the keyboard is up...
                    whatEverView.setVisibility(View.INVISIBLE);


                }else{
                    //ok now we know the keyboard is down...
                    whatEverView.setVisibility(View.VISIBLE);


                }
            }
        });
    }
Community
  • 1
  • 1
Charuක
  • 12,953
  • 5
  • 50
  • 88