Zelig
Zelig

Reputation: 1808

How to draw borders around each column and row in a GridLayout?

I want to display an array of values, as I could do it in Excel, with a GridLayout. So far, I have been able to display the grid and it's values in TextViews but I'm now struggling with the borders I can't draw the way I want.

My best attempt so far is to set each column width with tv.setWidth(myValue). For the cell's borders, I use a Shape :

 <?xml version="1.0" encoding="utf-8"?>
    <shape
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape= "rectangle">
        <solid android:color="#000000"/>
        <stroke android:width="1dp"
        android:color="#FFFFFF"/>
   </shape>

To create each cell's View, I use this code :

    tv = new TextView(this);
    tv.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
    tv.setGravity(Gravity.CENTER_VERTICAL);
    tv.setBackground(getResources().getDrawable(R.drawable.my_rectangle, getTheme()));
    tv.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
    tv.setWidth(myWidth);
    tv.setText("My text);

With this, each colums is equaly large and columns are correctly separated by a vertical straight line. A good point!

Unfortunately, lines are not separated by an horizontal straight line when at least one TextView is higher than others. In such a case, for TextViews that are smaller, the rectangles that surrounds them is too small and instead of surrounding the whole cell, they only surrounds their contents.

How can I do to have my lines separated by straight lines?

Upvotes: 0

Views: 29

Answers (0)

Related Questions