39

I want to add 10dp margin between EditText and Soft Keyboard.

enter image description here Here is my XML:

<RelativeLayout
    android:id="@+id/BottomLayout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:background="@drawable/bottom_glass" >

    <EditText
        android:id="@+id/message"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:background="@drawable/grey"
        android:ems="10"
        android:layout_marginBottom="10dp"
        android:hint="Enter Message"
        android:textColor="#ffffff" >


    </EditText>

    <ImageButton
        android:id="@+id/sendMessageButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginLeft="20dp"
        android:layout_toRightOf="@+id/message"
        android:background="@android:color/transparent"
        android:src="@drawable/sendselector" />

    <ImageButton
        android:id="@+id/imageButton2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginRight="20dp"
        android:layout_toLeftOf="@+id/message"
        android:background="@android:color/transparent"
        android:src="@drawable/sendmediaselector" />

</RelativeLayout>

Here is my onCreate() :

    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
    messageText.setOnTouchListener(new OnTouchListener(){

                @Override
                public boolean onTouch(View v, MotionEvent event) {
                            ((Swipe)getActivity()).getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_UNSPECIFIED);
                    return false;
                }});

I am using viewpager and full screen theme. The following EditText is in my 3rd fragment.

I've used android:windowSoftInputMode="adjustResize|adjustPan" and android:windowSoftInputMode="adjustResize", but nothing happened.

TheLethalCoder
  • 6,668
  • 6
  • 34
  • 69
Punit
  • 667
  • 1
  • 7
  • 17

6 Answers6

23

I find the attribute of paddingBottom can effect the distance between the editText and the keyboard. You can do like this:

<RelativeLayout   
  android:layout_width="match_parent"   
  android:layout_height="110dp"> 
  <ImageView
     android:layout_width="match_parent"
     android:layout_height="100dp"
     android:background="@drawable/your_edit_text_background"/>   
  <EditText
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:paddingBottom="10dp"/> 
</RelativeLayout>

This will make your keyboard margin your EditText by 10dp

Irshu
  • 8,248
  • 8
  • 53
  • 65
summer1991
  • 387
  • 2
  • 6
3

This seems to work, though has some unwanted effect on the layout.

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

If anybody knows how to get this working with ADJUST_PAN, that would be great!

Veeru
  • 4,936
  • 2
  • 43
  • 61
1

Try to add your RelativeLayout in ScrollView, and then add code below:

editTextField.setOnFocusChangeListener(new OnFocusChangeListener() {

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        // TODO Auto-generated method stub
        if (hasFocus)
             scrollView.scrollBy(0, 150);

    });
Vasily Kabunov
  • 6,511
  • 13
  • 49
  • 53
  • Thanks! If you have `RecyclerView` inside `NestedScrollView`, also scroll `NestedScrollView`, not `RecyclerView`. Also probably add a short delay after a keyboard appears with `postDelayed`. `RelativeLayout` is not required. – CoolMind Jun 30 '22 at 09:54
  • This code not worked on my side – Ucdemir Nov 02 '22 at 08:48
  • if you call scrollView.scrollBy(0, 150); after 300 ms, it work – Ucdemir Nov 02 '22 at 09:00
1

The Vasily Kabunov answer works for me but his answer need a little change for working. The next code is:

editTextField.setOnFocusChangeListener(new OnFocusChangeListener() {

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        // TODO Auto-generated method stub
        if (hasFocus) {
                Handler(Looper.getMainLooper()).postDelayed({
                    //Take care with this line I think that the scroll moves in px and not with dp.
                    scrollView.scrollBy(0, 80)
                }, 100) //You need to play with the time if your activity comes from onCreate and onResume you need to change this value to 1000.
            }

    });
}

And you need to add in your ScrollView or RecyclerView the next lines:

android:paddingBottom="80dp"
android:clipToPadding="false"

IMPORTANT: If you have activated fullscreen the flags adjustResize and adjustPan don't work.

jordiz
  • 279
  • 3
  • 8
0

I solved the same problem by adding just two lines

Add this line in EditText -

android:imeOptions="actionSend|flagNoEnterAction"

and this line in Manifeast file in Activity tag add -

<activity
            android:name="----"
            android:icon="---"
            android:windowSoftInputMode="stateVisible|adjustResize"
            android:label="@string/app_name"
            android:theme="-----" />

and remove all code that you use to do this like onTouchListner and it should work.

Cleb
  • 25,102
  • 20
  • 116
  • 151
-1

Adding android:windowSoftInputMode="adjustResize" to the activity in manifest should solve the issue. And this won't work if your activity is running in fullscreen. So in that case, instead of making it fullscreen via code, just add or change the style <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> to style.xml and use this as your AppTheme. adjustResize or adjustPan both will work.

Eldhose
  • 163
  • 2
  • 4