How to make a simple square on Android?

Asked

Viewed 2,228 times

1

Can you make a square full of one color without all the complications? I say complications, because the examples I see here on the internet requires you to create another class, inheriting from the View, making the drawRect() in function onDraw() through the Canvas...

In the end, is there such a simple thing:

int width = 10;
GridLayout gl = (GridLayout)findViewById( R.ids.mainGridLayout );
Square s = new Square( width );
gl.addView( s );

3 answers

3


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?

  • The drawRect() is a resource to be used in an object Canvas, 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?

  • I’m going to try with this - but I think it was more or less something like this that I was wanting! Thank you

  • Okay, I’ve updated the response with the source code and if you have any other requests, just say the word.

1

If all you want is a square, why not simply use a Linearlayout with a background color?

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_centerInParent="true"
        android:background="@android:color/holo_red_dark"
        android:layout_width="200dp"
        android:layout_height="200dp" />

</RelativeLayout>

It is possible to position it where you want even over other views.

0

You can also create your image and use as a Drawable feature.

  • For those who do not know, can give a reference "Drawable feature"? Thanks!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.