usage of findviewbyid in preferenceactivity
android, android-preferences, preferenceactivity
Solution
What i did was create a custom preference and it worked. I simply extended `Preference` class and inflated the layout provided in the layout/%MY_LAYOUT%.xml. The listeners were being called as well which is what i wanted to achieve.
This solution was nowhere i believe, hence, i adding this to community Wiki.
Note : if there is a single preference being added then single preference solution should suffice. I am adding multiple preferences on screen (in my case 2), i implemented custom preferences.
Problem
I have a preference screen and i am specifying a layout resource using the `android:layout` attribute. In the preference activity i am using/inflating the preference using `addPreferencesFromResource` from `onCreate` method. Inside the layout i have few textviews and buttons. The problem is i am unable to find any of the textviews, checkboxes and buttons using `findViewById` method ! Every result for `findViewById` returns null. EDIT: I need to attach a listener to the checkbox. Can anyone please help me over 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)Preference Activity onCreate: 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` as well but still the value returned is null. I want to find checkboxes for responder_generic.xml and email_preferences.xml. Is there a way to find and add listeners to them ? Thanks, Adithya