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

  • Home
  • SEARCH
  • 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 8275051
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T07:56:44+00:00 2026-06-08T07:56:44+00:00

I have tried to change the onClick image of a button using this code

  • 0

I have tried to change the onClick image of a button using this code in the listener

public void onClick(View v) {
   if(v==ButtonName)
   ButtonName.setImageResource(R.drawable.clicked_button_image);
   //action code                              

}  

The image of the button in this way changes correctly, however if someone uses the Back button of Android device, the button appears with clicked image (because clearly is showed the last instance of the previous activity)

if I try to use the code:

public void onClick(View v) {
       if(v==ButtonName)
       ButtonName.setImageResource(R.drawable.clicked_button_image);
       ButtonName.setImageResource(R.drawable.unclicked_button_image);
       //action code                              

    } 

The images of the button doesn’t changes.

How could I solve this problem? (without edit the xml).

  • 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-08T07:56:45+00:00Added an answer on June 8, 2026 at 7:56 am

    What about changing back when you click on the back button?

    @Override
    public boolean onRestore()
    {
        if(unclickedImageDisplayed)
        {
           ButtonName.setImageResource(R.drawable.unclicked_button_image);
        }
    
    }
    

    If you look at the documentation, and the activity lifecycle, you can see that the method on Restore is called when you are coming back to an activity.

    Maybe by having a private variable that you change when you change the background of your button, and by checking this boolean value for example, you can change the background of your button to give it his initial state, programmatically.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How to change cell background image on click on cell in iphone????I have tried
I am using Eclipse 3.4.1 Build M20080911-1700 I have tried to change the classpath
Anything I have tried didn't work. Currenly I have following code to change asp.net
I have this code which I am trying to change the ID of on.hover.
I have links like these that I want to change: mypage.com?page=missions&id=5 mypage.com?page=hello I tried
Have tried to find solutions for this and can't really come up with anything.
I have tried searching over the internet about this problem but not able to
I have tried this: #define format(f, ...) \ int size = strlen(f) + (sizeof((int[]){__VA_ARGS__})/sizeof(int))
I have a button on a page with an onClick I dynamically set via
How do I change the background image of a Button on Hover and on

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.