简体   繁体   中英

How can I have reference for drawableTop of a Button?

I declared a drawable inside my Button and I want to get a reference for that because I created a method where I display a Dialog, extract the text and drawable of that button and use it to change the text and icon of a dialog dynamically.

Please do check this snippet:

private fun showProductDialog(button : Button){
        //extract product info selected from button

        //show dialog
        val dialog = Dialog(this)
        dialog.setContentView(R.layout.dialog_product)
        //set text and icon
        val titleLayout = dialog.findViewById<LinearLayout>(R.id.ll_product_label)
        val titleText = titleLayout.findViewById<HelveticaBoldTextView>(R.id.tv_category_click)
        val titleIcon = titleLayout.findViewById<ImageView>(R.id.iv_product_icon)

        //get text of button, set to titleText
        titleText.text = button.text.toString().uppercase()

        //extract drawableTop of Button then
        //set image for titleIcon = drawable in Button
        
        dialog.show()
    }

To get DrawableTop you can use this

 Drawable[] drawables = button.getCompoundDrawables();

 Drawable d = drawables[1];

To set DrawableTop you can use this

 button.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.icon, 0, 0);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM