I need help with preferences. I need to create them programmatically. I have this preference screen:
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceCategory android:title="About" >
<Preference android:title="Software Version" android:summary="1.3.554" android:key="software"/>
<Preference android:title="User" android:summary="[email protected]" android:key="email"/>
<Preference android:title="Terms and conditions" />
</PreferenceCategory>
<PreferenceScreen android:key="log_out" android:layout="@layout/preference_log_out"/>
</PreferenceScreen>
I did this:
PreferenceScreen root = getPreferenceManager().createPreferenceScreen(this);
PreferenceCategory about = new PreferenceCategory(this);
about.setTitle("About");
root.addPreference(about);
Preference software = new Preference(this);
software.setTitle("Software Version");
software.setKey("software");
software.setSummary("1.3.554");
about.addPreference(software);
Preference user = new Preference(this);
user.setTitle("User");
user.setKey("user");
user.setSummary(preferences.getString("user_email", ""));
about.addPreference(user);
Preference terms_and_conditions = new Preference(this);
terms_and_conditions.setTitle("Terms and conditions");
terms_and_conditions.setKey("terms_and_conditions");
about.addPreference(terms_and_conditions);
RelativeLayout layout = new RelativeLayout(this);
layout.setId(1100);
AbsListView.LayoutParams p = new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, AbsListView.LayoutParams.WRAP_CONTENT);
layout.setLayoutParams(p);
TextView log_out_text = new TextView(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
log_out_text.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
log_out_text.setTypeface(null, Typeface.BOLD);
log_out_text.setGravity(Gravity.CENTER);
log_out_text.setTextColor(Color.parseColor("#FFFFFF"));
log_out_text.setBackgroundDrawable(log_out_drawable);
log_out_text.setLayoutParams(params);
layout.addView(log_out_text);
Preference log_out = new Preference(this);
log_out.setLayoutResource(1100);
root.addPreference(log_out);
setPreferenceScreen(root);
The problem is with the third line from above...Can anybody help me????
Error is:
01-30 10:56:16.886: E/AndroidRuntime(3420): FATAL EXCEPTION: main
01-30 10:56:16.886: E/AndroidRuntime(3420): android.content.res.Resources$NotFoundException: Resource ID #0x44c
01-30 10:56:16.886: E/AndroidRuntime(3420): at android.content.res.Resources.getValue(Resources.java:892)
01-30 10:56:16.886: E/AndroidRuntime(3420): at android.content.res.Resources.loadXmlResourceParser(Resources.java:1869)
01-30 10:56:16.886: E/AndroidRuntime(3420): at android.content.res.Resources.getLayout(Resources.java:731)
01-30 10:56:16.886: E/AndroidRuntime(3420): at android.view.LayoutInflater.inflate(LayoutInflater.java:318)
01-30 10:56:16.886: E/AndroidRuntime(3420): at android.preference.Preference.onCreateView(Preference.java:412)
01-30 10:56:16.886: E/AndroidRuntime(3420): at android.preference.Preference.getView(Preference.java:389)
01-30 10:56:16.886: E/AndroidRuntime(3420): at android.preference.PreferenceGroupAdapter.getView(PreferenceGroupAdapter.java:221)
01-30 10:56:16.886: E/AndroidRuntime(3420): at android.widget.AbsListView.obtainView(AbsListView.java:1554)
01-30 10:56:16.886: E/AndroidRuntime(3420): at android.widget.ListView.makeAndAddView(ListView.java:1793)
01-30 10:56:16.886: E/AndroidRuntime(3420): at android.widget.ListView.fillDown(ListView.java:718)
01-30 10:56:16.886: E/AndroidRuntime(3420): at android.widget.ListView.fillFromTop(ListView.java:775)
01-30 10:56:16.886: E/AndroidRuntime(3420): at android.widget.ListView.layoutChildren(ListView.java:1646)
01-30 10:56:16.886: E/AndroidRuntime(3420): at android.widget.AbsListView.onLayout(AbsListView.java:1384)
01-30 10:56:16.886: E/AndroidRuntime(3420): at android.view.View.layout(View.java:7228)
01-30 10:56:16.886: E/AndroidRuntime(3420): at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
01-30 10:56:16.886: E/AndroidRuntime(3420): at android.view.View.layout(View.java:7228)
01-30 10:56:16.886: E/AndroidRuntime(3420): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254)
01-30 10:56:16.886: E/AndroidRuntime(3420): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1130)
01-30 10:56:16.886: E/AndroidRuntime(3420): at android.widget.LinearLayout.onLayout(LinearLayout.java:1047)
01-30 10:56:16.886: E/AndroidRuntime(3420): at android.view.View.layout(View.java:7228)
01-30 10:56:16.886: E/AndroidRuntime(3420): at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
01-30 10:56:16.886: E/AndroidRuntime(3420): at android.view.View.layout(View.java:7228)
01-30 10:56:16.886: E/AndroidRuntime(3420): at android.view.ViewRoot.performTraversals(ViewRoot.java:1145)
01-30 10:56:16.886: E/AndroidRuntime(3420): at android.view.ViewRoot.handleMessage(ViewRoot.java:1865)
01-30 10:56:16.886: E/AndroidRuntime(3420): at android.os.Handler.dispatchMessage(Handler.java:99)
01-30 10:56:16.886: E/AndroidRuntime(3420): at android.os.Looper.loop(Looper.java:130)
01-30 10:56:16.886: E/AndroidRuntime(3420): at android.app.ActivityThread.main(ActivityThread.java:3687)
01-30 10:56:16.886: E/AndroidRuntime(3420): at java.lang.reflect.Method.invokeNative(Native Method)
01-30 10:56:16.886: E/AndroidRuntime(3420): at java.lang.reflect.Method.invoke(Method.java:507)
01-30 10:56:16.886: E/AndroidRuntime(3420): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
01-30 10:56:16.886: E/AndroidRuntime(3420): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
01-30 10:56:16.886: E/AndroidRuntime(3420): at dalvik.system.NativeStart.main(Native Method)
What you are trying to do : create a resource programaticaly. You cannot do that. Android resources are created during compilation process. (basically xml files are processed/compiled by the SDK and that's how resources are created).
What you can try is this:
public class MyPreference extends Preference
onCreateView(ViewGroup parent)
to return the layout you need.And using this class like this:
Preference log_out = new MyPreference(this);
root.addPreference(log_out);
Here is a short example (assuming your are extending PreferenceActivity):
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
PreferenceScreen screen = getPreferenceManager().createPreferenceScreen(this);
PreferenceCategory category = new PreferenceCategory(this);
category.setTitle("category name");
screen.addPreference(category);
CheckBoxPreference checkBoxPref = new CheckBoxPreference(this);
checkBoxPref.setTitle("title");
checkBoxPref.setSummary("summary");
checkBoxPref.setChecked(true);
category.addPreference(checkBoxPref);
setPreferenceScreen(screen);
}
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