-1

I´m a beginner and would like to know which one of setImageBitmap vs setImageResource vs setImageDrawable is best practice for changing between two appearances (states) of an ImageButton in Recycler List View. Just like the clock symbol in Androids AlarmClock which has 2 different states and is located in an RecyclerView: I need 10 reputation to post images so here´s a website with a picture of an alarm list: https://www.pctipp.ch/androidtipp/artikel/android-coole-wecker-features-in-samsung-und-aehnlichen-smartphones-81200/

I already found an accepted answer for a similar case: setImageResource Vs setDrawable It says setImageResource may be the worst solution but if I take the other solutions my ImageButton has problems for example it loses its former size. What is more it looks like it has to be done a lot more for example "decodeResource". I think it´s not good for its performance because it´s executed for every row. I know that setImageDrawable is always called when setImageBitmap so I think setImageBitmap wouldn´t be the right answer.

This code is part of my onBindViewHolder in my RecyclerView´s Adapter:

if (alarmAtPosition.isActive()){
    //Bitmap bm = BitmapFactory.decodeResource(Resources.getSystem(), R.drawable.ic_access_alarms_black_24dp);
    Drawable myDrawable = Resources.getSystem().getDrawable(R.drawable.ic_access_alarms_black_24dp);
    viewHolder.imageButton.setImageDrawable(myDrawable);
//viewHolder.imageButton.setImageResource(R.drawable.ic_access_alarms_black_24dp);
}
else {
    //Bitmap bm2 = BitmapFactory.decodeResource(Resources.getSystem(), R.drawable.ic_settings_black_24dp);
    Drawable myDrawable2 = Resources.getSystem().getDrawable(R.drawable.ic_settings_black_24dp);
    viewHolder.imageButton.setImageDrawable(myDrawable2);
//viewHolder.imageButton.setImageResource(R.drawable.ic_settings_black_24dp);
}

In my row.xml I don´t need to put android:src="@drawable/ic_settings_black_24dp if I use setImageResource. So it would be a lot easier for me to take this solution but that accepted answer is making me uncertain. Please tell me: Which one is for best performance?

Tony
  • 11
  • 4

1 Answers1

0

I've got a better method to toggle images ( Only for 2 states ).

Create a drawable let's say favourite.xml:

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

    <item android:state_checked="true" android:drawable="@drawable/your_checked_drawable"/>
    <item android:drawable="@drawable/your_unchecked_drawable"/>

</selector>

Now wherever you've added the imageButton of which you want to change the image, replace that with this:

                <CheckBox
                    android:id="@+id/favourite"
                    android:button="@null"
                    android:checked="false"
                    android:clickable="true"
                    android:focusable="true"
                    android:background="@drawable/favourite"
                    android:layout_width="32dp"
                    android:layout_height="32dp" />

And you're done!

This basically changes the default checkbox drawables with your custom ones and there you've 2 states.

  1. checkbox.isChecked

  2. !checkbox.isChecked

Edit: You can attach any animation to the checkbox, one I'm using is:

Add this file named scale.xml under res/anim:

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

    <scale
        android:fromXScale="1.0"
        android:fromYScale="1.0"
        android:toXScale="0.7"
        android:toYScale="0.7"
        android:pivotX="50%"
        android:pivotY="50%"
        android:duration="200"
        android:repeatMode="reverse"
        android:repeatCount="1"/>

</set>

Then in your activity:

private lateinit var mAnimation: Animation
mAnimation = AnimationUtils.loadAnimation(this, R.anim.scale)
checkbox.setOnClickListener {
     it.startAnimation(mAnimation)
}

And there you go.

Kashish Sharma
  • 749
  • 8
  • 18
  • I tested it, seems like this is even a more efficient solution for my case. I just don't understand why I would need focusable in xml in my case even though I've read the answer in https://stackoverflow.com/questions/14049292/difference-between-focusable-and-clickable When does a user use a keyboard for android in recycler view / checkBox? – Tony Aug 04 '19 at 15:44
  • And is it possible to get the same animation (background becomes darker for a short period) on the Image with your option when the user presses the Button for a bit longer? – Tony Aug 04 '19 at 16:00
  • You can add various animations, it's all up to you. I've updated the answer with the animation I'm using in my project. Please accept the answer if you find it helpful. – Kashish Sharma Aug 04 '19 at 16:44