ARR.s
ARR.s

Reputation: 769

How to add icon hint Edittext in center

How to add icon hint edittext in center

I have no idea to set that

and in xml file is not have android:drawbleCenter

How to create it

thanks for your help ;)

enter image description here

Upvotes: 2

Views: 1196

Answers (3)

akhilesh0707
akhilesh0707

Reputation: 6899

Unfortunately you can not set drawable at the center of the EditText like this, Alternatively, you can use background image which includes your hint + image what ever you display and inside,

Try below example.

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:background="@android:drawable/editbox_background"
android:gravity="center">

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@null"
    android:drawableLeft="@android:drawable/ic_menu_search"
    android:gravity="center"
    android:hint="Search" />
</RelativeLayout> 

You can achieve this type of view using above XML code

enter image description here

Upvotes: 4

Nguyễn Trung Hiếu
Nguyễn Trung Hiếu

Reputation: 2032

I have a solution

This is xml:

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="50dp">


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

    <TextView
        android:id="@+id/text_icon"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:drawableLeft="@android:drawable/ic_menu_search"
        android:drawablePadding="8dp"
        android:layout_gravity="center_horizontal"
        android:gravity="center_vertical"
        android:text="Search"
        android:textColor="#969696" />
</FrameLayout>

in onCreate()

edtSearch.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) {
            textIcon.setVisibility(View.GONE);
        }

        @Override
        public void afterTextChanged(Editable s) {
            textIcon.setVisibility(s.length() == 0 ? View.VISIBLE : View.GONE);
        }
    });

Try it!

Upvotes: 0

Girish H
Girish H

Reputation: 79

Add android:gravity="center_horizontal" to EditText

Upvotes: 0

Related Questions