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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T04:47:23+00:00 2026-06-02T04:47:23+00:00

I have a linearLayout with buttons as its children. I need the button backgrounds

  • 0

I have a linearLayout with buttons as its children. I need the button backgrounds to change when they get focus. The buttons are focusable in touch mode.

I have set the following selector xml to the buttons.

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true"  
           android:drawable="@drawable/tile_focused" />
    <item android:drawable="@drawable/tile" /> <!-- default -->
</selector>

Update

if (layout == null) {
        layout = new LinearLayout(this);
        RelativeLayout.LayoutParams rlp = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
        rlp.addRule(RelativeLayout.BELOW, R.id.toplogo);
        layout.setLayoutParams(rlp);
        layout.setId(wordLayoutID);
        layout.setBackgroundColor(Color.WHITE);
        mainLayout.addView(layout, rlp);
    } 

    Drawable tileBG = getResources().getDrawable(R.drawable.selector_tile);
    for (int i = 0; i < word.length(); i++) {
        Button btntile = new Button(this);
        LinearLayout.LayoutParams rlp =  new LinearLayout.LayoutParams(40, 40);
        btntile.setId(inputViewsIds+i);
        btntile.setBackgroundDrawable(tileBG);
        btntile.setFocusableInTouchMode(true);
        layout.addView(btntile, rlp);
    }

The problem is that when I set focus to any one button, all button’s backgrounds change to “tile_focused”.
I guess the problem is in the selector xml. What is wrong here?

  • 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-02T04:47:25+00:00Added an answer on June 2, 2026 at 4:47 am

    The root cause of this behavior was the common tile drawable reference I used for all buttons. I changed that and loaded separate drawables for each button. That fixed it.

    Thanks to Everyone.

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

Sidebar

Related Questions

I want that button(child) will have 25% of full width of horizontal LinearLayout (its
I have a simple LinearLayout with two Buttons side by side. They are supposed
I have a linear layout with several buttons in it. The button images are
I have a LinearLayout and its child - ImageView. My activity's onCreate does nothing
I have an empty LinearLayout , and I need to add a dynamic number
I have a fragment which has its own state (selected buttons, etc). That state
I have an array of buttons. Now, when each button is clicked, I want
I have a LinearLayout that I would like to change the background color of
I have Radio buttons that are created dynamically. LinearLayout linLayRoot = (LinearLayout)dialogView.findViewById(R.id.dialog_layout_root); RadioGroup radGp
I have a custom Button and I want to set its dimensions in code

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.