0

I would like to know the simplest way to Add and Remove a border from an Image in Android Studio using Java. I just want the user to have the indication that he clicked the Image (which is clickable of course) without having two different .png.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Adam Swid
  • 13
  • 2

3 Answers3

2

Wrap your ImageView on FrameLayout and add

android:foreground="?android:attr/selectableItemBackground" attribute.

            <FrameLayout
                android:layout_width="48dp"
                android:layout_height="match_parent"
                android:foreground="?android:attr/selectableItemBackground">

                <ImageView
                    android:id="@+id/myButton"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    style="?android:borderlessButtonStyle"
                    android:scaleType="center"
                    android:src="@drawable/my_drawable" />
            </FrameLayout>
Sergey Shustikov
  • 15,377
  • 12
  • 67
  • 119
0

Put the Image in a layout, then pad the layout with some value you wish. set an OnClickListener to that image View, and on Click, change the background colour of the layout the Image is placed in. You can have this as an on and off toggle very easily.

Edit:-

ssh's answer is much clearer than mine. Reference his please.

Ali Elgazar
  • 777
  • 2
  • 12
  • 26
0

You can add a View with 1dp width before your image and make it visible whenever you need to show the border.

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <View
        android:layout_width="1dp"
        android:layout_height="match_parent"
        android:visibility="invisible"/>
    <ImageView
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:src=""/>
</LinearLayout>
arsent
  • 6,975
  • 3
  • 32
  • 31