Change fields when a keyboard pops up

I got this code below, this is my main.xml

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

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@id/textView1"
        android:layout_alignParentLeft="true"
        android:layout_marginBottom="80dp"
        android:paddingTop="10dp"
        android:text="@string/parameter"
        android:textColor="@color/White"
        android:textSize="18sp" />

    <Spinner
        android:id="@+id/spinner1"
        style="@layout/spinner_layout"
        android:layout_width="120dp"
        android:layout_height="55dp"
        android:layout_alignLeft="@+id/spinner3"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="22dp"
        android:paddingLeft="5dp"
        tools:listitem="@android:layout/simple_list_item_1" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/spinner1"
        android:layout_alignBottom="@+id/spinner1"
        android:layout_alignLeft="@+id/output2"
        android:layout_alignParentRight="true"
        android:ems="10"
        android:inputType="numberDecimal" >

        <requestFocus />
    </EditText>

    <Spinner
        android:id="@+id/spinner3"
        android:layout_width="120dp"
        android:layout_height="55dp"
        android:layout_alignParentBottom="true"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="320dp"
        android:layout_toRightOf="@+id/textView7" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@id/textView7"
        android:layout_alignParentLeft="true"
        android:layout_marginBottom="80dp"
        android:text="@string/transmission"
        android:textColor="@color/White"
        android:textSize="18sp" />

    <EditText
        android:id="@+id/editText2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView1"
        android:layout_alignBottom="@+id/textView1"
        android:layout_alignLeft="@+id/EditText1"
        android:layout_alignRight="@+id/EditText1"
        android:ems="10"
        android:inputType="numberDecimal"
        android:text="70" />

    <EditText
        android:id="@+id/EditText1"
        android:layout_width="120dp"
        android:layout_height="55dp"
        android:layout_above="@id/editText2"
        android:layout_alignParentRight="true"
        android:layout_marginBottom="50dp"
        android:ems="10"
        android:hint="@string/uren"
        android:inputType="number|numberDecimal" />

    <TextView
        android:id="@+id/textView7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignTop="@+id/spinner3"
        android:text="@string/out"
        android:textColor="@color/White"
        android:textSize="18sp" />

    <TextView
        android:id="@+id/output2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView7"
        android:layout_alignBottom="@+id/textView7"
        android:layout_alignLeft="@+id/EditText1"
        android:layout_alignParentRight="true"
        android:background="@drawable/box"
        android:paddingTop="10dp"
        android:text="@string/textview"
        android:textSize="18sp" />

    <Spinner
        android:id="@+id/spinner2"
        style="@layout/spinner_layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@id/textView1"
        android:layout_marginBottom="80dp"
        android:layout_marginLeft="20dp"
        android:layout_toLeftOf="@+id/EditText1"
        android:layout_toRightOf="@id/textView2" />

    <TextView
        android:id="@+id/textView6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignTop="@+id/editText1"
        android:text="@string/in"
        android:textColor="@color/White"
        android:textSize="18sp" />

</RelativeLayout>

This is the code aboveThis happens when keyboard comes up

What I want is when the keyboard pops up to remove or reduce the margins so that everything remains visible in the upper half of the screen (since the keyboard is half used below. And when the keyboard goes away, then I want everything to be normal.

Fighting for 2 hours, so I’m not going to publish everything that I tried ... I got it android:windowSoftInputMode="adjustResize"in the manifest.

What happens now: when the keyboard rises, it also raises the rest of it (so that the lower half is on the upper half and the upper half is off the screen). But I want to reduce the margins, so everything remains in the upper half. Is it possible?

And you could help me achieve this.

+3
2

. ().

/**
 * RelativeLayout that can detect when the soft keyboard is shown and hidden.
 *  
 */

public class RelativeLayoutThatDetectsSoftKeyboard extends RelativeLayout {

    public RelativeLayoutThatDetectsSoftKeyboard(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public interface Listener {
        public void onSoftKeyboardShown(boolean isShowing);
    }
    private Listener listener;
    public void setListener(Listener listener) {
        this.listener = listener;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int height = MeasureSpec.getSize(heightMeasureSpec);
        Activity activity = (Activity)getContext();
        Rect rect = new Rect();
        activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
        int statusBarHeight = rect.top;
        int screenHeight = activity.getWindowManager().getDefaultDisplay().getHeight();
        int diff = (screenHeight - statusBarHeight) - height;
        if (listener != null) {
            listener.onSoftKeyboardShown(diff>128); // assume all soft keyboards are at least 128 pixels high
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);       
    }

    }
0

- , .

, . . , .

, , onCreate onConfigurationChanged.

PS: - , , .

0

All Articles