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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T04:55:52+00:00 2026-06-17T04:55:52+00:00

I have custom buttons that are supposed to have different backgrounds depending if they

  • 0

I have custom buttons that are supposed to have different backgrounds depending if they are selected or not selected. I want to know if there is a way to state this in the XML file. I have a button for Celsius and a button for Fahrenheit. I want it to work where if one is selected, it stays “pressed” and unable to be clicked, while the other button can be pressed.

        <Button
            android:id="@+id/celsiusButton"
            android:text="C"
            android:background="@drawable/button_unpressed_shape"
            android:layout_weight="3"
            android:layout_height="match_parent"
            android:layout_width="0dip"
            android:gravity="center" />

        <Button
            android:id="@+id/fahrenheitButton"
            android:text="F"
            android:background="@drawable/button_unpressed_shape"
            android:layout_weight="3"
            android:layout_height="match_parent"
            android:layout_width="0dip"
            android:gravity="center" />

The Celsius button is defaulted to selected. I try working on it like this in my code, but it just seems to messy:

    tempText = (TextView) findViewById( R.id.temperatureId );
    celsiusButton = (Button) findViewById( R.id.celsiusButton );
    celsiusButton.setBackgroundDrawable( getResources().getDrawable( R.drawable.button_pressed_shape ) );
    celsiusButton.setClickable( false );

    celsiusButton.setOnClickListener( new OnClickListener() {
        public void onClick(View v) {
            if( hasRead ) {
                    celsiusButton.setBackgroundDrawable( getResources().getDrawable( R.drawable.button_pressed_shape ) );
                    celsiusButton.setClickable( false );
                    fahrenheitButton.setBackgroundDrawable( getResources().getDrawable( R.drawable.button_unpressed_shape ) );
                    fahrenheitButton.setClickable( true );
                    temperature = ( ( ( temperature - 32 ) * 5 ) / 9 );
                    tempText.setText( Double.toString( temperature ).substring( 0, ( Double.toString( temperature ).length() - 2 ) ) + " C" );
            }
        }       
    });

    fahrenheitButton = (Button) findViewById( R.id.fahrenheitButton );
    fahrenheitButton.setOnClickListener( new OnClickListener() {
        public void onClick( View v ) {
            if( hasRead ) {
                fahrenheitButton.setBackgroundDrawable( getResources().getDrawable( R.drawable.button_pressed_shape ) );
                celsiusButton.setBackgroundDrawable( getResources().getDrawable( R.drawable.button_unpressed_shape ) );
                celsiusButton.setClickable( true );
                fahrenheitButton.setClickable( false );
                temperature = ( ( temperature * 9 ) / 5 ) + 32;
                tempText.setText( Double.toString( temperature ).substring( 0, ( Double.toString( temperature ).length() - 2 ) ) + "° F" );
            }
        }
    });
  • 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-17T04:55:53+00:00Added an answer on June 17, 2026 at 4:55 am

    If it’s selected or not selected you should use a toggle button https://developer.android.com/reference/android/widget/ToggleButton.html

    Be aware that there are still 4 states for that

    You define them in a selector like this

    <?xml version="1.0" encoding="utf-8"?>
    
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_checked="true" android:state_pressed="true" android:drawable="@drawable/likeactivepressed" />
        <item android:state_pressed="true" android:drawable="@drawable/likeinitialpressed"/>
        <item android:state_checked="true" android:drawable="@drawable/likeon"/>
        <item android:drawable="@drawable/likeinitial"/>
    </selector>
    

    Then define it in your button like this

      android:background="@drawable/like_button"
    

    Edit

    You could actually just use 1 button for your use. Alternatively you can use 2 radio buttons

    https://developer.android.com/reference/android/widget/RadioButton.html

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

Sidebar

Related Questions

We have a custom JSF component that renders some buttons and a select box
I have a custom ListView in which there are 2 buttons and a textview
I have created a rollover menu with custom buttons that displays correctly in IE
Check out this jsbin . I have a form with a custom button that
I have a custom ButtonUI class that paints the button. Before drawing the text,
I have a custom control that displays a tryAgain button when the control is
i have custom cell with 2 buttons(the function of these buttons is just to
I have been trying to make custom radio buttons using HTML, CSS, and JavaScript.
I have a custom dialog with one editText view and two buttons ok and
I have tab controller created through storyboards with a custom implementation (simple buttons and

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.