Dear sir,
i have struggle with the creating grid view in android. I have created a example program of the more than one image button by using grid view method. How to create a new intent when i clicked the image buttons.
I am also attaching example program of grid view in this post. Please find it out and tell me...
MainActivity.java
package pete.android.study;
import java.util.ArrayList;
import java.util.List;
import pate.android.study.R;
import pete.android.study.AppInfoAdapter.ViewHolder;
import android.app.Activity;
import android.content.res.Resources;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.Toast;
public class MainActivity extends Activity {
private GridView mGridMain;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mGridMain = (GridView)findViewById(R.id.gvMain);
Resources res = getResources();
List<AppInfo> listAppInfo = new ArrayList<AppInfo>();
listAppInfo.add(new AppInfo(BitmapFactory.decodeResource(res, R.drawable.app_browser), "Internet"));
listAppInfo.add(new AppInfo(BitmapFactory.decodeResource(res, R.drawable.app_clock), "Clock"));
listAppInfo.add(new AppInfo(BitmapFactory.decodeResource(res, R.drawable.app_display), "Display"));
listAppInfo.add(new AppInfo(BitmapFactory.decodeResource(res, R.drawable.app_favorite), "Favorite"));
listAppInfo.add(new AppInfo(BitmapFactory.decodeResource(res, R.drawable.app_home), "Home"));
listAppInfo.add(new AppInfo(BitmapFactory.decodeResource(res, R.drawable.app_mail), "Mail"));
listAppInfo.add(new AppInfo(BitmapFactory.decodeResource(res, R.drawable.app_media), "Media"));
mGridMain.setAdapter(new AppInfoAdapter(this, listAppInfo));
mGridMain.setOnItemClickListener(mItemClickListener); // add a ItemClickListener
}
// our handle for listener
private OnItemClickListener mItemClickListener = new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
ViewHolder holder = (ViewHolder)view.getTag();
if(holder == null) {
return;
}
Toast.makeText(MainActivity.this, "You have clicked on item '" + holder.tvName.getText() + "'", Toast.LENGTH_SHORT).show();
}
};
}