android change image button background

后端 未结 4 1433
我寻月下人不归
我寻月下人不归 2021-01-13 18:24

I cannot seem to change the background image of my image button. Heres the code i\'m currently trying to use:

ImageButton imgButton = (ImageButton) findViewB         


        
相关标签:
4条回答
  • 2021-01-13 18:26

    Use this method:

    imgButon.setBackground(getActivity().getDrawable(R.drawable.your_icon));
    
    0 讨论(0)
  • 2021-01-13 18:34

    in xml file, <Button> write: android:backgroud="@drawable/your_file"

    0 讨论(0)
  • 2021-01-13 18:36

    Make sure you are on same activity. If you are changing background of different activity first create the constructor then use object to change button.

    Activity obj= new activity();
    obj.imgButton.setBackgroundResource(R.drawable.tab2_selected);
    

    and also check that oncreate() method has void return type if you are using only findviewbyid.

    0 讨论(0)
  • 2021-01-13 18:44

    For solving this question you should implement

    imgButton.setImageResource(R.drawable. tab2_selected);
    
    0 讨论(0)
提交回复
热议问题