0

I have a simple contact form on my Android app, it has a Name and Message field and a submit button.

The form works fine, my issue is with the layout/behaviour of the keyboard.

When the user clicks on the Name field, the keyboard pops up and they enter their name and the keyboard then displays 'Next' button, so they can move to the Message field, but then the keyboard changes to 'Enter' for a carriage return, because the android:inputType is textMultiLine

The issue is that now the 'Submit' button is hidden behind the keyboard and the user has to hit the 'back' button to then see the submit button to submit the form.

If I change the android:inputType from textMultiLine to text, the keyboard displays 'Done' which works well, but now all the text entered in the Message field is all on one line and this isn't ideal either.

The ideal solution would be for the keyboard to display the 'Next' so that I can jump to the 'Submit' button (which I do not think is possible) or for the screen to scroll up to see the 'Submit' button (or at least be scrollable).

Any ideas on how I can do this? Thank you.

Thank you.

Here is my activity_contact.xml file:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/activity_home"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFFFFF"
    tools:context="com.mycompany.myapp.fragments.HomeFragment">
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="fitXY"
        android:src="@drawable/bgplain"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:id="@+id/toplinear">
        <LinearLayout
            android:id="@+id/linearmenu"
            android:layout_width="match_parent"
            android:paddingTop="10dp"
            android:paddingLeft="10dp"
            android:layout_height="wrap_content">
            <ImageView
                android:id="@+id/menu"
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:src="@drawable/menu"/>
        </LinearLayout>
        <LinearLayout
            android:layout_below="@+id/linearmenu"
            android:layout_width="match_parent"
            android:layout_above="@+id/tvvolume"
            android:layout_height="130dp"
            android:orientation="vertical"
            >
            <LinearLayout
                android:id="@+id/lineartitle"
                android:layout_below="@+id/linearmenu"
                android:layout_width="match_parent"
                android:layout_weight="4"
                android:layout_height="120dp">
                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:src="@drawable/logo"/>
            </LinearLayout>



        </LinearLayout>
    </LinearLayout>
    <LinearLayout
        android:layout_below="@+id/toplinear"
        android:layout_width="match_parent"
        android:paddingBottom="20dp"
        android:layout_height="match_parent">
        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <LinearLayout

                android:layout_width="match_parent"
                android:orientation="vertical"
                android:paddingLeft="15dp"
                android:paddingTop="10dp"
                android:paddingRight="15dp"
                android:layout_height="match_parent">

                <TextView
                    android:text="Name"
                    android:textColor="#000000"
                    android:textSize="16sp"
                    android:textAllCaps="true"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    />
                <EditText
                    android:id="@+id/edtname"
                    android:layout_marginTop="10dp"
                    android:layout_width="match_parent"
                    android:layout_height="40dp"
                    android:textSize="16sp"
                    android:singleLine="true"
                    android:inputType="textPersonName"
                    android:paddingLeft="10dp"
                    android:hint="Enter name here"
                    android:background="@drawable/edtback"/>

                <TextView
                    android:text="Message"
                    android:textAllCaps="true"
                    android:textColor="#000000"
                    android:textSize="16sp"
                    android:layout_marginTop="20dp"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    />
                <EditText
                    android:id="@+id/edtmessage"
                    android:layout_marginTop="10dp"
                    android:layout_width="match_parent"
                    android:layout_height="100dp"
                    android:gravity="top"
                    android:inputType="textMultiLine"
                    android:padding="10dp"

                    android:textSize="16sp"
                    android:hint="Enter message here"
                    android:background="@drawable/edtback"/>

                <Button
                    android:id="@+id/btnsubmit"
                    android:layout_width="match_parent"
                    android:layout_height="50dp"
                    android:text="Submit"
                    android:textAllCaps="true"
                    android:textSize="18sp"
                    android:textColor="#FFFFFF"
                    android:background="@drawable/btnback"
                    android:layout_marginTop="20dp"/>
            </LinearLayout>
        </ScrollView>

    </LinearLayout>

</RelativeLayout>

Update : I've tried adding a android:imeOptions="actionNext" to the Message field, but it won't override it, presumably because it is a textMultiLine type field(?)

omega1
  • 1,031
  • 4
  • 21
  • 41
  • 1
    You can use `text` input type, but limit letters in one line. It could not be done in XML. You need to do it programmatically. How to do it you can see there: https://stackoverflow.com/questions/40085758/limit-text-length-in-a-line-edittext-android. On other side you can add keyboard close listener on `EditText` focus lose. – Dumbo Feb 01 '18 at 13:43
  • @EJusius Thank you, I din't think it could be done from what I have read, the 'close keyboard on lose focus' may be a solution, I'll see what I can do with that with my limited knowledge! Thanks. – omega1 Feb 01 '18 at 13:48
  • I will try to show you an example of that – Dumbo Feb 01 '18 at 13:50
  • You should probably use TextWatcher and onTextChanged Listener to shift focus to the second EditText while putting them all in a scroll view in the layout. – CodeBulls Inc. Feb 01 '18 at 14:04

2 Answers2

0

How about adding

android:windowSoftInputMode="stateAlwaysHidden|adjustPan"

to your activity in AndroidManifest? Also you can try another options which probably will suits better https://developer.android.com/guide/topics/manifest/activity-element.html#wsoft

Marina Ž
  • 1
  • 1
  • Thank you. I tried that and see no difference, I had already tried adding `android:windowSoftInputMode="adjustResize"` which didn't make any difference either. – omega1 Feb 01 '18 at 13:55
0

This is an example of my second offer. To hide keyboard on EditText focus lose.
First you should create a onFocusChangeListener class.

public class KeyboardCloseListener implements View.OnFocusChangeListener {

    private Activity activity;

    public KeyboardCloseListener(Activity context) {
        this.activity = context;
    }

    @Override
    public void onFocusChange(View view, boolean b) {
        if (!b)
            closeKeyboard(view);
    }

    public void closeKeyboard(View view) {
        InputMethodManager inputMethodManager =(InputMethodManager)activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }
}  

Later you can use this class on all of your EditText fields.
In your activity you can use it like this:

KeyboardCloseListener keyboardCloseListener = new KeyboardCloseListener(this);
edittext.setOnFocusChangeListener(keyboardCloseListener);  

Now it should work. If not, add these lines to your parent View:

android:focusable="true"
android:clickable="true"
Dumbo
  • 1,630
  • 18
  • 33