javaandroidsettingspreference

How to create a simple settings menu in android? (with xmls & activities files)


I want to create a settings menu in my android app. I tried different ways to create a simple settings menu, but all of them had a lot of errors or were not working properly. Can anyone help me?


Solution

  • First create a new project. To show you how this could be done paste this into your main launcher activity:

        <?xml version="1.0" encoding="utf-8"?>
    <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
    
        <PreferenceCategory android:title="Settings" >
            <EditTextPreference
                    android:title="Password"
                    android:summary="Set Your Password"
                    android:key="prefUserPassword"/>
        </PreferenceCategory>
    
        <PreferenceCategory android:title="Security Settings" >
            <CheckBoxPreference
                android:defaultValue="false"
                android:key="prefLockScreen"
                android:summary="Lock The Screen With Password"
                android:title="Screen Lock" >
            </CheckBoxPreference>
    
            <ListPreference
                android:key="prefUpdateFrequency"
                android:title="Reminder for Updation"
                android:summary="Set Update Reminder Frequency"
                android:entries="@array/updateFrequency"
                android:entryValues="@array/updateFrequencyValues"
                />
        </PreferenceCategory>
    
    </PreferenceScreen>
    

    Since we have used arrays here, We need to define it inside array.xml file. Create an arrays.xml file inside values folder and write following

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <string-array name="updateFrequency">
            <item name="1">Daily</item>
            <item name="7">Weekly</item>
            <item name="3">Yearly</item>
            <item name="0">Never(I will Myself) </item>
        </string-array>
        <string-array name="updateFrequencyValues">
            <item name="1">1</item>
            <item name="7">7</item>
            <item name="30">30</item>
            <item name="0">0</item>
        </string-array>
    
    </resources>
    

    This is purely a sample taken from here