mardi 1 mars 2016

How to use custom CheckBox in PreferenceScreen on Android

I want use PreferenceScreen in my application for set some of settings. I've customized this page, and set layout for any preferences. all of preferences is OK, but CheckBoxPreferences, and when I click on it does not show any reaction, this is ture in every time. i can set is false!!! i've set android:id="@+android:id/checkbox" for custom CheckBox.
CheckBox custom xml :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://ift.tt/nIICcg"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#fff">

    <TextView
        android:id="@android:id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_marginRight="16dp"
        android:layout_marginTop="5dp"
        android:gravity="right"
        android:textColor="#000"
        android:textSize="18sp" />

    <TextView
        android:id="@android:id/summary"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_below="@+android:id/title"
        android:layout_marginRight="16dp"
        android:layout_marginTop="5dp"
        android:gravity="right"
        android:textColor="#c0c0c0"
        android:textSize="13sp" />

    <CheckBox
        android:id="@+android:id/checkbox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_margin="16dp"/>

</RelativeLayout>

PreferenceSreen xml :

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://ift.tt/nIICcg"
    xmlns:app="http://ift.tt/GEGVYd"
    xmlns:example="http://ift.tt/GEGVYd"
    xmlns:sample="http://ift.tt/GEGVYd">

        <CheckBoxPreference
            android:defaultValue="true"
            android:key="setting_main_subtitle_show"
            android:layout="@layout/pref_checkbox_layout"
            android:summary="Summary Text"
            android:title="Title" />

</PreferenceScreen>

AppPreference java code :

public class AppPreference {
    private SharedPreferences sharedPreferences;
    private SharedPreferences.Editor editor;
    private Context context;

    private static final String KeyShowSubTitle = "setting_main_subtitle_show";

    public AppPreference(Context context){
        this.sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
        this.editor = sharedPreferences.edit();
        this.context = context;
    }

    public Boolean getShowSubTitle(){
        return sharedPreferences.getBoolean(KeyShowSubTitle, true);
    }
}

MainPage java code :

Boolean show_subTitle = appPreference.getShowSubTitle();
if (show_subTitle == true){
    /// SubTitle Text
    toolbar.setSubtitle(appPreference.getSubTitleText());
} else {
    toolbar.setSubtitle("");
}

How can i fix this problem and use custom CheckBox in PreferenceScreen ?




Aucun commentaire:

Enregistrer un commentaire