Reputation: 432
My app contains an EditText
object with some text in it. When the activity opens I don't want this EditText
to be editable (view only, no blinking cursor & no keyboard pop up). Then in order to edit, the user clicks on the EditText
(This brings back the blinking cursor and the keyboard pops up).
There should have been a function like setEditable(boolean)
. The editable XML attribute in EditText
is deprecated (The warning suggests using setInputType()
instead).
Is there a way to achieve this?
Final note: I have searched the internet for couple hours and this is the best I ended up with:
@Override
protected void onCreate(Bundle savedInstanceState) {
...
editor = (EditText) findViewById(R.id.noteEditText);
editor.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
editor.setInputType(
EditorInfo.TYPE_TEXT_FLAG_CAP_SENTENCES | EditorInfo.TYPE_TEXT_FLAG_MULTI_LINE);
}
});
editor.setInputType(EditorInfo.TYPE_NULL);
...
}
This doesn't work as I intend. I have to double click in order to be able to edit (On first click the blinking cursor and the keyboard don't appear, in second click they do)
Upvotes: 0
Views: 322
Reputation: 11018
Place this in your parent tag of the XML and you are done.
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"
But the basic idea is to stop child views getting focus before anything else. So just give focus to the parent view group before the child.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainLayout"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true" >
<EditText
android:id="@+id/noteEditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:ems="10"
android:inputType="number"
android:maxLength="30" >
</EditText>
</RelativeLayout>
Upvotes: 2