Season
Season

Reputation: 1218

Android - how to make 2 hints on an EditText?

I know I can set a hint text for an EditText.
And currently, I just use space to separate 2 hints. However, the right hint text does not align to right of EditText.

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="30sp"
        android:hint="LEFT                RIGHT" />

What I want is as following style: enter image description here

How can make one hint on left and another hint on right for a single EditText?

Upvotes: 3

Views: 2027

Answers (2)

Konrad Krakowiak
Konrad Krakowiak

Reputation: 12365

Hi your issue is very interesting. In the source code of EditText you can see that hint is only CharSequence. There is no possible to set double hint. But you can create your own view which allow this. Look at my solution. Is very easy and it works.

two_hints_edit_text.xml

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_gravity="center_vertical"
              android:layout_width="match_parent"
              android:layout_height="wrap_content">
    <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/non_active_edit_text"
            android:focusable="false"
            android:gravity="right|center_vertical"
            android:hint="Right"
            android:paddingTop="4dp"
            android:background="@null"/>


    <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/active_edit_text"
            android:hint="Left"
            android:layout_gravity="center_horizontal"/>
</merge>

EditTextWithTwoHints.java file

public class EditTextWithTwoHints extends RelativeLayout{

    EditText activeField;
    EditText nonActiveEditText;

    final CharSequence hint;

    public EditTextWithTwoHints(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.two_hints_edit_text, this);
        activeField = (EditText)findViewById(R.id.active_edit_text);
        nonActiveEditText = (EditText)findViewById(R.id.non_active_edit_text);
        hint = nonActiveEditText.getHint();
        activeField.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                nonActiveEditText.setHint(s.length() !=0 ? "" : hint);
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }
}

Example on use in xml

<you.package.EditTextWithTwoHints
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/textView"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"/>

Additionally you can create your own attribute to make your view more friendly to use.

Upvotes: 2

Mago
Mago

Reputation: 545

I don't think it is possible by normal means. Try using custom view with layout like this:

<FrameLayout
    android:id="@+id/main_holder"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <EditText
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <LinearLayout
        android:id="@+id/hints_holder"
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/left_hint"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Left hint" />

        <View
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="0dp" />

        <TextView
            android:id="@+id/right_hint"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Left hint" />
    </LinearLayout>
</FrameLayout>

Then just add OnClickListener to main_holder and inside set visibility of hints_holder to View.GONE

Upvotes: 0

Related Questions