기록

androidStudio/kotlin/preference 본문

Moblie/Android

androidStudio/kotlin/preference

youngyin 2022. 5. 26. 10:00

index

(1) 의존성 주입 build.gradle (:app)

(2) SettingsActivity

(3) 화면구성 res/xml/root_preferences.xml

(4) add onClick listener

code

(1) 의존성 주입 build.gradle (:app)

plugins {...}

android {...}

dependencies {
   ...

    // preference
    implementation 'androidx.preference:preference-ktx:1.2.0'
}

(2) add SettingsFragment

class SettingsFragment : PreferenceFragmentCompat() {
    override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
        setPreferencesFromResource(R.xml.root_preferences, rootKey)
    }
}

new>fragment/activity>Gallery.. 에서 선택하여 생성하는 것도 가능하다.

(3) 화면구성 res/xml/root_preferences.xml

<PreferenceScreen xmlns:app="http://schemas.android.com/apk/res-auto">

    <PreferenceCategory
        app:title="@string/messages_header"
        app:iconSpaceReserved="false">

        <Preference
            app:key="feedback"
            app:title="feedback"
            app:iconSpaceReserved="false"/>

        <EditTextPreference
            app:key="signature"
            app:title="@string/signature_title"
            app:useSimpleSummaryProvider="true"
            app:iconSpaceReserved="false"/>

        <ListPreference
            app:defaultValue="reply"
            app:entries="@array/reply_entries"
            app:entryValues="@array/reply_values"
            app:key="reply"
            app:title="@string/reply_title"
            app:useSimpleSummaryProvider="true"
            app:iconSpaceReserved="false" />

    </PreferenceCategory>

    <PreferenceCategory
        app:icon="@drawable/adv_ic_info"
        app:title="@string/sync_header">

        <SwitchPreferenceCompat
            app:key="sync"
            app:title="@string/sync_title" />

        <SwitchPreferenceCompat
            app:dependency="sync"
            app:key="attachment"
            app:summaryOff="@string/attachment_summary_off"
            app:summaryOn="@string/attachment_summary_on"
            app:title="@string/attachment_title" />

    </PreferenceCategory>
</PreferenceScreen>

(4) add onClick listener

class SettingsFragment : PreferenceFragmentCompat() {
    override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
        setPreferencesFromResource(R.xml.root_preferences, rootKey)
    }

    override fun onPreferenceTreeClick(preference: Preference): Boolean {
        when (preference.key){
            "feedback" -> { // todo : sendEmail() }
        }
        return super.onPreferenceTreeClick(preference)
    }
}

keyword

  • app:iconSpaceReserved="false"

PreferenceFragmentCompat has padding on PreferenceCategory that I can't get rid of

  • preference

https://developer.android.com/guide/topics/ui/settings?hl=ko 

  • xmlns:app="http://schemas.android.com/apk/res-auto"
    • xml에서의 import 기능
    • 해당 위치에 있는 라이브러리를 각각 임의의 변수에 담아서 사용하겠다는 의미
    • app은 변수명

result

reference

2020.09.29 - [안드로이드/예제] - androidStudio/java/preference:1.1.1 사용하기

Comments