As I think you want to add a square to a GridLayout
, the simplest solution would be to create a View, set a background color, and include it in GridLayout
:
View view = new View(this);
view.setBackgroundColor(Color.GREEN);
gl.addView(view);
But for your information Android allows you to create shapes, such as rectangles (and therefore squares), and include them as the background of a layout as if it were a drawable . With this your squares can have:
- Custom color and thickness borders
- Rounded edges
- Background gradients (in the direction you want them)
For example:
res / drawable / my_retangulo.xml:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" android:padding="10dp">
<stroke
android:width="2dp"
android:color="#FFFFFFFF"/>
<gradient
android:startColor="#DD000000"
android:endColor="#DD2ECCFA"
android:angle="225"/>
<corners
android:radius="2dp"
android:bottomRightRadius="0dp"
android:bottomLeftRadius="0dp"
android:topLeftRadius="15dp"
android:topRightRadius="15dp"/>
</shape>
You include in the layout like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/meu_retangulo" />