rakesh
rakesh

Reputation: 79

How to create square button in android for background?

img

I want to create square shape in background. Android studio shape doesn,t support square.How to create manually in border.xml?

Here is my code

border.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android">

    <solid android:color="#9294a3" />

    <stroke
    android:width="2dp"
    android:color="#c2bbbb" />

    <corners android:radius="2dp" />

</shape>

my.xml

   <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="70dp"
    android:background="#9294a3">

        <TextView
        android:id="@+id/tv_2"
        android:layout_width="wrap_content"
        android:text="2"
        android:textColor="#c2bbbb"
        android:textSize="30sp"
        android:background="@drawable/border"
        android:layout_height="wrap_content" />

    </LinearLayout

Upvotes: 1

Views: 7709

Answers (3)

Kevan Aghera
Kevan Aghera

Reputation: 241

<TextView
        android:id="@+id/input_name"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:layout_gravity="right"
        android:direction="center"
        android:gravity="center"/>

Upvotes: 0

Arnold Brown
Arnold Brown

Reputation: 1433

Button like thisenter image description here

xml

 <TextView
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:background="@drawable/border"
        android:text="2"
        android:textSize="50sp"
        android:gravity="center"
        android:textColor="#ffffff"
        android:padding="5dp"/>

Style (drawable/border.xml)

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <solid android:color="@color/colorGray"/>
            <stroke android:color="@color/colorBlack"
                android:width="4dp" />
            <!--corners allow us to make the rounded corners button-->
            <corners android:radius="15dp" />
        </shape>
    </item>
</selector>

Upvotes: 3

Parth Munjpara
Parth Munjpara

Reputation: 82

you should specify shape='Rectangle' for square it will be better.

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="10dp"
android:shape="rectangle" >

<solid android:color="#9294a3" />

<stroke
android:width="2dp"
android:color="#c2bbbb" />

<corners android:radius="2dp" />

Upvotes: 0

Related Questions