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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T05:45:29+00:00 2026-06-01T05:45:29+00:00

Is there a pure-java way of setting the android:button attribute? My use case is

  • 0

Is there a pure-java way of setting the android:button attribute? My use case is I have some radio buttons with custom on/off images. They are currently in xml, but for several reasons I want their creation to be done in pure java. Currently the android:button references a <selector> xml file:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true"
        android:drawable="@drawable/red_down_full" />
    <item android:drawable="@drawable/red_down" /> 
</selector>

Is there any way to either

  • reference the <selector> xml file in java
  • skip the <selector> entirely and just set the state_checked image in java?
  • 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-01T05:45:31+00:00Added an answer on June 1, 2026 at 5:45 am

    Yes, you can set the xml image selector via code. If you want to set the selector on your RadioButton you have to call its setButtonDrawable(idOfYourSelector).

    The code will be like this:

    ((RadioButton)findViewById(R.id.yourRadioButton)).setButtonDrawable(R.drawable.yourSelector);
    

    If you want to set only the image you used for state_checked, then you need to supply the id of your state_checked image on setButtonDrawable(idOfYourImage) instead the id of your selector. In your case it will be:

    ((RadioButton)findViewById(R.id.yourRadioButton)).setButtonDrawable(R.drawable.red_down_full);
    

    To ensure that it will not overlap the label of your RadioButton, you have to know the width of your drawable. Then set the left padding of your RadioButton depending on the width of your drawable. This will ensure the image for your RadioButton will not overlap its label.

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

Sidebar

Related Questions

I'm wondering if there is a way in java (pure code, not some Eclipse
Is there a pure-Java equivalent to <jsp:forward page=... /> that I can use within
Is there a way to write this Java code as pure JSTL? for(int g
Is there a pure .net way to do this reliably? The solutions I keep
Is there a way to shift focus to a div by default through pure
If I have a recorded audio file (MP3), is there any way to get
I want to use java classes created in pure java project in my blackberry
I have a client-server application. The server is pure Java, the client is an
I'm building a video player in pure Java around JMF, with completely custom UI
Is there any way to persist a java object, thrift serialized, and recover it

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.