henrykodev
henrykodev

Reputation: 3084

Regain Focus on EditText when User Clicks It

I have an activity wiht some EditText, and I want the following behaviour for one of the EditText: on activity starting-up, the EditText is populated with some text. It should not show cursor, has no focus and not show keyboard (so user can just read the text and scroll up and down). Then when the user clicks/touches the EditText, it will gain focus, show cursor and show keyboard (so user can start editing).

The behaviour at starting up is working OK, but I cannot get the "start editing" behaviour to work when user clicks on the EditText.

Here is my code:

activity_main.xml: ...

    ...

    <EditText
        android:id="@+id/notepad"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="0.5"
        android:background="@android:color/transparent"
        android:textSize="@dimen/font_large"
        android:textStyle="normal"
        android:textColor="@color/appPrimaryText"
        android:textColorHint="@color/appAccentColour"
        android:gravity="top"
        android:scrollbars="vertical"        />

        ...

    <View
        android:layout_width="match_parent"
        android:layout_height="@dimen/activity_margin"></View>
</LinearLayout>

MainActivity.java:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mNoteEditText = (EditText) findViewById(R.id.notepad);
    final MainActivity mainActivity = this;
    mNoteEditText.setFocusable(false);
    mNoteEditText.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            showToastMessage("here", false);
            mNoteEditText.setFocusable(true);
            mNoteEditText.requestFocus();
            mNoteEditText.setCursorVisible(true);
            showKeyboard(mNoteEditText);
        }
    });
}

public void showKeyboard(EditText editText) {
    InputMethodManager inputMethodManager =
            (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);

    inputMethodManager.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
}

The activity's manifest entry is:

    <activity
        android:name=".ui.MainActivity"
        android:label="@string/app_name"
        android:launchMode="singleTask"
        android:windowSoftInputMode="stateAlwaysHidden|adjustPan" >
        <!--Main launcher.-->
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

The problem is, the onClick method is firing (I can see the toast), but the other lines have no effect on the EditText (mNoteEditText).

Thanks.

Upvotes: 0

Views: 127

Answers (1)

Awadesh
Awadesh

Reputation: 4058

So here is the best solution of your problem. Declare your manifest file like this-

<activity
    android:name=".ui.MainActivity"
    android:label="@string/app_name"
    android:launchMode="singleTask"
    android:windowSoftInputMode="stateHidden|adjustPan" >
    <!--Main launcher.-->
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

In your xml define editText like this

 <EditText
    android:id="@+id/notepad"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="0.5"
    android:background="@android:color/transparent"
    android:textSize="@dimen/font_large"
    android:textStyle="normal"
    android:textColor="@color/appPrimaryText"
    android:textColorHint="@color/appAccentColour"
    android:gravity="top"
    android:cursorVisible="false"
    android:scrollbars="vertical"        />

In your mainactivity edit protected void onCreate(Bundle savedInstanceState) method like this-

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mNoteEditText = (EditText) findViewById(R.id.notepad);
mNoteEditText.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
       if (view.getId() == mNoteEditText.getId())
    {
        mNoteEditText.setCursorVisible(true);
    }
    }
});
  }

that's it :)

Upvotes: 1

Related Questions