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 5960445
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:49:39+00:00 2026-05-22T18:49:39+00:00

i am trying to create an app. When user clicks on the button, it

  • 0

i am trying to create an app. When user clicks on the button, it gets pressed (image is loaded). When he clicks the button now, it gets unpressed (image is loaded) and so on. I am using the code below with which the problem is that when i click on the unpressed button 3rd time it doesn’t get into the pressed state again. So at first click it gets pressed, at second click it gets unpressed, and at the 3rd click nothing happens.

 NameButton = (Button)findViewById(R.id.Button01);
        NameButton.setOnClickListener(new View.OnClickListener() 
        {
            public void onClick(View v) 
            {
                NameButton.setBackgroundResource(R.drawable.pressed);
                NameButton.setOnClickListener(new View.OnClickListener() 
                {
                    public void onClick(View v) 
                    {
                        NameButton.setBackgroundResource(R.drawable.unpressed);
                    }
                });

            }
        });
  • 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-05-22T18:49:40+00:00Added an answer on May 22, 2026 at 6:49 pm

    You might do it like that:

        NameButton = (Button)findViewById(R.id.Button01);
        NameButton.setOnClickListener(new View.OnClickListener() 
        {
            public void onClick(View v) 
            {
                if (setPressed)
                    NameButton.setBackgroundResource(R.drawable.pressed);
                else
                    NameButton.setBackgroundResource(R.drawable.unpressed);
                setPressed = !setPressed;
            }
        });
    

    And have a boolean variable setPressed as a member variable.

    Alternatively, consider using ToggleButton.

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

Sidebar

Related Questions

I am trying to develop a iphone app by using cocos2d. I create a
Trying to create my first iPhone app that would play back audio. When I
I'm trying to create a Palm OS app to check a web site every
I am trying to create a utility keystroke app so i can do things
Trying to create a user account in a test. But getting a Object reference
Trying to create several layers of folders at once C:\pie\applepie\recipies\ without using several different
I'm trying to create an app launcher in vb.net but I do not know
I'm creating a WPF app using the MVVM design pattern, and I'm trying to
I am playing around with xCode and was trying to create a small app
I'm new to Xcode and I'm trying to create an app which has a

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.