-2

Good afternoon,

I am doing an app who needs 3 buttons aligned under the TabLayout, like in the picture below :

enter image description here

I do not know what is the best way to do them, because each one has different borders.

Anyone can help me ? Thank you !


I finally did it :

buttons.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:gravity="center_horizontal"
    android:layout_marginLeft="18sp"
    android:layout_marginRight="18sp">

    <Button
        style="?android:textAppearanceSmall"
        android:layout_width="wrap_content"
        android:layout_height="31sp"
        android:id="@+id/one"
        android:text="ONE"
        android:textColor="@color/red"
        android:layout_weight="1"
        android:background="@drawable/button_shape_one" />

    <Button
        style="?android:textAppearanceSmall"
        android:layout_width="wrap_content"
        android:layout_height="31sp"
        android:id="@+id/two"
        android:text="TWO"
        android:textColor="@color/red"
        android:layout_weight="1"
        android:background="@drawable/button_shape_two"/>

    <Button
        style="?android:textAppearanceSmall"
        android:layout_width="wrap_content"
        android:layout_height="31sp"
        android:id="@+id/three"
        android:text="THREE"
        android:textColor="@color/red"
        android:layout_weight="1"
        android:background="@drawable/button_shape_three"/>

</LinearLayout>

button_shape_one.xml

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">

    <solid android:color="@color/transparent"/>

    <stroke android:width="1dp"
        android:color="@color/red"
        />

    <corners android:bottomRightRadius="0dp"
        android:bottomLeftRadius="5dp"
        android:topLeftRadius="5dp"
        android:topRightRadius="0dp"/>
</shape>

button_shape_two.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:bottom="0dp"
        android:left="-2dp"
        android:right="-2dp"
        android:top="0dp">
        <shape android:shape="rectangle" >
            <stroke
                android:width="1dp"
                android:color="@color/red" />

            <solid android:color="@color/transparent" />

            <padding android:left="10dp"
                android:right="10dp"
                android:top="5dp"
                android:bottom="5dp" />
        </shape>
    </item>

</layer-list>

button_shape_three.xml

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">

    <solid android:color="@color/transparent"/>

    <stroke android:width="1dp"
        android:color="@color/red"
        />

    <corners android:bottomRightRadius="5dp"
        android:bottomLeftRadius="0dp"
        android:topLeftRadius="0dp"
        android:topRightRadius="5dp"/>
</shape>

Result :

enter image description here

Thank you all!

D. Math
  • 329
  • 6
  • 17

4 Answers4

1

Set width to "0dp" and weight to "1" for all your buttons and wrap them inside a horizontal LinearLayout

Simon Tenbeitel
  • 835
  • 1
  • 8
  • 20
1

roundedbuttons.xml in drawable

?xml version="1.0" encoding="UTF-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android"> 

<solid android:color="#ffffffff"/>    

<stroke android:width="3dp"
    android:color="#ff000000"
    />

 <corners android:bottomRightRadius="7dp" 
     android:bottomLeftRadius="7dp" 
     android:topLeftRadius="7dp"
     android:topRightRadius="7dp"/> 
 </shape>

Edit the shape according to your preference

In your xml layout

LinearLayout(Horizontal)
 - Buttons with background(roundedbuttons.xml) and weight="1"
Siddhesh Dighe
  • 2,894
  • 3
  • 16
  • 16
1

You need to create a Shape drawable for all of your buttons. For eg the center button can use below drawable as background.

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#606060"/>
    <stroke android:color="#303030" android:width="2dp" />
</shape>

For the other two you need rounded edges only on left and right.For that you may follow This post on SO.

*Note: change the colors according to your needs and you will also require a selector to be applied on each button to highlight the selection. The left and right borders can also be removed using shape drawable.For more details please see This post

The more easier approach will be to use This Library.

Community
  • 1
  • 1
Anirudh Sharma
  • 7,968
  • 13
  • 40
  • 42
  • Thank you ! But what about the right border of the first button and the left border of the right button ? If I do nothing it'll have double borders – D. Math Jun 29 '16 at 16:36
  • You can also also remove the borders using the shape drawable.Check the edit. – Anirudh Sharma Jun 29 '16 at 16:38
1

Why don't you use one linear layout with blue border as parent of 3 textViews with background. No need to make it complicated.

Laurence Pardz
  • 292
  • 3
  • 8