How to Draw Rectangle in Xml

Can I draw rectangle in XML?

Yes you can and here is one I made earlier:

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/listview_background_shape">
<stroke android:width="2dp" android:color="#ff207d94" />
<padding android:left="2dp"
android:top="2dp"
android:right="2dp"
android:bottom="2dp" />
<corners android:radius="5dp" />
<solid android:color="#ffffffff" />
</shape>

You can create a new XML file inside the drawable folder, and add the above code, then save it as rectangle.xml.

To use it inside a layout you would set the android:background attribute to the new drawable shape. The shape we have defined does not have any dimensions, and therefore will take the dimensions of the View that is defined in the layout.

So putting it all together:

<View
android:id="@+id/myRectangleView"
android:layout_width="200dp"
android:layout_height="50dp"
android:background="@drawable/rectangle"/>

Finally; you can set this rectangle to be the background of any View, although for ImageViews you would use android:src. This means you could use the rectangle as the background for ListViews, TextViews...etc.

Android: XML draw custom rectangle with solid border of one color with fill of another

For border you just need to add one line :

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<corners android:radius="20dp"/>
<padding android:left="10dp" android:right="10dp" android:top="10dp" android:bottom="10dp"/>
<solid android:color="#FFFFFF"/>

//ADD THIS LINE
<stroke android:width="1dp" android:color="#000000"/>

</shape>

Draw rectangle with XML shape settings in Android

You can load and use the XML defined drawable from code like so:

public class CustomView extends View {

Drawable shape;

public CustomView(Context context) {
super(context);
shape = context.getResources().getDrawable(R.drawable.shape);
}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);

shape.setBounds(left, top, right, bottom);
shape.draw(canvas)
}

// ... Additional methods omitted for brevity

}

How to draw a rectangle inner inside end triangle shape in xml drawable android?

You can try this as well

<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="100dp"
android:height="50dp"
android:viewportWidth="100.0"
android:viewportHeight="100.0">
<path
android:fillColor="#8BC34A"
android:pathData="M 0,0 L 100,0 90,50 100,100 0,100 z"/>
</vector>

and your TextView as:

<TextView
android:id="@+id/text_id"
android:layout_width="100dp"
android:layout_height="25dp"
android:layout_margin="@dimen/common_margin"
android:background="@drawable/you_drawable_here"
android:gravity="start|center"
android:paddingEnd="20dp"
android:text="your text here"
android:textColor="@color/white"
android:textSize="10sp"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

How to draw a rectangle with rounded corners using XML?

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

How to draw rounded rectangle in Android UI?

In your layout xml do the following:

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

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

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

</shape>

By changing the android:radius you can change the amount of "radius" of the corners.

<solid> is used to define the color of the drawable.

You can use replace android:radius with android:bottomLeftRadius, android:bottomRightRadius, android:topLeftRadius and android:topRightRadius to define radius for each corner.

Drawing custom oval and rectangle XML shape as buttom background

Try like this:

             <Button
android:layout_width="100dp"
android:layout_height="wrap_content"
android:background="@drawable/test"
android:text="Button"
android:textColor="@color/black"
/>

and @drawable/test will be like this:

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

<item
android:left="0dp"
android:right="-100dp"
android:top="-30dp"
android:bottom="-30dp">

<shape android:shape="oval">
<solid
android:color="@android:color/white" />
</shape>
</item>

</layer-list>

Note:
I know this is not the best solution. But for now this can help.
and if you want to increase the width of Button then change the below attributes accordingly to fit your need.

android:top="-30dp"
android:bottom="-30dp"

and the result will look like this:

Sample Image

draw a rectangle over image android

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">

<ImageView
android:id="@+id/coverImageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:scaleType="centerCrop"
/>

<View
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="@drawable/retangle_line"
android:layout_margin="50dp"
/>

</RelativeLayout>

To draw rounded rectangle in Android

I think you couldn't see the rounded rectangle as the radii are small so that they might not be noticed, try to set larger values for the four corners

Here is 120dp radii

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

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

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

</shape>

Sample Image

UPDATE

EDIT 2 : The error is now : The following classes could not be found: - corners (Fix Build Path, Edit XML) - shape (Fix Build Path, Edit XML) - solid (Fix Build Path, Edit XML) Tip: Try to build the project.

You can't use drawable tags into xml layout directly like <shape> or layer-list, instead you can refer to drawable resource with some layout view attributes like android:background as below

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/black">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/test"
android:padding="8dp"
android:text="Hello World!"
android:textColor="@android:color/holo_blue_dark"
android:textSize="22sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

Sample Image



Related Topics



Leave a reply



Submit