0

I have to create buttons styled like this:

enter image description here

Yes, strange I know. Those 2 corners should not scale if button text is shorter / longer.

Is this possible to create it using an XML ?

  • I tried a vector but the vector scales with the size of button.
  • Any other idea I have is to do it programmatically, e.g., something like in this answer

(Explanation of the design: we're experimenting with the design. Imagine button with 4 such corners. Buttons next to each other each having 2 corners close to the other one, etc. Our users love fancy design ... . :-) )

2 Answers2

1

you can create customView

 <LinearLayout
    android:orientation="vertical"
    android:layout_marginTop="100dp"
    android:layout_marginLeft="100dp"
    android:layout_width="100dp"
    android:layout_height="wrap_content">
    <View
        android:layout_marginLeft="90dp"
        android:background="#000000"
        android:layout_width="match_parent"
        android:layout_height="1dp"/>
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    <TextView
        android:gravity="center"
        android:textSize="13dp"
        android:textColor="#000000"
        android:text="Button text.."
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="50dp" />
        <RelativeLayout
            android:layout_width="1dp"
            android:layout_height="match_parent">
       <View
           android:background="#000000"
           android:layout_width="1dp"
           android:layout_height="10dp"/>
        <View
            android:layout_alignParentBottom="true"
            android:background="#000000"
            android:layout_width="1dp"
            android:layout_height="10dp"/>
        </RelativeLayout>
    </LinearLayout>
    <View
        android:layout_marginLeft="90dp"
        android:background="#000000"
        android:layout_width="match_parent"
        android:layout_height="1dp"/>
</LinearLayout>

image like this

J.Doe
  • 54
  • 1
1

Okay after a little research and best practice you surely need a Layerlist as a background for your Button(though even a TextView will work i.e it is also clickable like any view).

SOLUTION:

You will have to open the drawables folder and add a drawable resource called lets say custom_button_background then use this layerlist inside it:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape>
            <solid android:color="#000000" />
        </shape>
    </item>
    <item android:top="20dp" android:bottom="20dp" android:left="20dp" android:right="20dp">
        <shape>
            <solid android:color="#ffffff" />
        </shape>
    </item>
    <item android:right="80dp">
        <shape>
            <solid android:color="#ffffff" />
        </shape>
    </item>
    <item android:top="80dp" android:bottom="80dp">
        <shape>
            <solid android:color="#ffffff" />
        </shape>
    </item>
</layer-list>

Then we are done! This will be our background, In my Android studio the Preview for this look like this:
image on how it looks
You can adjust the values to reduce them to your needs. To set this as your Button or any View you simply add this attribute to it:

android:background="@drawable/custom_button_background"

Just adjust the measures to fit your Button Size!

Xenolion
  • 12,035
  • 7
  • 33
  • 48