In my app I have a PreferenceScreen which looks like this:
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<CheckBoxPreference
android:key="state"
android:title="@string/stateTitle"
android:summary="@string/stateSummary"
android:defaultValue="false" />
<Preference
android:key="test"
android:layout="@layout/pref_control"
android:dependency="state" />
</PreferenceScreen>
Now my question is how can I get the views which are in my pref_control.xml
from the PreferenceActivity? I tried with normal findViewById()
but that ends in a NullPointerException. Here is my code that causes the exception:
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
((Button) findViewById(R.id.startTime)).setText("bla");
((Button) findViewById(R.id.endTime)).setText("bla");
}
You can get the Preference by using Preference p = findPrefences("test")
from the Activity (or better the PreferenceFragment, if you are using newer versions of Android).
On that object you can call .getView(convertView, parent)
. For your purposes, it should be ok to call that method with null as paramters: findPreferences("test").getView(null, null)
.
HTH
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With