android - Use ImageView with a GridView to set number of images displayed in the rows and columns -
i want layout 8x8 grid of images, 40x40px icons, within gridview layout using imageview class.
i have tried playing around setlayoutparams , setscaletype methods of imageview class have not been able achieve desired affect. here have, experimenting 3x3 grid of small icons until hang of it.
main acitvity:
package com.topherwilso.ropasci; import android.os.bundle; import android.app.activity; import android.view.menu; import android.widget.gridview; public class mainactivity extends activity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); gridview gv = (gridview) findviewbyid(r.id.gridview); gv.setadapter(new imageadapter(getapplicationcontext())); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.main, menu); return true; } }
and imageadapter class:
package com.topherwilso.ropasci; import android.content.context; import android.view.view; import android.view.viewgroup; import android.widget.baseadapter; import android.widget.gridview; import android.widget.imageview; import android.widget.imageview.scaletype; public class imageadapter extends baseadapter { int[] images = { r.drawable.one, r.drawable.two, r.drawable.three, r.drawable.four, r.drawable.five, r.drawable.six, r.drawable.seven, r.drawable.eight, r.drawable.nine }; private context context; public imageadapter(context applicationcontext){ context=applicationcontext; } @override public int getcount() { //number of data elements displayed return images.length; } @override public object getitem(int position) { // todo auto-generated method stub return null; } @override public long getitemid(int position) { // todo auto-generated method stub return 0; } @override public view getview(int position, view convertview, viewgroup parent) { imageview iv; if(convertview != null){ iv = (imageview) convertview; } else{ iv = new imageview(context); iv.setlayoutparams(new gridview.layoutparams(120, 120)); iv.setscaletype(scaletype.center); iv.setpadding(0, 0, 0, 0); } iv.setimageresource(images[position]); return iv; } }
this looks now,
this like,
try below
<?xml version="1.0" encoding="utf-8"?> <gridview xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/gridview" android:padding="40dp" android:layout_width="fill_parent" android:layout_height="fill_parent" android:columnwidth="0dp" android:numcolumns="3" // set number of columns 3 android:verticalspacing="20dp" android:horizontalspacing="20dp" android:gravity="center" />
http://developer.android.com/guide/topics/ui/layout/gridview.html
http://developer.android.com/reference/android/widget/gridview.html
specifying number of rows not possible. can use tablelayout instead of gridview if above not work. in case have 9 icons setting column number 3 should work.
for tablelayout
http://developer.android.com/reference/android/widget/tablelayout.html
Comments
Post a Comment