PreferenceFragment buttons (Preference) not enable but CheckBoxPreference work

544 views Asked by At

I made an SettingsActivity with the android preferences.

In my SettingsActivity I extend SherlockFragmentActivity, so I decided to use PreferenceFragment.

I made a custom class that extend PreferenceFragment, called GenericPreferenceFragment, that load my preference xml from resource.

The preferences are 2 buttons and 2 checkboxes.

All works good in my Nexus with last android update, but in the samsung with Android 4.2 the buttons don't seem enabled (they are gray)!

Here a screen:

android < 4.4 buttons not enabled

Why do that? How can I fix this?


Code:

Here is part of my com.xxx.yyy.SettingsActivity:

public class SettingsActivity extends SherlockFragmentActivity implements
        OnPreferenceClickListener, OnClickListener {

    private final String TAG = "SETTINGS";
    private ImageButton homeButton;
    private ImageButton settingsButton;
    private TextView activityTitle;

    private ProgressDialog pDialog;
    private GenericPreferenceFragment gPrefFragment;

    private CheckBoxPreference cbpGCM;
    private CheckBoxPreference cbpMail;
    private Preference pKeywords;
    private Preference pLogout;



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        return super.onOptionsItemSelected(item);
    }


    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);

        setupActionBar();
        gPrefFragment = (GenericPreferenceFragment) getFragmentManager().findFragmentById(R.id.pref_frag_id);

        FontsOverride.setDefaultFont(getApplicationContext(), "DEFAULT",
                "OpenSans-Regular.ttf");
        layoutInitializer();
    }

    // @Override
    // public void onBuildHeaders(List<Header> target) {
    // loadHeadersFromResource(R.xml.preference_headers, target);
    // }

    private void setupActionBar() {
        getSupportActionBar();
        getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
        getSupportActionBar().setCustomView(R.layout.custom_abs_layout);
    }

    private void layoutInitializer() {
        pDialog = DialogUtil.makeDialog(SettingsActivity.this);

        // --- ACTIONBAR ---
        homeButton = (ImageButton) findViewById(R.id.button_menu_home);
        settingsButton = (ImageButton) findViewById(R.id.button_menu_settings);
        activityTitle = (TextView) findViewById(R.id.tv_activity_title);
        activityTitle.setText(R.string.title_settings);

        homeButton.setBackgroundResource(0);
        settingsButton.setBackgroundResource(0);
        homeButton.setImageDrawable(getResources().getDrawable(R.drawable.ic_return));


        cbpGCM = (CheckBoxPreference)  gPrefFragment.findPreference("pref_device_notification");
        cbpMail = (CheckBoxPreference)  gPrefFragment.findPreference("pref_mail_notification");
        pKeywords = (Preference)  gPrefFragment.findPreference("pref_keyword");
        pLogout = (Preference)  gPrefFragment.findPreference("pref_logout");

        pKeywords.setOnPreferenceClickListener(this);
        pLogout.setOnPreferenceClickListener(this);
        cbpMail.setOnPreferenceClickListener(this);
        cbpGCM.setOnPreferenceClickListener(this);

        homeButton.setOnClickListener(this);


        // set button on/off
          ...
        cbpGCM.setChecked(gcmStatus);
        cbpMail.setChecked(mailStatus);
    }

    @Override
    public boolean onPreferenceClick(Preference preference) {
        String key = preference.getKey();
        if (key.equalsIgnoreCase("pref_keyword")) {
            finish();
            startActivity(...);
        } else if (key.equalsIgnoreCase("pref_logout")) {
            getLogout();
        }else if (key.equalsIgnoreCase("pref_device_notification")) {
            boolean status = cbpGCM.isChecked();
            switchGCM(status);
        } else if (key.equalsIgnoreCase("pref_mail_notification")) {
            boolean status = cbpMail.isChecked();
            switchMail(status);
        }

        return false;
    }
}

This is R.layout.activity_settings:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_margin="10dp"
        android:background="#"
        android:orientation="vertical" >

        <fragment
            android:id="@+id/pref_frag_id"
            android:name="com.xxx.yyy.GenericPreferenceFragment"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />
    </LinearLayout>

 </LinearLayout>

This is my com.xxx.yyy.GenericPreferenceFragment:

public class GenericPreferenceFragment extends PreferenceFragment {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preferences);

    }
}

And this is the R.xml.preferences:

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#" >

    <PreferenceCategory
        android:key="pref_keyword_settings"
        android:title="@string/label_keyword_settings" >
        <Preference
            android:dependency="pref_keyword_settings"
            android:icon="@drawable/ic_keyword_settings"
            android:key="pref_keyword"
            android:summary="@string/description_keyword_preference"
            android:title="@string/title_keyword_preference" />
    </PreferenceCategory>


    <PreferenceCategory
        android:key="pref_notification_settings"
        android:title="@string/label_notification_settings" >
        <CheckBoxPreference
            android:defaultValue="true"
            android:icon="@drawable/ic_notification_settings"
            android:key="pref_device_notification"
            android:summary="@string/description_device_notification"
            android:title="@string/title_device_notification" />
        <CheckBoxPreference
            android:defaultValue="true"
            android:icon="@drawable/ic_mail_settings"
            android:key="pref_mail_notification"
            android:summary="@string/description_mail_notification"
            android:title="@string/title_mail_notification" />
    </PreferenceCategory>


    <PreferenceCategory
        android:key="pref_logout_settings"
        android:title="@string/label_logout_settings" >
        <Preference
            android:dependency="pref_logout_settings"
            android:icon="@drawable/ic_logout"
            android:key="pref_logout"
            android:enabled="true"
            android:summary="@string/description_logout_preference"
            android:title="@string/title_logout_preference" />
    </PreferenceCategory>

</PreferenceScreen>
1

There are 1 answers

0
Tenaciousd93 On

After some attemps I've got solution: I have to remove android:dependency = "pref_keyword_settings" and android:dependency = "pref_logout_settings"from R.xml.preferences. With the dependency Preferences.isEnable() result was always false!