How To Access Widgets In A Custom Dialogpreference With A Inflated Layout?
Im very new to android and Im trying to load/persist values from my customized DialogPreference. Currently, this fails because findViewById returns null. Is the way I (try) to do i
Solution 1:
Ok I found it out myself. Well, I still do not know what caused the error, but I did a lot of changes to the layout and code and suddenly it just worked. I tried to revert to the code that I posted here, but I cannot reproduce the error. Im posting my working code, so anybody who runs into this problem, may use it.
An admin may also choose to delete this post, as it may be not possible to reproduce the error.
Here is the layout:
<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><TextViewandroid:text="Insert IP address"android:layout_width="match_parent"android:layout_height="wrap_content" /><EditTextandroid:layout_width="match_parent"android:layout_height="wrap_content"android:id="@+id/AddressBox" /><TextViewandroid:text="Insert identifier"android:layout_width="match_parent"android:layout_height="wrap_content" /><EditTextandroid:layout_width="match_parent"android:layout_height="wrap_content"android:id="@+id/HostnameBox" /></LinearLayout>
and the AddressDialogPreference.java:
publicclassAddressDialogPreferenceextendsDialogPreference {
private EditText ipBox;
private EditText hostBox;
publicAddressDialogPreference(Context context, AttributeSet attrs) {
super(context, attrs);
setDialogLayoutResource(R.layout.address_dialog);
}
@OverrideprotectedvoidonBindDialogView(View view) {
ipBox = (EditText) view.findViewById(R.id.AddressBox);
hostBox = (EditText) view.findViewById(R.id.HostnameBox);
SharedPreferencespref= getSharedPreferences();
hostBox.setText(pref.getString(getKey() + "_host","ExampleHostname"));
ipBox.setText(pref.getString(getKey() + "_ip","192.168.1.1"));
super.onBindDialogView(view);
}
@OverrideprotectedvoidonDialogClosed(boolean positiveResult) {
if(!positiveResult)
return;
SharedPreferences.Editoreditor= getEditor();
editor.putString(getKey() + "_host",hostBox.getText().toString());
editor.putString(getKey() + "_ip",ipBox.getText().toString());
editor.commit();
super.onDialogClosed(positiveResult);
}
}
Post a Comment for "How To Access Widgets In A Custom Dialogpreference With A Inflated Layout?"