LoginSignup
13
13

More than 5 years have passed since last update.

ListPreferenceで設定してある内容を表示

Posted at

androidの設定でListPreferenceは割りとよく使われていると思います。
でも「設定してある内容がダイアログを開かないと見えない」っていう微妙なところがありますよね。
summaryに、設定してある内容を表示するってのはよく見かけますが、設定の変更をListenerで監視したりめんどくさい処理が多いですし、ListPreferenceにsummaryとして書きたかった内容は書けなくなります。そこでsummaryは維持しつつ右側に内容を表示しちゃおう!って話です。

実装

まずはListPreferenceを継承したPreferenceを作ります。

ParamReadableListPreference.java
public class ParamReadableListPreference extends ListPreference {

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public ParamReadableListPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public ParamReadableListPreference(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public ParamReadableListPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ParamReadableListPreference(Context context) {
        super(context);
    }

    @Override
    protected View onCreateView(ViewGroup parent) {
        setWidgetLayoutResource(R.layout.param_readable_list_preference);
        return super.onCreateView(parent);
    }

    @Override
    protected void onBindView(View view) {
        super.onBindView(view);
        TextView textView = (TextView)view.findViewById(R.id.valueView);
        textView.setText(getEntry());
    }
}

次にlayoutのxml

param_readable_list_preference.xml
<?xml version="1.0" encoding="utf-8"?>

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:layout_gravity="center"
    android:padding="16dip"
    android:id="@+id/valueView"/>

あとはPreferenceのxmlで

<?xml version="1.0" encoding="utf-8"?>

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <ListPreference
        android:key="key"
        android:defaultValue="sample-value1"
        android:title="sample"
        android:summary="summary"
        android:entries="@array/sample"
        android:entryValues="@array/sample_value" />

    <com.sample.ParamReadableListPreference
        android:key="key"
        android:defaultValue="sample-value1"
        android:title="sample"
        android:summary="summary"
        android:entries="@array/sample"
        android:entryValues="@array/sample_value" />
</PreferenceScreen>

でこんなかんじに。
Screenshot_2015-04-11-12-15-32.png Screenshot_2015-04-11-12-28-31.png Screenshot_2015-04-11-12-28-36.png

これで右側に設定内容が表示されます。
サマリーも表示出来ますし、コードもそれなりに綺麗にまとまりますし、表示もわりと綺麗です。

最後に

このサンプルの設定のxmlでは、keyを同値にしているのでListPreferenceの方の設定を変更すると、ParamReadableListPreference側の値の表示が変更されなければなりませんが、変更出来ません。ただしParamReadableListPreferenceで値を変更すれば表示も更新されますし、同じkeyを1つの画面で何度も使うような状況はそう無いので、そこまで細かい実装はしてありません。もし必要でしたらListenerをいい感じに実装する必要があるかと思います。

13
13
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
13
13