Flynn
Flynn

Reputation: 6191

Android: Share sibling Views pressed state

I have a layout that has two children in it. My goal is to have the whole layout function as a button. Each of the children has a different color to show pressed state. When I press them individually they show their pressed state fine. However when one is pressed, I would like them both to show pressed state.

My first thought was to use duplicateParentState, but the parent never seems to be in the pressed state because the Views fill the layout. My next thought was to use AddStatesFromChildren, but all this did was make the parent show pressed, but not the children. Finally I tried using both, but that caused an error.

How can I get sibling Views to share a pressed state?

Upvotes: 4

Views: 618

Answers (2)

Lisa Wray
Lisa Wray

Reputation: 2272

I'm only 4 years late to this, but you were close:

  • set them both inside a parent layout
  • set your click listener on the parent
  • then set android:duplicateParentState="true" on each of the children.

Works like a charm!

Upvotes: 1

leenephi
leenephi

Reputation: 900

You could possibly do something like:

Button button = new Button(this);
button.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {

        Button otherButton = (Button) findViewById(R.id.other_button);
        otherButton.setPressed(true);

    }
})

But, of course, have the buttons called as you need to.

Upvotes: 0

Related Questions