Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make edit text not editable but clickable in JAVA

Is it possible to make EditText clickable but not editable.

I don't want it to be editable (Neither should the keyboard come up nor should we be able to change the hint)

Actually I want to use Edit text simply as an image with a hint (which cannot be changed). I know the actual way was to use an ImageView and one TextView, but I want it to try with EditText because I'll then be using only one view instead of 2. Also every thing is dynamic so no XML.

For the above requirement the solution in XML is android:editable="false" but I want to use this in Java.

But,

If I use :-

et.setEnabled(false); 

or

 et.setKeyListener(null); 

It makes the EditText not EDITABLE but at the same time it makes it non clickable as well.

like image 212
Aamir Shah Avatar asked Dec 30 '12 12:12

Aamir Shah


People also ask

How do you make EditText not editable and clickable?

For the above requirement the solution in XML is android:editable="false" but I want to use this in Java. et. setKeyListener(null); It makes the EditText not EDITABLE but at the same time it makes it non clickable as well.

How do I turn an editable into a string?

Simply use toString() on the Editable instance to get String.


2 Answers

The trick here is :-

et.setFocusable(false); et.setClickable(true); 
like image 71
Aamir Shah Avatar answered Oct 07 '22 07:10

Aamir Shah


As editable is deprecated. you can use android:inputType as none in designing xml or as InputType.TYPE_NULL in coding.

Designing XML

<android.support.v7.widget.AppCompatEditText     android:id="@+id/edt"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:clickable="true"     android:focusable="false"     android:inputType="none"     android:text="EditText"/> 

Coding

edt.setClickable(true); edt.setFocusable(false); edt.setInputType(InputType.TYPE_NULL); 

And below is the click event of edittext

edt.setOnClickListener(new View.OnClickListener() {     @Override     public void onClick(View v) {         Toast.makeText(YourActivity.this,"edt click", Toast.LENGTH_LONG).show();     } }); 
like image 41
Nirav Bhavsar Avatar answered Oct 07 '22 08:10

Nirav Bhavsar