0. Result
1. Add the gradle dependency for the support preference fragment
app/build.gradle
1 2 3 | dependencies { compile 'com.android.support:preference-v7:26.1.0' } | cs |
2. Create the pref_settings in res/xml and
Add a CheckBoxPreference to the preference
values/strings.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <resources> <!-- Label for the show text1 checkbox preference --> <string name="pref_show_text1_label">Show Text1</string> <!-- Key name for storing text1 in SharedPreferences --> <string name="pref_show_text1_key" translatable="false">show_text1</string> <!-- Label for the show text2 checkbox preference --> <string name="pref_show_text2_label">Show Text2</string> <!-- Key name for storing text2 in SharedPreferences --> <string name="pref_show_text2_key" translatable="false">show_text2</string> <!-- Summary for Checkboxes --> <string name="pref_show_true">Shown</string> <string name="pref_show_false">Hidden</string> </resources> | cs |
values/bools.xml
1 2 3 4 5 | <?xml version="1.0" encoding="utf-8"?> <resources> <bool name="pref_show_text1_default">true</bool> <bool name="pref_show_text2_default">false</bool> </resources> | cs |
xml/pref_settings
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <?xml version="1.0" encoding="utf-8"?> <!-- 계층적인 구조를 만들고 싶을 경우, preferenceScreen 태그 안에 preferenceScreen 태그를 중복해서 사용하면 된다.--> <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> <!-- defaultValue: 기본적으로 설정할 값 summary: 체크박스가 어떤 역할을 하는지 유저에게 설명한다. 여기선 summaryOn 일때 보이게, summaryOff 일때 안보이게 설정 --> <CheckBoxPreference android:defaultValue="@bool/pref_show_text1_default" android:key="@string/pref_show_text1_key" android:summaryOff="@string/pref_show_false" android:summaryOn="@string/pref_show_true" android:title="@string/pref_show_text1_label" /> <CheckBoxPreference android:defaultValue="@bool/pref_show_text2_default" android:key="@string/pref_show_text2_key" android:summaryOff="@string/pref_show_false" android:summaryOn="@string/pref_show_true" android:title="@string/pref_show_text2_label" /> </PreferenceScreen> | cs |
3. Create the SettingsFragment class that extends PreferencesFragmentCompat and
Call add PreferencesFromResource method to add the preference file
SettingsFragment.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | package com.tistory.qlyh8.preferences; /* * Created by YUNHEE on 2018-01-03. */ import android.os.Bundle; import android.support.v7.preference.PreferenceFragmentCompat; public class SettingsFragment extends PreferenceFragmentCompat { @Override public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { addPreferencesFromResource(R.xml.pref_settings); } } | cs |
4. Set the root layout of activity_settings to the newly created SettingsFragment
layout/activity_settings.xml
1 2 3 4 5 6 | <?xml version="1.0" encoding="utf-8"?> <fragment xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/activity_settings" android:name="com.tistory.qlyh8.preferences.SettingsFragment" android:layout_width="match_parent" android:layout_height="match_parent"/> | cs |
5. Add a preference theme to styles.xml
values/styles.xml
1 2 | <!-- PreferenceFragmentCompat 라이브러리를 사용하기 때문에 추가 --> <item name="preferenceTheme">@style/PreferenceThemeOverlay</item> | cs |
Android Course of Udacity - Lesson 6
'Android' 카테고리의 다른 글
Preferences - (5) List Preference (0) | 2018.01.04 |
---|---|
Preferences - (4) sharedPreference 에서 값 가져오기 (0) | 2018.01.04 |
Preferences - (2) Settings 메뉴 틀 만들기 (0) | 2018.01.03 |
Preferences - (1) Data Persistence & Preference Fragment (0) | 2018.01.03 |
Intent - (3) Implicit Intent를 코드에 적용하기 (0) | 2018.01.01 |