As it seems to me 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 the GridLayout
:
View view = new View(this);
view.setBackgroundColor(Color.GREEN);
gl.addView(view);
But for your information Android allows you to create "shapes" (shapes) as for example rectangles (and so squares) and include them as background of a layout as if it were a drawable. With that your squares can have:
- Customized color and thickness edges
- Rounded edges
- Bottom gradient (in the direction you want)
For example:
res/drawable/meu_rectangle.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" />
Does it not have a way to add it only with code? For example,
gv.addView( drawRect( 100, 100, 5, 5 ) )
? Or am I not getting the concept of Android and its idiosyncrasies?– brazilianldsjaguar
The
drawRect()
is a resource to be used in an objectCanvas
, which is a "canvas painting" that after drawing needs to be converted to a bitmap in order to be displayed. The simplest solution with code is to create a View, assign it a background color and add it to Gridlayout:View v = new View(this); v.setBackgroundColor(cor); gl.addView(v);
that would be it?– Piovezan
I’m going to try with this - but I think it was more or less something like this that I was wanting! Thank you
– brazilianldsjaguar
Okay, I’ve updated the response with the source code and if you have any other requests, just say the word.
– Piovezan