Android preference widgetlayout resource

I created a preferences entry inside the preferences screen that looks like this:

<PreferenceScreen> <Preference android:title="title" android:key="key" android:widgetLayout="@layout/someview"/> </PreferenceScreen> 

Here I set the widgetlayout resource, which should be displayed to the right of the preference item (for example, a flag for setting a flag). I can also set this resource in my PreferenceActivity.onCreate() code as follows:

 Preference myPreference = findPreference("key"); myPreference.setWidgetLayoutResource(R.layout.someview); 

Both approaches work fine, so I see my resource to the right of the preference item. However, I cannot access this resource (someview) to change its properties at runtime.

You cannot manually specify a resource identifier by inflating it from a resource or findViewById . I have an invalid resource / resource identifier that was not found. It seems like preference activity is inflating the resource after a while.

Does anyone face the same problem? Any ideas on how to change widgetlayout resource widgetlayout at runtime?

Here is a similar question, but did not answer it

+7
source share
3 answers

I seem to have the same problem, and I tried like this:

1) create a custom Preference and override onBindView() :

 public class ProgressBarPreference extends Preference { private ProgressBar mProgressBar; // constructors @Override protected void onBindView(View view) { super.onBindView(view); mProgressBar = (ProgressBar) view.findViewById(R.id.progressBar); } public ProgressBar getProgressBar() { return mProgressBar; } } 

2) add this preference to the xml screen:

 <package.ProgressBarPreference android:key="key" android:summary="summary" android:title="title" android:widgetLayout="@layout/preference_widget_progressbar" /> 

preference_widget_progressbar.xml

 <?xml version="1.0" encoding="utf-8"?> <ProgressBar xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/progressBar" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" /> 

or call setWidgetLayoutResource() .

3) get a ProgressBar when onPreferenceTreeClick() :

 if (preference instanceof ProgressBarPreference) { ProgressBar progressBar = ((ProgressBarPreference) preference).getProgressBar(); // TODO something } 

However, in a year!

+12
source

I have a partial workaround for the joinAero case. If the goal is simply to show or hide an undefined progress bar in widgets, the trick is to set and clear the widget layout resource in the code.

1) Define the widget layout containing the ProgressBar. No need to assign an identifier. preference_widget_progressbar.xml:

 <?xml version="1.0" encoding="utf-8"?> <ProgressBar xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" /> 

2) Do not specify the widget in settings.xml

 <package.ProgressBarPreference android:key="key" android:summary="summary" android:title="title" /> 

3) When you want to show the progress bar, set the widget resource to preference_widget_progressbar. To clear it, set the value to 0. To update the screen, a call to notifyChanged() is required.

 public void showProgressBar(boolean isVisible) { // Show or hide the entire widget (which contains only a ProgressBar) setWidgetLayoutResource(isVisible ? R.layout.preference_widget_progressbar: 0); notifyChanged(); // Update screen } 

Getting a pointer to the actual ProgressBar is surprisingly difficult, especially if the library project is preferred. I had some success using getIdentifier() according to this post , but this approach actually hides the entire widget, while setting the visibility to the ProgressBar leaves a visible empty widget.

+2
source

It works here:

Create your widget layout by setting the onClick attribute,

 <?xml version="1.0" encoding="utf-8"?> <ImageView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/icon" android:layout_width="48dp" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:background="?android:attr/selectableItemBackground" *android:onClick="onMyKeyClicked"* android:scaleType="centerInside" android:src="@drawable/ic_action_info" /> 

Then set the widget layout in the settings,

 <ListPreference android:defaultValue="1" android:entries="@array/my_entries" android:entryValues="@array/my_values" android:key="my_key" android:title="@string/pref_my_key_title" android:widgetLayout="@layout/pref_layout_my_key" /> 

Then in your activity there is a method like

 public void onMyKeyClicked(View v) { Toast.makeText(this, "My key widget clicked", Toast.LENGTH_SHORT).show(); } 

So, for each preference with widgets, you must have a layout that has the onClick attribute set for a unique method in Activity. What do you think?

0
source

All Articles