Reputation: 769
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 ;)
Upvotes: 2
Views: 1196
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
Upvotes: 4
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