Every one want to do some thing with android ListView and GridView and Much More.
Here is the Example of Custom GridView in Which we have used ImageView and TextView below it.
Now One more thing that is this custom gridview can also done by the using View ,But i done it as shown in my code using Class Like what you want to put in grid just put it in gridview xml file and also make class ViewHolder as done by me in this tutorials .
Here below is my class for the GridView
public class Gridview extends Activity {
GridView mGridViewl;
//Context mContext = Gridview.this;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mGridViewl = (GridView) findViewById(R.id.gridview);
mGridViewl.setAdapter(new EfficientAdapter(this));
}
private static class EfficientAdapter extends BaseAdapter{
private LayoutInflater mLayoutInflater;
public EfficientAdapter(Context context){
mLayoutInflater=LayoutInflater.from(context);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mThumbIds.length;
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public View getView(int position, View converView, ViewGroup parent) {
ViewHolder mVHolder;
if(converView == null){
converView=mLayoutInflater.inflate(R.layout.customgrid, parent, false);
mVHolder=new ViewHolder();
mVHolder.mImageView=(ImageView)converView.findViewById(R.id.imgview);
mVHolder.mTextView=(TextView)converView.findViewById(R.id.text);
mVHolder.mImageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
mVHolder.mImageView.setPadding(8,8,8,8);
converView.setTag(mVHolder);
}else{
mVHolder=(ViewHolder)converView.getTag();
}
mVHolder.mImageView.setImageResource(mThumbIds[position]);
mVHolder.mTextView.setText(COUNTRIES[position]);
return converView;
}
}
static class ViewHolder{
ImageView mImageView;
TextView mTextView;
}
static final String[] COUNTRIES = new String[] { "Afghanistan", "Albania",
"Algeria", "American Samoa", "Andorra", "Angola", "Anguilla",
"Antarctica", "Antigua and Barbuda", "Argentina" };
private static Integer[] mThumbIds = { R.drawable.android,
R.drawable.android1, R.drawable.android2,
R.drawable.android3, R.drawable.android4,
R.drawable.android5, R.drawable.android6, R.drawable.android7,
R.drawable.android8, R.drawable.android9 };
}
and here is my main.xml file for setcontentView(); in my activity
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<GridView android:id="@+id/gridview"
android:stretchMode="columnWidth"
android:cacheColorHint="#00000000"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="3"
android:clipChildren="true"
android:horizontalSpacing="5dip"
android:verticalSpacing="5dip">
</GridView>
</LinearLayout>
here is the another file of customgrid.xml for use in gridview
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<ImageView
android:src="@drawable/android"
android:scaleType="center"
android:cropToPadding="true"
android:adjustViewBounds="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imgview"
android:layout_gravity="center"/>
<TextView
android:text="@string/hello"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/text"
android:layout_gravity="center_horizontal"
/>
</LinearLayout>
Here is the Out Put Image of my Device
Here is the Example of Custom GridView in Which we have used ImageView and TextView below it.
Now One more thing that is this custom gridview can also done by the using View ,But i done it as shown in my code using Class Like what you want to put in grid just put it in gridview xml file and also make class ViewHolder as done by me in this tutorials .
Here below is my class for the GridView
public class Gridview extends Activity {
GridView mGridViewl;
//Context mContext = Gridview.this;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mGridViewl = (GridView) findViewById(R.id.gridview);
mGridViewl.setAdapter(new EfficientAdapter(this));
}
private static class EfficientAdapter extends BaseAdapter{
private LayoutInflater mLayoutInflater;
public EfficientAdapter(Context context){
mLayoutInflater=LayoutInflater.from(context);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mThumbIds.length;
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public View getView(int position, View converView, ViewGroup parent) {
ViewHolder mVHolder;
if(converView == null){
converView=mLayoutInflater.inflate(R.layout.customgrid, parent, false);
mVHolder=new ViewHolder();
mVHolder.mImageView=(ImageView)converView.findViewById(R.id.imgview);
mVHolder.mTextView=(TextView)converView.findViewById(R.id.text);
mVHolder.mImageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
mVHolder.mImageView.setPadding(8,8,8,8);
converView.setTag(mVHolder);
}else{
mVHolder=(ViewHolder)converView.getTag();
}
mVHolder.mImageView.setImageResource(mThumbIds[position]);
mVHolder.mTextView.setText(COUNTRIES[position]);
return converView;
}
}
static class ViewHolder{
ImageView mImageView;
TextView mTextView;
}
static final String[] COUNTRIES = new String[] { "Afghanistan", "Albania",
"Algeria", "American Samoa", "Andorra", "Angola", "Anguilla",
"Antarctica", "Antigua and Barbuda", "Argentina" };
private static Integer[] mThumbIds = { R.drawable.android,
R.drawable.android1, R.drawable.android2,
R.drawable.android3, R.drawable.android4,
R.drawable.android5, R.drawable.android6, R.drawable.android7,
R.drawable.android8, R.drawable.android9 };
}
and here is my main.xml file for setcontentView(); in my activity
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<GridView android:id="@+id/gridview"
android:stretchMode="columnWidth"
android:cacheColorHint="#00000000"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="3"
android:clipChildren="true"
android:horizontalSpacing="5dip"
android:verticalSpacing="5dip">
</GridView>
</LinearLayout>
here is the another file of customgrid.xml for use in gridview
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<ImageView
android:src="@drawable/android"
android:scaleType="center"
android:cropToPadding="true"
android:adjustViewBounds="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imgview"
android:layout_gravity="center"/>
<TextView
android:text="@string/hello"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/text"
android:layout_gravity="center_horizontal"
/>
</LinearLayout>
Here is the Out Put Image of my Device