Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically change data of Custom PreferenceScreen

I am working on custom PreferenceScreen, I have created a custom screen for the settings page using PreferenceActivity.

Below is my preference screen.

enter image description here

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>
like image 785
Niranj Patel Avatar asked Apr 18 '13 06:04

Niranj Patel


1 Answers

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.

like image 66
CodeShane Avatar answered Nov 08 '22 07:11

CodeShane