LoginSignup
1
1

More than 3 years have passed since last update.

【Kotlin】AndroidxのPreferenceFragmentCompatを使ってみる

Posted at

はじめに

今回も例によって初学者向けの内容になっているかと思います。私自身が初学者である為、理解の浅い所や間違いなどあるかも知れません。都度ご指摘頂けると嬉しいです。

さて、Androidには昔からアプリの設定画面を簡単に作る仕組みがあります。ググれば記事は沢山見つかるのですが情報が古くなっているものも多かったので、Androidxライブラリでの実装をKotlinで書いてみようと思います。

準備

Androidxライブラリを使うため、build.gradle(appのほう)のdependencies内に記述が必要です。

build.gradle(Module.app)
dependencies{

        implementation 'androidx.preference:preference:1.1.1'//これを追加
}

構成

先に必要なファイルを挙げておきます。
-SettingActivity.kt
-activity_setting.xml
-SettingFragment.kt
-preference.xml

レイアウト

activity_setting.xml
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".SettingActivity">

    <FrameLayout
        android:id="@+id/settings_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

constraintlayoutのところはlinearでもなんでも平気だと思います。

SettingActivity

今回はフラグメントを表示するだけのシンプルなアクティビティです。
メインアクティビティなどからこのアクティビティに遷移すればオッケーです。

SettingActivity.kt
class SettingActivity: AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_setting)

        supportFragmentManager
                .beginTransaction()
                .replace(R.id.settings_container,fragment)
                .commit()
    }
}

SettingFragmentの実装

SettingFragment.kt
class SettingFragment: PreferenceFragmentCompat() {

    override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
        setPreferencesFromResource(R.xml.preferences,rootKey)
    }
}

preference.xmlを作成する

まずresフォルダ内にxmlフォルダを作る(なければ)。xmlフォルダ内にpreference.xmlファイルを作成する。

preference.xml
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

    <PreferenceCategory android:title="@string/category">
        <CheckBoxPreference
            android:key="key1"
            android:title="@string/title1"
            android:summary="summary1"
            android:summaryOn="@string/on"
            android:summaryOff="@string/off"
            android:defaultValue="false"
            />
        <EditTextPreference
            android:key="key2"
            android:title="@string/title2"
            android:summary="@string/summary2"
            android:dialogTitle="@string/dialog"
            />
        <ListPreference
            android:key="key3"
            android:title="@string/list"
            android:summary="@string/summary3"
            android:entries="@array/list"
            android:entryValues="@array/value"
            />
    </PreferenceCategory>
</PreferenceScreen>

PreferenceScreenで括ってある以外はこれまでのPreferenceと殆ど変わりません。
中身についての説明は割愛しますので必要に応じて調べてみていただければと思います。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1