Android - Changing the positive text of an EditTextPreference Dialog Programmatically button

I am trying to change positiveButtonText of Dialog to `EditTextPreference. I tried the following without success:

  // The reference to the preference in the view heirarchy mUrlPreference = (EditTextPreference) getPreferenceScreen().findPreference(pref); // Add a textwatcher mUrlPreference.getEditText().addTextChangedListener(prefWatcher); 

prefWatcher :

 private TextWatcher prefWatcher = new TextWatcher() { @Override public void onTextChanged(CharSequence s, int start, int before, int count) { } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void afterTextChanged(Editable s) { Log.i(TAG_LOG, "Updating the positive button text"); mUrlPreference.setPositiveButtonText("Download"); } }; 

When I change the text in EditText, the log is printed, but the text of the Positive button does not change. Is something missing here? My initial thought was that I would need to update the hierarchy of the dialog box, but I did not see any methods in the API for this. Any ideas on what I'm missing here?

+4
source share
1 answer

According to the documentation setPositiveButtonText () , the updated text will be shown in subsequent dialog boxes. To actually affect the button, do this instead:

 @Override public void afterTextChanged(Editable s) { mUrlPreference.setPositiveButtonText("Download"); Button button = (Button) mUrlPreference.getDialog().findViewById(android.R.id.button1); button.setText("Download"); button.invalidate(); // if necessary } 
+1
source

Source: https://habr.com/ru/post/1402245/


All Articles