How to draw a line over a GridView programmatically

2k views Asked by At

I am new to posting on stack overflow, so I will do my best to be descriptive here.

Right now I am playing around with android studio. I have set up a gridview (I built my own custom image adapter) that contains square images that form a 5 by 6 grid with some padding in-between each grid space. What I want to do is be able to draw a line (using canvas and paint) on top of this gridview programatically.

I have looked at quite a few posts on stack overflow in order to try and solve my issue, such as this one: Draw a line on top of existing layout programmatically

When following the instructions from the link above ^^^, I was able to create a program that draws a line over an ImageView.

But when I use similar code in my program that tries to draw a line over a gridView, it just draws the line and nothing else.

Here are code snippets of what I'm trying to do. This is the XML file:

<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="match_parent"
    android:orientation="vertical">

    <!-- Game layout *er -->
    <GridView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/gridviewGame"
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:layout_height="0dp"
        android:numColumns="5"
        android:stretchMode="columnWidth"
        android:gravity="center"
        />

    <com.example.ella.lazorsgame.DrawView
        android:id="@+id/paintView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />

</LinearLayout>

This is the custom view code:

public class DrawView extends View {
    Paint paint = new Paint();
    private int strokeWidth = 8;

    public DrawView(Context context, AttributeSet attrs) {
        super(context, attrs);
        paint.setColor(Color.RED);
        paint.setStrokeWidth(strokeWidth);
    }

    @Override
    public void onDraw(Canvas canvas) {
        canvas.drawLine(0, 0, 300, 300, paint);
    }

}

And this is where I'm drawing the grid of images in my main activity (this code works by itself... the grid just doesn't show up when I try and draw the line on top):

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game_setup);

        GridView gridview = (GridView) findViewById(R.id.gridviewGame);
        blockGridAdapter = new ImageAdapter(this, R.drawable.selectedblock, 30);

        // set up grid space object array *er
        for (int i = 0; i < BlockSpaces.length; i++) {
            BlockSpaces[i] = new BlockSpace(gridview, i);
        }

        // set up grid visual display on phone screen *er
        gridview.setAdapter(blockGridAdapter);

        // ------------- INITIAL GAME SETUP (block placements) -------------- *er
        blockSetup(levelSelected);

        gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                updateSelection(position);
            }
        });
    }

Thank you for your help! Let me know if more information is needed.

1

There are 1 answers

0
Ilja KO On

You are not calling the super method of onDraw

super.onDraw(canvas);

thus the only thing that is drawn is the line and the system doesn't handle your other views that are invalidated and needs to de redrawn!!!

check this out: http://developer.android.com/training/custom-views/custom-drawing.html