0

Below is a part of my code that sets the phone number automatically.

  @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
        Preference connectionPref = findPreference(key);
        if (connectionPref != null) {
            connectionPref.setSummary(sharedPreferences.getString(key, ""));
        }

        //get phone number automatically
        if (key.equals("pref_phone_number")) {
            if (ContextCompat.checkSelfPermission(MainActivity.getContext(), android.Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {

            }

            TelephonyManager tMgr = (TelephonyManager) MainActivity.getContext().getSystemService(Context.TELEPHONY_SERVICE);
            String mPhoneNumber = tMgr.getLine1Number();
            connectionPref.setSummary(mPhoneNumber);
            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.putString(key, mPhoneNumber);
            editor.commit();
        }

    }

Would it be possible to have the EditTextPreference programmatically filled with mPhoneNumber when it's clicked on and the edit window opens? I want to give the user the ability to edit their number, which right now they can not.

david
  • 997
  • 3
  • 14
  • 34

1 Answers1

0

Can you try like this,

EditTextPreference pref = (EditTextPreference)PreferenceManager.findPreference("edit");
EditText prefEditText = pref.getEditText();
prefEditText.setInputType(InputType.TYPE_CLASS_TEXT);
prefEditText.setSingleLine(true);
prefEditText.setText("yourText");
Manoj Perumarath
  • 9,337
  • 8
  • 56
  • 77
  • This wouldn't be in the `onSharedPreferenceChanged` method right? which method should I put it in? – david Oct 31 '18 at 05:14