Get an idea of ​​preference with custom layout

I have a preference android: layout gft. I set the layout in the android: layout property of preference in the xml file.

Now I need to initialize the button in the layout (this is the +1 button). I need to get this button in the onCreate method (this is the +1 button), but when I use findViewById(button_id) , it returns null.

Is there any way to get an idea of ​​this preference?

UPDATE: It seems that preference is not created after I add xml and I get null. Where can I call findViewById so that the button is already created?

Thanks.

Xml preferences:

 <Preference android:title="Rate this app" android:key="rate" android:widgetLayout="@layout/plusone_pref"/> </PreferenceScreen> 

Preferred xml layout:

 <?xml version="1.0" encoding="utf-8"?> <com.google.android.gms.plus.PlusOneButton xmlns:android="http://schemas.android.com/apk/res/android" xmlns:plus="http://schemas.android.com/apk/lib/com.google.android.gms.plus" android:id="@+id/plus_one_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:focusable="false" plus:size="standard" /> 
+8
android android-preferences
source share
1 answer

You need to create a class extending the Preference , then override onBindView and you will have access to the view.

 public class MyPreference extends Preference { ... @Override protected void onBindView(View view) { super.onBindView(view); View myView = (View) view.findViewById(R.id.my_id); // ... do stuff } } 

You may have to override some other methods, read about user preferences in PreferenceActivity - http://developer.android.com/guide/topics/ui/settings.html

+9
source share

All Articles