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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T13:01:28+00:00 2026-05-22T13:01:28+00:00

How to achieve an android:background change effect on multiple Views at the same time,

  • 0

How to achieve an android:background change effect on multiple Views at the same time, on clicking any of them?

Note that contents vary

I got a “badge” layout in two parts. They act like one button, but the two parts have different StateListDrawable (selectors) defining backgrounds for state_pressed etc. The first part is a header (“lefter”) with an image. The second is a content area getting filled with various sub-contents. It all comes down to that the contents of the both parts both come in various sizes.

I want the two badge parts to stretch separately AND share a common background effect when any of them is pressed.

<LinearLayout
    style="@style/Badge">

    <!-- LEFTER -->
    <LinearLayout
            style="@style/BadgeLefter"
            android:background="@drawable/badge_lefter_bg_blue">
            <ImageView
                    style="@style/BadgeLefterImage"
                    android:src="@drawable/office_building"/>
    </LinearLayout>

    <!-- CONTENT -->
    <LinearLayout
            android:id="@+id/badge_blue_content"
            style="@style/BadgeContent"
            android:background="@drawable/badge_content_bg_blue">
    </LinearLayout> 
</LinearLayout>

Example of StateListDrawable background selector (like @drawable/badge_content_bg_blue above):

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="false" android:state_pressed="false"
    android:drawable="@drawable/badge_bg_blue_normal"/>
<item android:state_focused="false" android:state_pressed="true"
    android:drawable="@drawable/badge_bg_blue_pressed"/>
<item android:state_focused="true"
    android:drawable="@drawable/badge_bg_blue_focused"/>
<item android:state_focused="true" android:state_pressed="true"
    android:drawable="@drawable/badge_bg_blue_pressed"/>
</selector>

where badge_bg_blue_normal etc are 9-patch png’s.

I’m new to android. I failed to find a similar question here. It does not help to custom my own Button because buttons only have one android:background selector. I found no 9-patch supporting multiple stretchable areas. I realize I could build a custom component. Is that a good idea here?

I use my badges widely around the app. An elegant solution would be greatly appreciated!
🙂

* SOLUTION EDIT: *

After building a custom component with a TouchListener I’m all set: ^_^

public class Badge extends LinearLayout {
    //...
    public Badge(Context c, AttributeSet attrs) {
        super(c, attrs);
        //... 
        // inflating layouts and setting selector backgrounds here
        //... 
        setClickable(true);
        setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {

                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    lefter.setPressed(true);
                    contents.setPressed(true);
                }
                else if (event.getAction() == MotionEvent.ACTION_UP || !isInside(v, event)) {
                    lefter.setPressed(false);
                    contents.setPressed(false);
                }
                return false;
            }

            private boolean isInside(View v, MotionEvent event) {
                //...
            }
        });
   }
   //...
}
  • 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-22T13:01:29+00:00Added an answer on May 22, 2026 at 1:01 pm

    If I understand your question correctly, you have two buttons, and upon pressing one of them, both of them should behave like they are being pressed?

    Upon pressing one of the buttons, did you try to call the View.setPressed(bool) function on the other one, and so on? That’s where I would start if I had design like yours above. If that doesn’t work, another approach could be to simply switch the background drawable on all buttons when pressing one of them, and simply not use the built-in state_pressed functionality.

    Regarding 9-patches that are stretchable in multiple areas, you can achieve this using the 9-patch-tool included in the android SDK. I did it with a button I made myself which had a static center area (vertically) and two stretchable areas above and below.

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

Sidebar

Related Questions

How can I achieve the following layout in Android? What I would like, is
Is it possible to achieve better data transfer rate with multiple parallel TCP connections
Hello In my android application i would like to get Time since when the
How can one achieve string localisation in Android?
What I want to achieve is this. I want to give the user the
I am trying to achieve better performance for my Java SWT application, and I
I regularly achieve 100% coverage of libraries using TDD, but not always, and there
How can I achieve this? I am trying to load content from the database
How would I achieve the pseudo-code below in JavaScript? I want to include the
I'm trying to achieve the equivalent of a WinForms ListView with its View property

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.