I am working on custom PreferenceScreen
, I have created a custom screen for the settings page using PreferenceActivity
.
Below is my preference screen.
Issue:- I need to change badge of Download data dynamically. I followed this question for achieve this layout. I already tried all answer of that question but not working single answer.
Is there any other way to find View which is inside preference?
settings.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<Preference android:title="@string/settings_user_profile" android:key="user_profile" android:summary="@string/settings_user_profile_desc" android:layout="@layout/setting_list"></Preference>
<Preference android:title="@string/settings_download" android:key="download_data" android:summary="@string/settings_download_desc" android:layout="@layout/setting_list"></Preference>
</PreferenceScreen>
You can subclass Preference
, overriding onBindDialogView()
- just remember to change the XML from <Preference...>
to include your package and class <com.example.app.BadgedPreference...>
:
@Override
protected void onBindDialogView(View v) {
super.onBindDialogView(v);
Log.v( "onBindDialogView()", v.getClass().getSimpleName() );
}
If your BadgedPreference
has a method for handling your change, you can use it a bit more easily - from within your PreferenceActivity
:
PreferenceScreen myPrefScreen = (PreferenceScreen)getPreferenceScreen();
BadgedPreference bp = (BadgedPreference)myPrefScreen.findPreference("download_data");
// Custom method
bp.setBadgeValue(12);
Even more simply, you can use a standard Preference
, just style its summary field as a 'badge', then you can set the value using the included Summary field:
Preference pref = (Preference)myPrefScreen.findPreference("download_data");
pref.setSummary("12");
Let me know if you have any questions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With