0

I need to set a border to a LinearLayout when the user click on it. Every solution I have tried so far change the color of all the LinearLayout, not just the border.

<LinearLayout
    android:id="@+id/linearlayout_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:clickable="true"
    android:paddingBottom="3dp"
    android:paddingLeft="10dp"
    android:paddingRight="3dp"
    android:paddingTop="10dp>
</LinearLayout>

and the code I have used to add the border :

GradientDrawable drawable = new GradientDrawable();
drawable.setShape(GradientDrawable.RECTANGLE);
drawable.setStroke(1, Color.BLACK);
relativelayout_main.setBackgroundDrawable(drawable);

Any suggestion?

stavros.3p
  • 2,244
  • 6
  • 20
  • 37

1 Answers1

0

I would consider using a background resource item using a selector for clicked/not clicked state that uses a shape with a specific color/stroke width or something like that.

Something like what is listed here https://stackoverflow.com/a/8203840/3934023 but combined with the selector to use that only in the clicked state, like in this answer: https://stackoverflow.com/a/5295522/3934023.

Community
  • 1
  • 1
Zach
  • 3,909
  • 6
  • 25
  • 50