0

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)

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • 1
    You could just make sure the EditText doesn't get focus first when the activity resumes/starts and whenever the EditText is tapped it will get focus and function as normal. – cincy_anddeveloper Nov 17 '17 at 16:24
  • 1
    Possible duplicate of [Stop EditText from gaining focus at Activity startup](https://stackoverflow.com/questions/1555109/stop-edittext-from-gaining-focus-at-activity-startup) – Simo Nov 17 '17 at 16:27

1 Answers1

2

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>
vikas kumar
  • 10,447
  • 2
  • 46
  • 52