how to change background image of button when clicked/focused?

后端 未结 8 1072
旧时难觅i
旧时难觅i 2020-11-30 01:00

I want to change the background image of a button when clicked or focused.

This is my code:

Button tiny = (Button)findViewById(R.id.tiny);
tiny.setOn         


        
8条回答
  •  眼角桃花
    2020-11-30 01:34

    To change the button background we can follow 2 methods

    1. In the button OnClick, just add this code:

       public void onClick(View v) {
           if(v == buttonName) {
              buttonName.setBackgroundDrawable
               (getResources().getDrawable(R.drawable.imageName_selected));
            }
      
             }
      

      2.Create button_background.xml in the drawable folder.(using xml)

      res -> drawable -> button_background.xml

         
          
      
                
                
               
          
      

      Now set the above file in button's background file.

           

      2nd method is better for setting the background fd button

提交回复
热议问题