How to Create Borderless Buttons in Android [duplicate]
Asked Answered
T

8

95

The Android Design Guidelines say to use borderless buttons (see picture below), but don't really explain how. Someone asked this same question a few weeks ago here: How to create standard Borderless buttons (like in the design guidline mentioned)? and there was an answer marked as "the" answer, but I am still lost and I don't see a way to add comments to a question that has been "closed"

The answer-er said

"Look into the theme attributes buttonBarStyle, buttonBarButtonStyle, and borderlessButtonStyle"

but I still can't figure out how to actually use those things. I Googled around a bit and couldn't find anything so I figured I'd just ask the question again, and hopefully someone can provide a little more detail on how this works.

enter image description here

Trapper answered 6/2, 2012 at 21:57 Comment(0)
J
154

I thought I had this solved when I looked here a few weeks ago and noticed the answer about using a transparent background but it isn't quite good enough because it prevents the button from being highlighted when pressed.

Also, setting the style to Widget.Holo.Button.Borderless isn't appropriate because it makes the button boundaries to big.

To figure this out once and for all, I check the android source code for the standard Calendar app and found that it uses the following:

android:background="?android:attr/selectableItemBackground"

Doing it this way ensures the button is borderless and the correct size.

Jamal answered 30/3, 2012 at 23:0 Comment(6)
how can this be done from java code rather than XML? I'm creating an ImageButton in code and want it to be borderless but also have the highlight colour when touchedZaccaria
It does the trick, but it seems to me like a very convoluted and non-intuitive way. And this is for something Google is promoting in their official UI guidelines...Goggin
selectableItemBackground is only supported from API level 11, is there a solution for older versions?Agretha
you can try setting the background to @android:color/transparent for older versions. but it takes away the touch effect im afraid.Staid
@Agretha You will have to create your own <selector> for lower versionsPedicel
@Agretha android:background="?attr/selectableItemBackground" will work on API < 11 using the AppCompat library (note exclusion of android: prefix)Supplicatory
C
92

Look at this: http://developer.android.com/guide/topics/ui/controls/button.html#Borderless

The attribute on your Button or ImageButton tag:

    style="?android:attr/borderlessButtonStyle"
Cwmbran answered 10/7, 2012 at 17:8 Comment(4)
what about the style that makes a ripple effect that goes a bit outside the view? for example, in Lollipop's contacts app, when you search for a contact, and then you click the "up" button (the arrow) ? It's as if the button itself is circular, but has transparent background...Alimentation
@Dalc, awesome workingKev
@Cwmbran How can i apply it programtically ..?Deformity
This works for Buttons, but when you do this for ImageButtons, though the border goes away, it loses its ability to wrap its width only to the image provided, and starts behaving like a normal button with a minimum width. So for ImageButtons, the best way is android:background="?android:attr/selectableItemBackground"Rescissory
P
23

If you use ActionbarSherlock...

<Button 
  android:id="@+id/my_button" 
  style="@style/Widget.Sherlock.ActionButton" />
Piccalilli answered 7/12, 2012 at 13:34 Comment(2)
If you use HoloEverywhere <Button android:background="?selectableItemBackground" />Demean
Worked perfectly for me with minimumSdk 9 < style="@style/Widget.Sherlock.ActionButton" >Holophytic
D
19

Some days ago a stumbeled over this again.

Here my solution:

This is done in 2 steps: Setting the button background attribute to android:attr/selectableItemBackground creates you a button with feedback but no background.

android:background="?android:attr/selectableItemBackground"

The line to divide the borderless button from the rest of you layout is done by a view with the background android:attr/dividerVertical

android:background="?android:attr/dividerVertical"

For a better understanding here is a layout for a OK / Cancel borderless button combination at the bottom of your screen (like in the right picture above).

<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:layout_alignParentBottom="true">
        <View
            android:layout_width="match_parent"
            android:layout_height="1dip"
            android:layout_marginLeft="4dip"
            android:layout_marginRight="4dip"
            android:background="?android:attr/dividerVertical"
            android:layout_alignParentTop="true"/>
        <View
            android:id="@+id/ViewColorPickerHelper"
            android:layout_width="1dip"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_alignParentBottom="true"
            android:layout_marginBottom="4dip"
            android:layout_marginTop="4dip"
            android:background="?android:attr/dividerVertical" 
            android:layout_centerHorizontal="true"/>
        <Button
            android:id="@+id/BtnColorPickerCancel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_toLeftOf="@id/ViewColorPickerHelper"
            android:background="?android:attr/selectableItemBackground"
            android:text="@android:string/cancel" 
            android:layout_alignParentBottom="true"/>
        <Button
            android:id="@+id/BtnColorPickerOk"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:background="?android:attr/selectableItemBackground"
            android:text="@android:string/ok" 
            android:layout_alignParentBottom="true" 
            android:layout_toRightOf="@id/ViewColorPickerHelper"/>
    </RelativeLayout>
Defeatism answered 14/7, 2012 at 5:27 Comment(0)
M
15

This code works for me:

<View
    android:layout_width="match_parent"
    android:layout_height="1dip"
    android:background="?android:attr/dividerVertical" />

<LinearLayout
    style="?android:attr/buttonBarStyle"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:measureWithLargestChild="true"
    android:orientation="horizontal"
    android:paddingLeft="2dip"
    android:paddingRight="2dip"
    android:paddingTop="0dip" >

    <Button
        android:id="@+id/cancel"
        style="?android:attr/buttonBarButtonStyle"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:onClick="onClickCancel"
        android:text="@string/cancel" />

     <Button
        android:id="@+id/info"
        style="?android:attr/buttonBarButtonStyle"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:onClick="onClickInfo"
        android:visibility="gone"
        android:text="@string/info" />

    <Button
        android:id="@+id/ok"
        style="?android:attr/buttonBarButtonStyle"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:onClick="onClickSave"
        android:text="@string/save" />
</LinearLayout>

I show 3 buttons at the bottom

Marquettamarquette answered 27/4, 2013 at 18:4 Comment(0)
R
10
android:background="@android:color/transparent"
Razo answered 6/2, 2012 at 21:58 Comment(2)
If the image background is not transparent by itself, it won't help much.Prolong
also android:background="@null" worksShana
G
9
<Button android:id="@+id/my_button" style="@android:style/Widget.Holo.Button.Borderless" />
Gob answered 8/2, 2012 at 7:38 Comment(0)
P
0

You should also set margins and padding of the picture to 0. Also look at the second, not marked answer at How to create standard Borderless buttons (like in the design guidline mentioned)?

Prolong answered 6/2, 2012 at 22:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.