gregkorossy / Android-Support-Preference-V7-Fix

Android androidx.preference support library has some issues, this lib tries to fix them.
https://discord.gg/87NVsSK
Apache License 2.0
497 stars 46 forks source link

setIcon works weirdly #76

Closed anonym24 closed 7 years ago

anonym24 commented 7 years ago

Instead of android:icon="@drawable/test" I tried this:

findPreference("test").setIcon(getDrawable(R.drawable.test))

private Drawable getDrawable(int id) {
        return ContextCompat.getDrawable(mContext, id);
    }

and it was working, it even was working with Vector Drawable (.xml images) because adding vector drawable in xml didn't work:

app:srcCompat="@drawable/test" (for .xml images)

only this method (programmatically) helped me to set vector drawable for a preference

But when I later run my app and opened the settings, icons weren't there anymore. I tried to build my project again and now it just doesn't want to work, only works with xml android:icon="@drawable/test". I don't really get what is going on)

So now setIcon method doesn't even work with ordinary png image for me:

    @Override
    public void onCreatePreferencesFix(Bundle bundle, String s) {
        addPreferencesFromResource(R.xml.preferences);
        ...
        // set icons
        findPreference("test").setIcon(getDrawable(R.drawable.test)); 
        ...
    }

What could be a problem?

anonym24 commented 7 years ago

Oh I found the problem, I commented in xml one of my preference (SeekBarPreference one) but I didn't remove code in my preference fragment: SeekBarPreference seekBarPreference = (SeekBarPreference) findPreference("myseekbar");

and it just broke everything. interesting that app doesn't crash if it can't find the preference, it just breaks everything and doesn't execute some code in onCreatePreferencesFix method. really hard to debug app)

so now findPreference("test").setIcon(getDrawable(R.drawable.test)); seems to be working again

also interesting that

SwitchPreferenceCompat test = (SwitchPreferenceCompat) findPreference("test");
        test.setIcon(ContextCompat.getDrawable(getActivity(), R.drawable.test));

doesn't work, and code after these lines also aren't being executed.

Google shocks me)