Home » Android » text – Android EditText Width because Hint

text – Android EditText Width because Hint

Posted by: admin June 15, 2020 Leave a comment

Questions:

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"

        />
How to&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

            }
        });

Answer:

I guess it’s not possible to change the font of hint as font is same for hint and text

but the work around will be

First
set the text size

 android:textSize="20sp"

second

then on text change listener change the text size programitically to 10sp.
then u might be able to use wrap content…