Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to make hint disappear when edittext is touched?

In my application in android are many EditText fields. And I ran into a problem with hint. It is not disappearing when EditText is focused, but it disappears when I start to write something. How can this problem be fixed? Because I need the hint to disappear when the EditText field is touched.

It is for example:

<EditText     android:layout_width="260dp"     android:layout_height="30dp"     android:ems="10"     android:inputType="text"     android:id="@+id/driv_lic_num_reg"     android:hint="@string/driver_license_numb"     android:textColor="@color/black"     android:textColorHint="@color/grey_hint"     android:background="@drawable/input_field_background"     android:layout_alignParentTop="true"     android:layout_centerHorizontal="true"     android:layout_margin="2dp"     android:textCursorDrawable="@color/black"     android:textSize="15dp"     android:paddingLeft="10dp"     android:nextFocusDown="@+id/pass_reg"     android:imeOptions="actionNext"     android:maxLines="1"     android:maxLength="50"     android:focusableInTouchMode="true"/> 
like image 678
ToRaRo Avatar asked Jan 21 '14 16:01

ToRaRo


People also ask

How do I remove text from EditText?

just use the android:hint attribute in your EditText. This text shows up when the box is empty and not focused, but disappears upon selecting the EditText box. this should be the accepted answer, +1 for simplicity.

How do you make EditText read only?

The most reliable way to do this is using UI. setReadOnly(myEditText, true) from this library. There are a few properties that have to be set, which you can check out in the source code.

How do you set different gravity for Textinputlayout hint and text in android?

To set a different gravity for the EditText , don't set any gravity attributes in the layout, then set the EditText 's gravity programmatically, in your code. That is, after setContentView() , use findViewById() to get the EditText , then call setGravity(Gravity. CENTER_HORIZONTAL) on it.


1 Answers

You can also custom your XML code by using Selector. Here is an example code.

Create a selector. In file selector.xml

<?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="@color/gray" /> </selector> 

In your view

android:textColorHint="@drawable/selector" 
like image 133
windyzboy Avatar answered Sep 29 '22 09:09

windyzboy