[Novice Wang] I want to change the background of the button selected under certain conditions

Asked 2 years ago, Updated 2 years ago, 136 views

When selecting any one of the 29 buttons under the condition that n=1 is performed during multiple actions,

If the background of the selected button is A, I would like to change it to B.


for(i in 0..28) {
            var name: Int = getResources().getIdentifier("imageButton" + i, "id", getPackageName())
            var bgi: Int = getResources().getIdentifier("imageButton" + i, "background", getPackageName())
            if (n == 1) {
                (findViewById<ImageButton>(name)).setOnClickListener {
                    if (bgi == R.drawable.A) {
                        (findViewById<ImageButton>(name)).setBackgroundResource(R.drawable.B)
                    }
                }
            }
        }

I made a command sentence like this, and it doesn't work even if you put it in override funonCreate()

I can't put it in another function that works on n=1 day, but did I make a wrong statement?ㅜ <

Please advise me how to change it

android studio kotlin imagebutton

2022-09-22 19:58

1 Answers

var bgi: Int = getResources().getIdentifier("imageButton" + i, "background", getPackageName())

I think you tried to get background id of ImageButton in the above code, but getIdentifier() is the parameter R.id.something to find the name in the and .string. Therefore, the background id cannot be obtained with the above code.

First, check the description of getIdentifier() here .

Then we need another way. When you specify the android:background property in ImageButton, a Drawable object is created internally for that resource. If you specified an image file, BitmapDrawable will be generated, and if you created customDrawable with layer-list, LayerDrawable object will be created.

The problem is that there is no proper way to get the resource id from the Drawable object. Therefore, I would like to customize ImageButton and reply to your inquiry.

First, we will not use the android:background property, but create and use a custom property. It is written as below.

1. res/values/attrs.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="CustomImageButton">
        <attr name="backgroundResource" format="integer" />
    </declare-styleable>
</resources>

2.Create Custom ImageButton

class CustomImageButton(context: Context, attrs: AttributeSet) : ImageButton(context, attrs) {

    private var backgroundResourceId = View.NO_ID

    init {
        //custom attrs load
        context.theme.obtainStyledAttributes(
                attrs,
                R.styleable.CustomImageButton,
                0, 0).apply {

            try {
                val bgResId = getResourceId(R.styleable.CustomImageButton_backgroundResource, 0)
                if (bgResId != 0) {
                    setBackgroundResource(bgResId)
                }
            } } finally {
                recycle()
            }
        }
    }

    override fun setBackgroundResource(resid: Int) {
        super.setBackgroundResource(resid)
        backgroundResourceId = resid
    }

    fun sameCheck(checkeResId: Int) = backgroundResourceId == checkeResId
}

This allows you to specify drawable id as namespace:backgroundResource property when creating layout. Also override the setBackgroundResource() and put the backgroundResId value in the variable. You can then see which id's drawable resource is defined in the corresponding ImageButton and compare whether the sameCheck() is the currently defined drawable resource id.

3.layout creation

I added 6 ImageButtons. Each id is imageButton(0~5) and uses the background resource property created above. I used namespace called app, but actually namespace is okay to use something else. In addition, ic_launcher_background was designated as background.

<?xml version="1.0" encoding="utf-8"?>

<filysoft.buttontest.CustomImageButton
    android:id="@+id/imageButton0"
    ...
    app:backgroundResource="@drawable/ic_launcher_background" />

<filysoft.buttontest.CustomImageButton
    android:id="@+id/imageButton1"
    ...
    app:backgroundResource="@drawable/ic_launcher_background" />

<filysoft.buttontest.CustomImageButton
    android:id="@+id/imageButton2"
    ...
    app:backgroundResource="@drawable/ic_launcher_background" />

<filysoft.buttontest.CustomImageButton
    android:id="@+id/imageButton3"
    ...
    app:backgroundResource="@drawable/ic_launcher_background" />

<filysoft.buttontest.CustomImageButton
    android:id="@+id/imageButton4"
    ...
    app:backgroundResource="@drawable/ic_launcher_background" />

<filysoft.buttontest.CustomImageButton
    android:id="@+id/imageButton5"
    ...
    app:backgroundResource="@drawable/ic_launcher_background" />

4. Finally, write MainActivity as follows.

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    for (i in 0..5) {
        val btnId = resources.getIdentifier("imageButton$i", "id", packageName)
        findViewById<View>(btnId).setOnClickListener(imageBtnClickListener)
    }
}

private val imageBtnClickListener = View.OnClickListener {
    (it as CustomImageButton).apply {
        if (n == 1) {
            if (sameCheck(R.drawable.ic_launcher_background)) {
                setBackgroundResource(R.drawable.ic_launcher_foreground)
            }
        }
    }
}

Compare drawables with sameCheck() when n == 1. I compared whether it is the same as ic_launcher_background, and if it is the same, I wrote to change it to ic_launcher_foreground.

If button is created dynamically, or if there are only a few types of background used, I think we can also think about using the tag in view. I didn't know the exact situation of Jaguar, so I wrote it in the above way. There can be many other ways.

There may be some mistakes in improvising, but I think it will be helpful if you understand the approach and flow.


2022-09-22 19:58

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.