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

后端 未结 8 1046
旧时难觅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:30

    Sorry this is wrong.

    For changing background color/image based on the particular event(focus, press, normal), you need to define a button selector file and implement it as background for button.

    For example: button_selector.xml (define this file inside the drawable folder)

    <?xml version="1.0" encoding="utf-8"?>
     <selector xmlns:android="http://schemas.android.com/apk/res/android">
         <item android:state_pressed="true"
               android:color="#000000" /> <!-- pressed -->
         <item android:state_focused="true"
               android:color="#000000" /> <!-- focused -->
         <item android:color="#FFFFFF" /> <!-- default -->
     </selector>
    
        <!-- IF you want image instead of color then write 
    android:drawable="@drawable/your_image" inside the <item> tag -->
    

    And apply it as:

     <Button
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:drawable="@drawable/button_selector.xml" />
    
    0 讨论(0)
  • 2020-11-30 01:30

    You can also create shapes directly inside the item tag, in case you want to add some more details to your view, like this:

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_pressed="true">
            <shape>
                <solid android:color="#81ba73" />
                <corners android:radius="6dp" />
            </shape>
            <ripple android:color="#c62828"/>
        </item>
        <item android:state_enabled="false">
            <shape>
                <solid android:color="#788e73" />
                <corners android:radius="6dp" />
            </shape>
        </item>
        <item>
            <shape>
                <solid android:color="#add8a3" />
                <corners android:radius="6dp" />
            </shape>
        </item>
    </selector>
    

    Beware that Android will cycle through the items from top to bottom, therefore, you must place the item without condition on the bottom of the list (so it acts like a default/fallback).

    0 讨论(0)
  • 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

         <?xml version="1.0" encoding="UTF-8"?>
          <selector xmlns:android="http://schemas.android.com/apk/res/android">
      
               <item android:state_selected="true"
                     android:drawable="@drawable/tabs_selected" /> <!-- selected-->
               <item android:state_pressed="true"
                     android:drawable="@drawable/tabs_selected" /> <!-- pressed-->
               <item  android:drawable="@drawable/tabs_selected"/>
          </selector>
      

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

           <Button
                 android:layout_width="fill_parent" 
                 android:layout_height="wrap_content"
                 android:background="@drawable/button_background"/>
      
                                (or)
      
               Button tiny = (Button)findViewById(R.id.tiny);
                     tiny.setBackgroundResource(R.drawable.abc);
      

      2nd method is better for setting the background fd button

    0 讨论(0)
  • 2020-11-30 01:39

    you can implement in a xml file for this as follows:

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true" android:drawable="@drawable/your_imagename_while_focused"/>
    <item android:state_pressed="true" android:drawable="@drawable/your_imagename_while_pressed" />
    <item android:drawable="@drawable/image_name_while_notpressed" />  //means normal
    </selector>
    

    now save this xml file in drawable folder and name it suppos abc.xml and set it as follows

     Button tiny = (Button)findViewById(R.id.tiny);
     tiny.setBackgroundResource(R.drawable.abc);
    

    Hope it will help you. :)

    0 讨论(0)
  • 2020-11-30 01:42
    1. Create a file in drawable play_pause.xml
    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    
        <item android:state_selected="true"
              android:drawable="@drawable/pause" />
    
        <item android:state_selected="false"
              android:drawable="@drawable/play" />
        <!-- default -->
    </selector>
    
    1. In xml file add this below code
     <ImageView
                    android:id="@+id/iv_play"
                    android:layout_width="@dimen/_50sdp"
                    android:layout_height="@dimen/_50sdp"
                    android:layout_centerInParent="true"
                    android:layout_centerHorizontal="true"
                    android:background="@drawable/pause_button"
                    android:gravity="center"
                    android:scaleType="fitXY" />
    
    1. In java file add this below code
    iv_play = (ImageView) findViewById(R.id.iv_play);
    iv_play.setSelected(false);
    

    and also add this

    iv_play.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    iv_play.setSelected(!iv_play.isSelected());
                    if (iv_play.isSelected()) {
                        ((GifDrawable) gif_1.getDrawable()).start();
                        ((GifDrawable) gif_2.getDrawable()).start();
                    } else {
                        iv_play.setSelected(false);
                        ((GifDrawable) gif_1.getDrawable()).stop();
                        ((GifDrawable) gif_2.getDrawable()).stop();
                    }
                }
            });
    
    0 讨论(0)
  • 2020-11-30 01:44

    Its very easy to implement . For that you need to create a one xml file(selector file) and put it in drawable folder in res. After that set xml file in button's background in your layout file.

    button_background_selector.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <selector
      xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_focused="true" android:state_pressed="false" android:drawable="@drawable/your_hover_image" />
        <item android:state_focused="true" android:state_pressed="true" android:drawable="@drawable/your_hover_image" />
        <item android:state_focused="false" android:state_pressed="true" android:drawable="@drawable/your_hover_image"/>
        <item android:drawable="@drawable/your_simple_image" />
    </selector>
    

    Now set the above file in button's background.

    <Button
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:textColor="@color/grey_text"
        android:background="@drawable/button_background_selector"/>
    
    0 讨论(0)
提交回复
热议问题