androidsharedpreferencesandroid-preferenceslistpreferencedialog-preference

How to make a custom Dialog Preference inside Preference


I searched all Around this forum , but not getting what i exactly need. I need a Custom DialogPreference in Preference but that DialogPreference shouldn't have blue-strip header which i hate, And i already prepared one activity header template xml file for other activities which can be used as custom activity header.so i want to use that on this dialogPrerence. Plus i want Custom Preference File name but here the problem is it creates two Preference File name, one for Preference and Another for DialogPreference

but i found something like this here Using EditTextPreference with 2 user input fields

    <com.yourdomain.YourDialogPreference
        android:title="Title"
        android:summary="Summary"
        android:key="dialog_preference"/>

i 've done so far with this. DialogPreference Opens well, but how can i attach my header template to this custom DialogPreference


Solution

  • I figured out my self. Here you go.

    1st include following line for the Header Template in the DialogPreference XML

    <include layout="@layout/activity_header_template" />
    

    and prepare own custom dialog layout just like normal custom dialog Template. The real need is, i want to customize the DialogPreference, I want two inputs for Password 1 and Password 2. (just to confirm the password)

    This is my ListPreference XML code

    <?xml version="1.0" encoding="utf-8"?>
    <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    
        <PreferenceCategory android:title="@string/preference_header_encryption">
    
            <CheckBoxPreference
                android:key="prefkey_use_passcode"
                android:title="@string/preference_name_set_passcode"
                android:summary="@string/preference_summary_set_passcode" />
    
            <!-- This is how you need to attach CustomDialogPrefernce, by using the class name -->
            <!-- Please ignore title here. Title will come from DialogPreference Constructor --> 
            <com.nerds.notes.SettPassword
                android:key="prefkey_set_passcode"
                android:summary="@string/preference_app_protection"
                android:dialogMessage="@string/action_delete"
                android:positiveButtonText="@string/passcode_ok_button_text"
                android:negativeButtonText="@string/passcode_cancel_button_text"
                android:dependency="prefkey_use_passcode" />
    
            <CheckBoxPreference
                android:key="prefkey_app_protection"
                android:title="@string/preference_app_protection"
                android:summary="@string/preference_summary_app_protection"
                android:dependency="prefkey_use_passcode" />
    
        </PreferenceCategory>
    
    </PreferenceScreen>
    

    Following lines are very Important, the DialogPreference Constructor

    public SettPassword(Context context, AttributeSet attrs) {
        super(context, attrs);
        setPersistent(false);
        setTitle(R.string.preference_name_set_passcode); // This will override ListPreference Title
        setDialogLayoutResource(R.layout.passcode_set_dialog_template);
    }
    

    Following lines should be coded in the ListPreference OnCreate Method to have custom Preference File name

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        PreferenceManager manager = getPreferenceManager();
        manager.setSharedPreferencesName("Your Preference File Name");
        manager.setSharedPreferencesMode(MODE_PRIVATE);
    
        addPreferencesFromResource(R.xml.settings); // ListPreference XML file from XML Folder
    }