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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:51:04+00:00 2026-06-09T15:51:04+00:00

This must be a silly question as there are so many related question here

  • 0

This must be a silly question as there are so many related question here in stackoverflow and maybe the answer is simple. But anyway here it is.
I’ve got a button and create a drawable xml for it.
So under my button here is the code:

<Button
            android:id="@+id/button2"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="match_parent"
            android:layout_height="30dip"           
            android:background="@drawable/button2" 
            />

Now under my drawable/button2 i have created a selector in which i am not using an image.
Here is my code:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" >
    <shape>
        <solid
            android:color="#FF9E35" />
        <stroke
            android:width="1dp"
            android:color="#992f2f" />
        <corners
            android:radius="0dp"
            android:topLeftRadius="8dp"
            android:bottomLeftRadius="0dp"
            android:topRightRadius="0dp"
            android:bottomRightRadius="8dp" />
        <padding
            android:left="5dp"
            android:top="5dp"
            android:right="5dp"
            android:bottom="5dp" />
    </shape>
 </item>
 <item>
    <shape>
        <gradient
            android:startColor="#FF9E35"
            android:endColor="#992f2f"
            android:angle="270" />
        <stroke
            android:width="1dp"
            android:color="#992f2f" />
        <corners
            android:radius="0dp"
            android:topLeftRadius="8dp"
            android:bottomLeftRadius="0dp"
            android:topRightRadius="0dp"
            android:bottomRightRadius="8dp" />
        <padding
            android:left="5dp"
            android:top="5dp"
            android:right="5dp"
            android:bottom="5dp" />
    </shape>
    </item>
   </selector>

Now what I am trying to accomplish is that when I clicked that button, it change its background color to what is state_pressed is implying and return to normal if another button is clicked(Most likely a toggle button)
Is there anyway to do it on code behind or in xml selector?

Thanks,

JC

  • 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-09T15:51:05+00:00Added an answer on June 9, 2026 at 3:51 pm

    What you can actually do in Java code to accomplish it is :

    public class LaunchActivity extends Activity implements OnTouchListener{
    
        private Button yourButton;
    
    
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);        
            setContentView(R.layout.main);      
    
            yourButton= (Button)findViewById(R.id.yourButton);
            yourButton.setOnTouchListener(this); 
    
        }
    
        @Override
        public boolean onTouch(final View view, MotionEvent event) {
    
        final int action = event.getAction();
    
            if(view.getId()==R.id.yourButton){
                if(action == MotionEvent.ACTION_DOWN)
                      yourButton.setBackgroundResource(R.drawable.ic_button_pressed);
                if(action == MotionEvent.ACTION_UP)
                      yourButton.setBackgroundResource(R.drawable.ic_button_normal);
            }
    
     }
    }
    

    That will change the background of the button if he’s pressed. When the user release his finger of it, it will change the background again. If you want to change the background only if another button is pressed, don’t write the ACTION_UP case and in the other ACTION_DOWN case of another button, change the background of other button, see this below :

     @Override
            public boolean onTouch(final View view, MotionEvent event) {
    
        final int action = event.getAction();
    
            if(view.getId()==R.id.yourButton){
                if(action == MotionEvent.ACTION_DOWN){
                      secondButton.setBackgroundResource(R.drawable.ic_button_normal);
                      yourButton.setBackgroundResource(R.drawable.ic_button_pressed);
                }
            }
                if(view.getId()==R.id.secondButton){
                if(action == MotionEvent.ACTION_DOWN){
                      yourButton.setBackgroundResource(R.drawable.ic_button_normal);
                      secondButton.setBackgroundResource(R.drawable.ic_button_pressed);
                }
            }
    
     }
    

    Hope it helps !

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

Sidebar

Related Questions

This must be a duplicate. But with so many NSPredicate questions out there, I
This question maybe be silly, but I just want to confirm whether I get
Maybe this question is silly but I really don't know how to solve. First,
I'm afraid that this is a very silly question, but I must be missing
Maybe this is a very silly question but I'm new using django. I have
This must be very simple. But I just can't seem to find the proper
This must be so simple, but I can't find it: How do you restore
This must be a stupid question, but nevertheless I find it curious: Say I
This must be simple, but I can't seem to figure it out. I am
This must be a dumb question, but I don't see any obvious references. How

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.