2

I am learning the Preferences and I have created this

enter image description here

After clicking on EditText I am getting this

enter image description here

What I am trying to do is this.

enter image description here

The keybord and focus on edittext only shows when I click on it. I want it automatically when I click on EditText(This is editxText).

Vivek Verma
  • 65
  • 1
  • 10

2 Answers2

2

You can write like this :

    EditText editText = findViewById(R.id.textViewId);
    editText.requestFocus();
    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
Nensi Kasundra
  • 1,980
  • 6
  • 21
  • 34
  • 1
    It is an EditText Preference, not an edit text, I can not access it by its id bcz it does not have an id, it has a key – Vivek Verma Jan 10 '20 at 12:23
1

in your preference fragment that extends from PreferenceFragmentCompat override onResume() with below

@Override
public void onResume() {
    super.onResume();
    Preference search = findPreference(getString(R.string.setting_edit_text_key));
    search.performClick();
}

Illustration: you inflate the EditTextPreference using its key, and perform a click on it.

Zain
  • 37,492
  • 7
  • 60
  • 84
  • One thing, there is no method named as performClick(). You have to use setOnPrefernceClickListener()\ – Vivek Verma Jan 10 '20 at 15:57
  • Hi @VivekVerma it seems that you are using `Preference` from `android.preference` package instead of `android.support.v7.preference package`; `performClick()` is available at the later one – Zain Jan 10 '20 at 17:44