Adding a TextView in preference settings.

In this preferred screen, the user disconnects the device from their account. At the moment, I have an Unlink device, as soon as the user clicks on it, the cancellation occurs.

But I would like to add a piece of text as follows:

Joe Foo Device ( joefoo@gmail.com ) - Unlink Device

Can I do it? I also need to dynamically add the username from the settings.
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" > <Preference android:title="@string/pref_title_advanced_unlink" > <TextView somehow must be in here android:id="@id/user_name_and_email" /> <intent android:action="android.intent.action.VIEW" android:targetPackage="com.example.tvrplayer" android:targetClass="com.example.tvrplayer.UnlinkActivity" android.setflags="FLAG_ACTIVITY_CLEAR_TOP"/> </Preference> </PreferenceScreen> 
+4
source share
2 answers

Settings have subtitles called summary . Give preference to the key, then you can use findPreference(CharSequence key) in your PreferenceFragment to get a reference to your preference object, like calling findViewById to get Views. Then call setSummary(int) or setSummary(CharSequence) in the preference object.

Alternatively, you can do something completely more complex by providing a custom layout for your preferences and / or Preference subclass objects and implementing some custom data bindings. But I think the above should do what you want.

+2
source

enter image description here

preferences.xml

 <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" > <PreferenceCategory android:key="pref_title_advanced" android:title="Advanced" > <CheckBoxPreference android:defaultValue="false" android:key="pref_title_advanced_link" android:title="Link Device" /> </PreferenceCategory> </PreferenceScreen> 

PrefsActivity.java

 private SharedPreferences mPreferences; private SharedPreferences.OnSharedPreferenceChangeListener mPrefListener; private CheckBoxPreference mCheckBoxPref; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.preferences); mCheckBoxPref = (CheckBoxPreference) getPreferenceScreen().findPreference( "pref_title_advanced_link"); /* * set initial summary as you desire. For example, userIdCurrent can be: * "No Devices linked." */ mCheckBoxPref.setSummary(userIdCurrent); mPrefListener = new SharedPreferences.OnSharedPreferenceChangeListener() { public void onSharedPreferenceChanged(SharedPreferences prefs, String key) { if (key.equals("pref_title_advanced_link")) { /* * set post-click summary as you desire. For example, * userIdPost can be: * "Joe Foo Device ( joefoo@gmail.com )". */ mCheckBoxPref.setSummary(userIdPost); } } }; mPreferences.registerOnSharedPreferenceChangeListener(mPrefListener); } 
+2
source

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