1

I am trying to implement this custom preference https://gist.github.com/JakeWharton/515681 but the preference does not show :-(((

enter image description here

EDIT EDIT EDIT EDIT

It turns out it works on Gingerbread but not on KitKat :-((((

PLEASE THE OTHER; NOT CUSTOM PREFERENCE, ARE SHOWING NORMALLY WITHOUT PROBLEMS...


This is MyPreferencesActivity:

public class MyPreferencesActivity extends PreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getPreferenceManager().setSharedPreferencesName("prefs");
addPreferencesFromResource(R.xml.prefs);

// add a validator to the "numberofCircles" preference so that it only
// accepts numbers
Preference circlePreference = getPreferenceScreen().findPreference("numberOfCircles");

// add the validator
circlePreference.setOnPreferenceChangeListener(numberCheckListener);
}
}

This is my prefs.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/frankandrobot.glwallpapervideodemo.com" >

<CheckBoxPreference
    android:key="touch"
    android:title="Enable Touch" >
</CheckBoxPreference>

<EditTextPreference
    android:key="numberOfCircles"
    android:title="Number of Circles" >
</EditTextPreference>

<frankandrobot.glwallpapervideodemo.com.IconCheckBoxPreference
        android:key="help"
        android:title="Ciao"
        app:icon="@drawable/icon" />

</PreferenceScreen>

This is the Custom preference:

package frankandrobot.glwallpapervideodemo.com;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.preference.CheckBoxPreference;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;


public class IconCheckBoxPreference extends CheckBoxPreference {
private Drawable mIcon;

public IconCheckBoxPreference(final Context context, final AttributeSet attrs, final int defStyle) {
    super(context, attrs, defStyle);

    this.setLayoutResource(R.layout.icon_checkbox_preference);

    this.mIcon = context.obtainStyledAttributes(attrs, R.styleable.IconPreference, defStyle, 0).getDrawable(R.styleable.IconPreference_icon);
}

public IconCheckBoxPreference(final Context context, final AttributeSet attrs) {
    this(context, attrs, 0);
}

@Override
protected void onBindView(final View view) {
    super.onBindView(view);

    final ImageView imageView = (ImageView)view.findViewById(R.id.icon);
    if ((imageView != null) && (this.mIcon != null)) {
        imageView.setImageDrawable(this.mIcon);
    }
}

/**
 * Sets the icon for this Preference with a Drawable.
 *
 * @param icon The icon for this Preference
 */
public void setIcon(final Drawable icon) {
    if (((icon == null) && (this.mIcon != null)) || ((icon != null) && (!icon.equals(this.mIcon)))) {
        this.mIcon = icon;
        this.notifyChanged();
    }
}

/**
 * Returns the icon of this Preference.
 *
 * @return The icon.
 * @see #setIcon(Drawable)
 */
public Drawable getIcon() {
    return this.mIcon;
}
}

This is my attrs.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="IconPreference">
    <attr name="icon" format="reference" />
</declare-styleable>
</resources>
Lisa Anne
  • 4,482
  • 17
  • 83
  • 157
  • You're using deprecated APIs. Read about PreferenceFragment. – Eng.Fouad Jan 29 '14 at 16:38
  • @Eng.Fouad Thanks Fouad, yes, but it MUST work even if I do not use the fragment – Lisa Anne Jan 29 '14 at 16:41
  • check this link http://developer.android.com/reference/android/preference/PreferenceFragment.html and try this example it will works on kitkat http://android-er.blogspot.in/2012/07/example-of-using-preferencefragment.html – rajahsekar Jan 29 '14 at 16:42
  • @rajahsekar thank you rajahsekar, but that is not the point, it MUST work without fragment! – Lisa Anne Jan 29 '14 at 16:55
  • TO work this you have to use isvalidfragment it is add in kitkat please check following links http://stackoverflow.com/questions/19973034/isvalidfragment-android-api-19 http://stackoverflow.com/questions/20868643/why-does-kit-kat-require-the-use-of-the-isvalidfragment – rajahsekar Jan 29 '14 at 17:00
  • @rajahsekar but I do not get any exception, if I do not use fragments then I do to have to use isvalidfragment. Additionally, other custom preferences work perfectly, therefore the problem must lie somewhere else – Lisa Anne Jan 30 '14 at 09:59
  • 1
    What does "does not show" mean? – CommonsWare Jan 30 '14 at 15:38
  • @CommonsWare thanks CommonsWare, please see the picture added. Thanks! – Lisa Anne Jan 30 '14 at 15:48
  • 1
    Have you inspected the activity using Hierarchy View? Are the widgets there (but are transparent, or black-on-black, or `View.INVISIBLE`)? Or are there no widgets at all? – CommonsWare Jan 30 '14 at 15:54
  • @CommonsWare I have, there is no widget: there are the widgets for the first two Preferences but the third is simply non existent, there is not widget at all for the third Preference – Lisa Anne Jan 30 '14 at 15:59
  • 1
    Hmmm... OK, let me see if I can reproduce the problem. I'll chime back in here once I have, with more comments or an answer. – CommonsWare Jan 30 '14 at 16:07
  • @CommonsWare thank you very much CommonsWare ! – Lisa Anne Jan 30 '14 at 16:11

1 Answers1

2

The simplest answer is: CheckBoxPreference, at least for Android 4.1+, supports android:icon, and so you do not need your own custom preference class.

The only reason for my hedging on 4.1 is that I just haven't tried it on anything older.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • thank you, I will try and let you know. Thanks again. – Lisa Anne Jan 30 '14 at 18:49
  • thanks that was the issue! But the problem remains that `android:icon`is not supported on Gingerbread. – Lisa Anne Feb 01 '14 at 18:14
  • @LisaAnne: Then use two versions of your preference XML. In `res/xml-v11/`, put a file that uses `android:icon`. In `res/xml/`, put a file that uses `IconCheckBoxPreference`. – CommonsWare Feb 01 '14 at 18:18