Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7907185
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T11:18:45+00:00 2026-06-03T11:18:45+00:00

I created a button in the layout . In the Drawable folder I created

  • 0

I created a button in the layout . In the Drawable folder I created a XML file named btn01_state. The btn01_state.xml is assigned to the button i created through “android:background=@drawable/btn01_state“

Now, the button has a default image img1.when i click on the button, the image1 changes to img2, and once i release the clicked mouse button, the image2 again changed to img1 again.

what i want to do is,to change the image of the button with evey click.

for an example, initially
btn01 has img01

if btn01 is pressed==> set img of btn01 to img02 and keep img02 till the btn01 is pressed again. Now, btn01 has img02 on it.

When btn01 is pressed, set img01 to btn01.

I hope this clarified more what i want to do.

btn_selector:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/android_blue"
      android:state_pressed="true" />
<item android:drawable="@drawable/ic_launcher"
      android:state_focused="true" />
<item android:drawable="@drawable/ic_launcher" />

main.xml

<Button 
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:id="@+id/btn01"
    android:background="@drawable/btn01_state"/>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T11:18:46+00:00Added an answer on June 3, 2026 at 11:18 am

    You can do it easily within the code.

    boolean isPressed = false;
    button.setOnClickListener(buttonListener);
    
    OnClickListener buttonListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            if(isPressed)
               button.setBackgroundResource(R.drawable.icon1);
            else
               button.setBackgroundResource(R.drawable.icon2);
    
            isPressed = !isPressed;
       }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have one ImageButton in my xml layout like that : <ImageButton android:id=@+id/tabsButton android:background=@drawable/button
i have created an image button in my xml <Button android:layout_height=wrap_content android:id=@+id/send android:layout_width=50dip android:background=@drawable/button_back>
I created a state-list selector file item_bg_selector.xml in the drawable folder like this :
I have a main activity with this layout file: <?xml version=1.0 encoding=utf-8?> <LinearLayout xmlns:android=http://schemas.android.com/apk/res/android
I have created a button in my screen's XML like this: <Button android:text=Lets Get
I have created a custom Button as follows. file : buttoncontrol.xml <?xml version=1.0 encoding=utf-8?>
I want to create a Button otherthan creating it from xml layout.i want to
I created a button with a 9patch image as a background. In the 9patch
I have created a button CSS class that uses background images with different positions
I have created a button in my Android application & I tried to set

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.