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?

Student
  • 1,986
  • 5
  • 19
  • 35

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,559
  • 6
  • 21
  • 45
  • Thanks for the answer. I followed the instruction,I did get the border, but the inside is black. – Student Dec 16 '16 at 02:33
  • I tried inside the shape, but nothing changed – Student Dec 16 '16 at 02:35
  • Black is represented in hex with 000000, so that's behaving correctly. Please post what you have in your original post, I'll take a look. – Zach Dec 16 '16 at 02:37