Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android EditText Width because Hint

I have an EditText. When i have less then 8 characters, the EditText has an minimum width. I want to use wrap_content.

It is caused because by the hint.

How can i solve this? I have tried everything.

I want like this:

enter image description here

Here is my code

<EditText
        android:id="@+id/et_actionbar_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="YOLO"
        android:hint="New team"
        android:gravity="center"
        android:textColor="@color/gray1"
        android:textStyle="bold"
        android:background="@color/gold"
        android:layout_centerInParent="true"

        android:minWidth="0dp"
        android:minEms="0"
        android:minHeight="0dp"
        android:paddingLeft="0dp"
        android:layout_marginLeft="0dp"

        />
like image 921
BertLeonaers Avatar asked Apr 25 '14 10:04

BertLeonaers


1 Answers

The only way to do this is to remove the hint or reduce the length of hint string. If you don't want to remove the hint, hide it when a text is entered into the EditText.

editText.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {

                if(!TextUtils.isEmpty(editText.getText().toString())){
                    editText.setHint("");
                 }else{
                    edittext.setHint(R.string.hint);
                 }

            }

            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                    int arg3) {
                // TODO Auto-generated method stub

            }

            @Override
            public void afterTextChanged(Editable arg0) {
                // TODO Auto-generated method stub

            }
        });
like image 99
Anu Avatar answered Nov 07 '22 04:11

Anu