0

I have been struggling for a while to draw borders. I want to use a GridView to make a sliding puzzle game wheres the image is divided into squares and mixed up. Firstly, I have set up a GridView with String ArrayAdapter to just see how it will look.

It should look something like this. I just want to know how to make this frame with borders.

example

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<GridView
    android:id="@+id/gridView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:numColumns="auto_fit">
</GridView>



</LinearLayout>

Inside Activity class:

String[] words = {"example","example1","example2"};
GridView grid;

public void onCreate(Bundle bund){
    super.onCreate(bund);       
    setContentView(R.layout.grid_layout);

    grid = (GridView) findViewById(R.id.gridView1);             
    grid.setAdapter(new ArrayAdapter<String>(MyActivity.this, android.R.layout.simple_list_item_1, words));

    //grid.setOnItemClickListener(this);

And if I run this code I get a GridView without borders. I have already looked at similar questions but I still don`t know how to do it.

enter image description here

EDIT Looking at How to set border of GridView on Android.

EDIT: It appears that I needed to create resource colors.xml. By following the tutorial listed above it works. Thank you for the help provided.

Community
  • 1
  • 1

1 Answers1

0

You should do the next:

  1. set background color of your gridview it will be a border color
  2. set background color of your grid item as you need
  3. set vertical and horizontal spacing it will be a border thickness

And don't forget to change you grid item layout height as match_parent

GridView gv = findViewById(R.id.my_grid_view);
gv.setBackgroundColor(Color.WHITE);
gv.setVerticalSpacing(1);
gv.setHorizontalSpacing(1);

refer to:https://stackoverflow.com/a/18311057/2771869

Community
  • 1
  • 1
Mohammad Rababah
  • 1,730
  • 4
  • 17
  • 32