Using findviewbyid in preference

I have a preferences screen and I am specifying a layout resource using the android:layout attribute. In preference activity, I use / inflate preference using the addPreferencesFromResource method from onCreate . Inside the layout, I have few text views and buttons.

The problem is that I cannot find text fields, checkboxes, and buttons using the findViewById method! Each result for findViewById returns null.

EDIT

I need to bind a listener to a flag.

Can anyone help me here?

EDIT 2 :

1) preference_layout.xml

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="33dp" android:gravity="center_vertical" android:minHeight="33dp" android:id="@+id/preference_header_switch_item_responder_layout" style="?android:attr/listSeparatorTextViewStyle" > <CheckBox android:id="@+id/switchWidget" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:clickable="true" android:focusable="false" android:padding="8dip" android:layout_marginRight="14dip" /> </LinearLayout> 

2) preference screen

 <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" > <Preference android:defaultValue="false" android:key="auto_responder_key" android:widgetLayout="@layout/preference_layout" android:title="@string/auto_responder"> </Preference> </PreferenceScreen> 

3) OnCreate prefix operation:

EDIT 3:

 @Override protected void onCreate(Bundle savedInstanceState) { if(savedInstanceState != null && savedInstanceState.containsKey(HEADERS_KEY)) { headers = savedInstanceState.getParcelableArrayList(HEADERS_KEY); } super.onCreate(savedInstanceState); applicationContext = getApplicationContext(); setupSimplePreferencesScreen(); // setContentView(getListView()); headerCompound = findViewById(R.id.switchWidget); headerCompoundEmail = findViewById(R.id.switchWidgetEmail); } 

and

 private void setupSimplePreferencesScreen() { if (!isSimplePreferences(this)) { return; } addPreferencesFromResource(R.xml.responder_generic); PreferenceCategory fakeHeader = new PreferenceCategory(this); fakeHeader.setLayoutResource(R.layout.place_holder_auto_reply_header); getPreferenceScreen().addPreference(fakeHeader); addPreferencesFromResource(R.xml.auto_reply_sms_call_check_box_preference); addPreferencesFromResource(R.xml.auto_reply_preferences); Preference smsTemplatePreference = findPreference(SMS_TEMPLATE_KEY); if(smsTemplatePreference != null) { bindPreferenceSummaryToValue(smsTemplatePreference); } Preference customTemplatePreference = findPreference(SMS_CUSTOM_TEMPLATE_KEY); if(customTemplatePreference != null) { customTemplatePreference.setEnabled(false); bindPreferenceSummaryToValue(customTemplatePreference); getPreferenceScreen().removePreference(customTemplatePreference); } // Add 'email' preferences, and a corresponding header. // Add 'email header' preferences. addPreferencesFromResource(R.xml.email_enable_check); addPreferencesFromResource(R.xml.email_preferences); Preference passwordPreference = findPreference(EMAIL_PWD_KEY); if(passwordPreference != null) { bindPreferenceSummaryToValue(passwordPreference); // the advanced version of SMS template } Preference customPasswordPreference = findPreference(EMAIL_CUSTOM_PWD_KEY); if(customPasswordPreference != null) { bindPreferenceSummaryToValue(customPasswordPreference); getPreferenceScreen().removePreference(customPasswordPreference); } // Add 'network' preferences, and a corresponding header. fakeHeader = new PreferenceCategory(this); fakeHeader.setLayoutResource(R.layout.place_holder_network_header); getPreferenceScreen().addPreference(fakeHeader); addPreferencesFromResource(R.xml.network_preferences_check_box); // Add 'misc' preferences, and a corresponding header. fakeHeader = new PreferenceCategory(this); fakeHeader.setLayoutResource(R.layout.place_holder_misc_header); getPreferenceScreen().addPreference(fakeHeader); addPreferencesFromResource(R.xml.misc_preferences); addPreferencesFromResource(R.xml.misc_reject_ignore_check_box_preferences); // Bind the summaries of EditText/Switch/CheckBox preferences to // their values. When their values change, their summaries are updated // to reflect the new value, per the Android Design guidelines. bindPreferenceSummaryToValue(findPreference(AUTO_REPLY_SMS_KEY)); bindPreferenceSummaryToValue(findPreference(AUTO_REPLY_CALL_KEY)); bindPreferenceSummaryToValue(findPreference(DATA_NETWORK_KEY)); bindPreferenceSummaryToValue(findPreference(WIFI_KEY)); bindPreferenceSummaryToValue(findPreference(PASSCODE_KEY)); bindPreferenceSummaryToValue(findPreference(EMAIL_USR_KEY)); bindPreferenceSummaryToValue(findPreference(EMAIL_INT_KEY)); bindPreferenceSummaryToValue(findPreference(AUTO_RESPONDER_KEY)); } 

I tried the above code in onPostCreate , but still the return value is null.

I want to find checkboxes for responder_generic.xml and email_preferences.xml. Is there any way to find and add listeners to them?

Thanks Adithya

+5
android android-preferences preferenceactivity
Mar 31 '13 at 9:39
source share
3 answers

What I did was create user preferences, and it worked. I simply extended the Preference class and inflated the layout specified in the layout /% MY_LAYOUT% .xml. The listeners were also called what I wanted to achieve.

I donโ€™t believe this solution anywhere, so I am adding this to the Wiki community.

Note : if one preference is added, then a one-time solution is enough. I add a few settings to the screen (in my case 2), I implemented custom settings.

+2
Apr 02 '13 at 12:00
source share

From what you asked, it seems that you cannot get any kind when using

 findViewById() 

therefore, one of the possible reasons for this behavior may be that you may not have caused

  setContentView(R.id.yourlayout); 

check this in your code ...

0
Mar 31 '13 at 11:08
source share

I had the same problem as trying to put RadioGroup on PreferenceScreen. I tried to install the tested RadioButton when creating an Activity (for example, restoring a boolean value from sharedpreferences), but I could not findViewById or a similar OnCreate method. Here's my (ugly but working) solution: I extended the RadioButton class:

 public class RadioButtonSettings extends RadioButton { public RadioButtonSettings(Context context, AttributeSet attrs) { super(context, attrs); boolean isLocal = context.getSharedPreferences("",Context.MODE_PRIVATE).getBoolean("local",true); switch(getId()){ case R.id.radio_in: if(isLocal) setChecked(true); break; case R.id.radio_out: if(!isLocal) setChecked(true); break; default: break; } }} 

then put somewhere in your preferences.xml file (loaded by the PreferenceActivity function):

 <PreferenceCategory android:title="CATEGORY EXAMPLE"> <Preference android:layout="@xml/category_one" android:selectable="false" android:key="radiobuttons"> </Preference> </PreferenceCategory> 

and finally create another file in the res / xml directory (named category_one.xml) and put:

 <RadioGroup android:id="@+id/radio" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <your.package.RadioButtonSettings tools:context="your.package.SettingsActivity" android:id="@+id/radio_in" android:text="@string/radioIn" android:layout_width="wrap_content" android:layout_height="wrap_content"> </your.package.RadioButtonSettings> <your.package.RadioButtonSettings tools:context="your.package.SettingsActivity" android:id="@+id/radio_out" android:text="@string/radioOut" android:layout_width="wrap_content" android:layout_height="wrap_content"> </your.package.RadioButtonSettings> </RadioGroup> 

You can also use the android: onClick = "" option from the xml file to get OnClickListener. In the specific case, it was a simple check for Boolean "local" (radio blocks "in" and "radio blocks"). You can also override other classic methods. I think this is a better solution than expanding the entire class of preferences. Hope this helps someone.

0
Apr 15 '16 at 20:27
source share



All Articles