Vivek Mishra
Vivek Mishra

Reputation: 5705

Creating a static grid view

I want to create a static gridview in my app . It should be 5X5 dimension. I know how to build it but thing I don't understand is that I want to inflate just a single blue color image to grid's all rows and columns. I just can't figure out how to achieve this. What I think that if we use adapter we should have a list of images or so but I just have a single image.

Upvotes: 0

Views: 2808

Answers (1)

user4952596
user4952596

Reputation:

If you simply want to render same images wit 5 X 5.

create activity_main.xml

<RelativeLayout 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"

    tools:context=".MainActivity" >

    <GridView
        android:id="@+id/gridView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView1"
        android:numColumns="5" >

    </GridView>

   <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="25dp"
        android:textStyle="bold"
        android:text=" Computer Languages..." />

</RelativeLayout>

call from your MainActivity

gv=(GridView) findViewById(R.id.gridView1);
gv.setAdapter(new CustomAdapter(this));

Create CustomAdapter.

public class CustomAdapter extends BaseAdapter {
    int count = 10;
    Context context;
    private static LayoutInflater inflater=null;
    public CustomAdapter(MainActivity mainActivity) {
        // TODO Auto-generated constructor stub
        context=mainActivity;
        inflater = ( LayoutInflater )context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return count;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        View rowView = inflater.inflate(R.layout.image_list, null);
        return rowView;
    }

}

and the layout for this,

<?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" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_gravity="center"
        android:layout_width="88dp"
        android:layout_height="88dp"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp"
        android:src="@drawable/ic_launcher" />

</LinearLayout> 

Upvotes: 1

Related Questions