Try something like:
package com.justinbuser.preferences;
import android.preference.PreferenceActivity;
import android.preference.PreferenceScreen;
import android.os.Bundle;
public class DynamicSettingsActivity extends PreferenceActivity{
public DynamicSettingsActivity(){
super();
}
@Override
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
PreferenceScreen defaultRoot = getPreferenceManager().createPreferenceScreen(this);
defaultRoot.setKey("root");
PreferenceScreen videoScreen = getPreferenceManager().createPreferenceScreen(this);
videoScreen.setTitle(R.string.video_chooser_title);
videoScreen.setSummary(R.string.video_chooser_summary);
videoScreen.setKey(getString(R.string.video));
//The following MUST be called before creating videoPref
defaultRoot.addPreference(videoScreen);
videoPref = new VideoChooserPreferenceCategory(mContext, videoScreen);
videoPref.setTitle(R.string.video_chooser_dialog_title);
videoPref.setSummary(R.string.video_chooser_dialog_summary);
videoScreen.addPreference(videoPref);
setPreferenceScreen(defaultRoot);
}
}
With a custom preference category like:
package com.justinbuser.preferences;
import android.content.Context;
import android.preference.Preference;
import android.preference.PreferenceCategory;
import android.preference.PreferenceScreen;
import android.preference.Preference.OnPreferenceClickListener;
public class VideoChooserPreferenceCategory extends PreferenceCategory implements OnPreferenceClickListener {
private PreferenceScreen parentScreen;
private final Context mContext;
public VideoChooserPreferenceCategory(Context context) {
super(context);
mContext = context;
}
public VideoChooserPreferenceCategory(Context context, PreferenceScreen preferenceScreen) {
super(context);
parentScreen = preferenceScreen;
mContext = context;
}
@Override
protected void onAttachedToActivity() {
if(this.getPreferenceCount() > 0)
{
this.removeAll();
}
Preference videoPref = new Preference(mContext);
videoPref.setKey(videoIds[videoId]);
videoPref.setTitle("Video Title");
videoPref.setSummary("Video Description");
videoPref.setOnPreferenceClickListener(this);
addPreference(videoPref);
super.onAttachedToActivity();
}
public boolean onPreferenceClick(Preference preference) {
parentScreen.getDialog().dismiss();
this.callChangeListener(preference.getKey());
return true;
}
}