Reputation: 95
is there a method by which an EditText can be 'temporarily' cleared when SELECTED? In other words, I'd like to achieve these steps:
1) at the beginning it is present a hint
2) as soon as I select the EditText, it appears cleared, empty, with only the lightening cursor visible
3) without clicking 'send', so whithout confirming the written text, I click on somewhere else in the activity
4) the intial hint will appears again
I tried using android:hint, but the text doesn't disappear when EditText is selected. Can anyone help me?
Thanks.
Upvotes: 0
Views: 42
Reputation: 1432
You need the hint text color transparent when EditText is focused and gray in normal state. Create a text_color_hint.xml
file in res/drawable
:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:color="@android:color/transparent" />
<item android:color="#808080" />
</selector>
Then assign the text_color_hint
to android:textColorHint
. Your EditText should look something like this:
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="HINT HERE"
android:textColorHint="@drawable/text_color_hint"/>
To avoid the EditText being auto focused, you can:
1) Add android:focusable="true"
and android:focusableInTouchMode="true"
in the parent layout of EditText, here I take LinearLayout as an example:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:focusableInTouchMode="true">
OR
2) Create a dummy LinearLayout which takes no space, add the above 2 elements to it, and put it right before the EditText:
<LinearLayout
android:layout_width="0dp"
android:layout_height="0dp"
android:focusable="true"
android:focusableInTouchMode="true"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="HINT HERE"
android:textColorHint="@drawable/text_color_hint"/>
Upvotes: 1