Mark023 Mark023 - 4 months ago 13
Android Question

How to add colored rectangles with textview in it

enter image description hereI am developing an Android app.
Dynamically I created name (obtained from server), but now I need to add colored rectangles of same height and width and some textview in it.

I have attached the image that I need to develop.

How can I achieve this? Using canvas? I am finding it very difficult.

image

Answer

For TextView, just add padding and background to achive this :) Try this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:background="#BBBBBB"
    tools:context="xyz.mhuy.myapplication.MainActivity">

    <RelativeLayout
        android:id="@+id/relativeLayout1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/black" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="HelloWorld"
            android:padding="10dp"
            android:textColor="#ffffff"
            android:gravity=""/>

        <TextView
            android:id="@+id/button2"
            android:padding="10dp"
            android:background="#ff0000"
            android:textColor="#ffffff"
            android:textStyle="bold"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="next"
            android:layout_alignParentRight="true"/>

    </RelativeLayout>
</LinearLayout>

Above Is what you mean? the result is: enter image description here