Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make make a border for a clicked imagebutton in Android?

I am using Imagebuttons to show some icons in my android project.

<ImageButton
    android:id="@+id/button_one"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:padding="20dp"
    android:contentDescription="@string/button_one"
    android:background="@android:color/transparent"
    android:src="@drawable/button_one" />

I would like, when the button is in pressed state, there to be a white border with rounded corners. What can I do to make this happen? Can this be done using just code or will I need an extra background image?

like image 314
user2426316 Avatar asked Feb 18 '14 00:02

user2426316


People also ask

How do I put a border around an android TextView?

To add a border to Android TextView we need to create an XML containing shape as a rectangle file under the drawable's folder and set it as background to the TextView. <stroke> tag is used to set the border width and color.

How do I add a border to a layout?

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml. In the above code we have taken one text view with background as border so we need to create a file in drawable as boarder.

How do I remove the border from ImageButton in android?

You could use ImageButton. setBackgroundResource(int) (android:background attribute) which will get rid of that border.

Can you add text to an ImageButton android?

Answer: You can not display text with imageButton . Method that tell in Accepted answer also not work. If you use android:drawableLeft="@drawable/buttonok" then you can not set drawable in center of button . If you use android:background="@drawable/button_bg" then color of your drawable will be changed.


1 Answers

Here's one way if you don't want to create separate images with a border.

Create a drawable xml with your border style (ex. border.xml)

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <stroke
        android:width="1dp"
        android:color="#FFF" />
    <padding
        android:left="1dp"
        android:top="1dp"
        android:right="1dp"
        android:bottom="1dp" />
    <corners
        android:bottomRightRadius="8dip"
        android:bottomLeftRadius="8dip"
        android:topRightRadius="8dip"
        android:topLeftRadius="8dip" />
</shape>

Create a selector drawable (ex. some_selector_name.xml). Basically when pressed, it will show your border drawable. Otherwise, it will be transparent.

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@drawable/border" />
    <item android:drawable="@android:color/transparent" /> 
</selector>

Set your image button's background to your selector drawable.

<ImageButton
    android:id="@+id/button_one"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:padding="20dp"
    android:contentDescription="@string/button_one"
    android:background="@drawable/some_selector_name.xml"
    android:src="@drawable/button_one" />

Now when you press the button, it should display the white border. This is just an example but you should get the idea.

like image 132
singularhum Avatar answered Oct 01 '22 14:10

singularhum