I am studying android launcher at the moment.
I've succeeded in making the launcher with Gridview with sample code from the web.
However, I don't want to show all the apps installed on the phone, just wanna show selected apps.
Is there any way to do that?
package com.example.gridlauncher;
import java.util.List;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
public class MainActivity extends Activity {
PackageManager myPackageManager;
public class MyBaseAdapter extends BaseAdapter {
private Context myContext;
private List<ResolveInfo> MyAppList;
MyBaseAdapter(Context c, List<ResolveInfo> l){
myContext = c;
MyAppList = l;
}
#Override
public int getCount() {
return MyAppList.size();
}
#Override
public Object getItem(int position) {
return MyAppList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
// if it's not recycled, initialize some attributes
imageView = new ImageView(myContext);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView)convertView;
}
ResolveInfo resolveInfo = MyAppList.get(position);
imageView.setImageDrawable(resolveInfo.loadIcon(myPackageManager));
return imageView;
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myPackageManager = getPackageManager();
Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> intentList = getPackageManager().queryIntentActivities(intent, 0);
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new MyBaseAdapter(this, intentList));
gridview.setOnItemClickListener(myOnItemClickListener);
}
OnItemClickListener myOnItemClickListener =
new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
ResolveInfo cleckedResolveInfo =
(ResolveInfo)parent.getItemAtPosition(position);
ActivityInfo clickedActivityInfo =
cleckedResolveInfo.activityInfo;
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.setClassName(
clickedActivityInfo.applicationInfo.packageName,
clickedActivityInfo.name);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
startActivity(intent);
}
};
}
Between
List<ResolveInfo> intentList = getPackageManager().queryIntentActivities(intent, 0);
and
gridview.setAdapter(new MyBaseAdapter(this, intentList));
just add a white list filter to remove the unneeded items from intentList.
Related
I got a source code about GridView.
But, I confused why some code were error.
These are the code. I got error on OnClick Method in MainActivity. Is here anyone help me? Thanks
public void onItemClick(AdapterView <? > AdapterView, View View, int Int, long Long) {
AdapterView = ((ImageItem) imageItems.get(Int)).getTitle();
toDetail(Int, AdapterView);
}
});
I want to each item that when it clicked shown the detailed image and text. I also get code like this but when I clicked "GingerBread" item, it goes to first item ("Astro")
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
ImageItem item = (ImageItem) parent.getItemAtPosition(position);
//Create intent
Intent intent = new Intent(getApplicationContext(), DetailActivity.class);
// intent.putExtra("android.intent.extra.TEXT", Integer.parseInt(getIntent().getStringExtra("android.intent.extra.TEXT")));
intent.putExtra("title", item.getTitle());
//Start details activity
startActivity(intent);
overridePendingTransition(R.anim.home_detail_entry, R.anim.home_detail_exit);
}
});
MainActivity.java
package com.acer.gridview;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends Activity {
public static String[] bitmapName = {
"Astro", "Bender", "Cupcake", "Donut", "Eclair",
"Froyo", "Gingerbread", "HoneyComb"
};
public static int[] bitmapId = {
R.drawable.Astro, R.drawable.Bender, R.drawable.Cupcake, R.drawable.Eclair,
R.drawable.Froyo, R.drawable.Gingerbread, R.drawable.HoneyComb
};
private GridViewAdapter gridAdapter;
private GridView gridView;
ArrayList < ImageItem > imageItems;
private ArrayList < ImageItem > getData() {
imageItems = new ArrayList();
BitmapFactory.Options localOptions = new BitmapFactory.Options();
localOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;
int i = 0;
for (;;) {
if (i > 7) {
return imageItems;
}
Bitmap localBitmap = BitmapFactory.decodeResource(getResources(), bitmapId[i], localOptions);
imageItems.add(new ImageItem(localBitmap, bitmapName[i]));
i += 1;
}
}
#
Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_grid);
gridView = (GridView) findViewById(R.id.gridView);
gridAdapter = new GridViewAdapter(this, R.layout.row_grid, getData());
gridView.setAdapter(gridAdapter);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView <? > AdapterView, View View, int Int, long Long) {
////////////This code shown error///////////////////
AdapterView = ((ImageItem) imageItems.get(Int)).getTitle();
toDetail(Int, AdapterView);
////////////This code shown error///////////////////
}
});
}
public void toDetail(int Int, String String) {
Intent localIntent = new Intent(this, DetailActivity.class);
localIntent.putExtra("android.intent.extra.TEXT", Integer.toString(Int));
localIntent.putExtra("title", String);
startActivity(localIntent);
}
/**
* A placeholder fragment containing a simple view.
*/
private boolean ActionBar(final Intent aIntent) {
try {
startActivity(aIntent);
return true;
} catch (ActivityNotFoundException e) {
return false;
}
}
public void showDialog() {
LayoutInflater inflater = getLayoutInflater();
View localView = inflater.inflate(R.layout.help_dialog, null);
((ImageView) localView.findViewById(R.id.imageView1)).setImageResource(R.drawable.makhrojulhuruf);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(localView);
builder.show();
}
#
Override
public boolean onCreateOptionsMenu(Menu menu) {}
#
Override
public boolean onOptionsItemSelected(MenuItem item) {
}
}
GridViewAdapter.java
package com.acer.gridview;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
/**
* Created by Acer on 08/02/2016.
*/
public class GridViewAdapter extends ArrayAdapter<ImageItem> {
private Context context;
private int layoutResourceId;
private ArrayList<ImageItem> data = new ArrayList();
public GridViewAdapter(Context context, int layoutResourceId, ArrayList<ImageItem> data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ViewHolder holder = null;
if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new ViewHolder();
holder.imageTitle = (TextView) row.findViewById(R.id.text);
holder.image = (ImageView) row.findViewById(R.id.image);
row.setTag(holder);
} else {
holder = (ViewHolder) row.getTag();
}
ImageItem item = (ImageItem) data.get(position);
holder.imageTitle.setText(item.getTitle());
holder.image.setImageBitmap(item.getImage());
return row;
}
static class ViewHolder {
TextView imageTitle;
ImageView image;
}
}
ImageItem.java
package com.acer.gridview;
import android.graphics.Bitmap;
/**
* Created by Acer on 08/02/2016.
*/
public class ImageItem {
private Bitmap image;
private String title;
public ImageItem(Bitmap Bitmap, String String)
{
setImage(Bitmap);
setTitle(String);
}
public Bitmap getImage()
{
return this.image;
}
public String getTitle()
{
return this.title;
}
public void setImage(Bitmap Bitmap)
{
this.image = Bitmap;
}
public void setTitle(String String)
{
this.title = String;
}
}
hello friends i have listview that contain all install app and a checkbox.my problem is that when i check item using checkbox it is not remain permanently ie if i check some item using checkbox and exit app and then run app it should show all previous checked item.
here my package ApkListActivity.java
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import android.app.Activity;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;
import com.ibc.android.demo.appslist.adapter.ApkAdapter;
import com.ibc.android.demo.appslist.app.AppData;
public class ApkListActivity extends Activity {
PackageManager packageManager;
ListView apkList;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
packageManager = getPackageManager();
List<PackageInfo> packageList = packageManager
.getInstalledPackages(PackageManager.GET_PERMISSIONS);
List<PackageInfo> packageList1 = new ArrayList<PackageInfo>();
/* To filter out System apps */
for (PackageInfo pi : packageList) {
boolean b = isSystemPackage(pi);
if (!b) {
packageList1.add(pi);
}
}
Collections.sort(packageList1, new Comparator<PackageInfo>() {
public int compare(PackageInfo o1, PackageInfo o2) {
return o1.applicationInfo
.loadLabel(getPackageManager())
.toString()
.compareTo(
o2.applicationInfo.loadLabel(
getPackageManager()).toString());
}
});
apkList = (ListView) findViewById(R.id.applist);
apkList.setAdapter(new ApkAdapter(this, packageList1, packageManager));
apkList.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long row) {
PackageInfo packageInfo = (PackageInfo) parent
.getItemAtPosition(position);
AppData appData = (AppData) getApplicationContext();
appData.setPackageInfo(packageInfo);
// Intent appInfo = new Intent(getApplicationContext(),
// ApkInfo.class);
// startActivity(appInfo);
PackageInfo country = (PackageInfo) parent
.getItemAtPosition(position);
Toast.makeText(
getApplicationContext(),
"Clicked on Row: "
+ country.applicationInfo.loadLabel(
getPackageManager()).toString(),
Toast.LENGTH_LONG).show();
}
});
}
private boolean isSystemPackage(PackageInfo pkgInfo) {
return ((pkgInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) ? true
: false;
}
}
and this is my ApkAdapter.java
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.TextView;
import com.ibc.android.demo.appslist.activity.R;
public class ApkAdapter extends BaseAdapter {
List<PackageInfo> packageList = null;
Activity context;
Context context2;
PackageManager packageManager;
private ArrayList<Boolean> checkList = new ArrayList<Boolean>();
boolean selected = false;
// List<ApplicationInfo> packageList = new ArrayList<ApplicationInfo>();
public ApkAdapter(Activity context, List<PackageInfo> packageList,
PackageManager packageManager) {
super();
this.context = context;
this.packageList = packageList;
this.packageManager = packageManager;
for (int i = 0; i < packageList.size(); i++) {
checkList.add(selected);
}
}
public int getCount() {
return ((null != packageList) ? packageList.size() : 0);
// return packageList.size();
}
public PackageInfo getItem(int position) {
return ((null != packageList) ? packageList.get(position) : null);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (null == view) {
LayoutInflater inflater = context.getLayoutInflater();
view = inflater.inflate(R.layout.apklist_item, null);
// holder = new ViewHolder();
}
PackageInfo data = packageList.get(position);
if (null != data) {
TextView apkName = (TextView) view.findViewById(R.id.appname);
ImageView apkicon = (ImageView) view.findViewById(R.id.icon_image);
TextView packageName = (TextView) view
.findViewById(R.id.packagename);
CheckBox checkBox = (CheckBox) view.findViewById(R.id.checkbox);
checkBox.setTag(Integer.valueOf(position)); // set the tag so we can
// identify the correct
// row in the listener
checkBox.setChecked(checkList.get(position)); // set the status
// as
// we stored it
checkBox.setOnCheckedChangeListener(mListener); // set the listener
PackageInfo packageInfo = (PackageInfo) getItem(position);
Drawable appIcon = packageManager
.getApplicationIcon(packageInfo.applicationInfo);
String appName = packageManager.getApplicationLabel(
packageInfo.applicationInfo).toString();
;
apkicon.setImageDrawable(appIcon);
apkName.setText(appName);
packageName.setText(packageInfo.packageName);
}
return view;
}
OnCheckedChangeListener mListener = new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
checkList.set((Integer) buttonView.getTag(), isChecked);
}
};
}
You can save the List of Boolean values in a simple 1010101 format and then save this integer in SharedPreferences. When your app is opened, just use the integer to repopulate your checkboxes.
I am working on an application where i have a gridview containing all my images from the gallery of my phone and when i click on an image it opens in another activvity (i already did this part).
I need to make the new activity swipe so i can see all the images one after the other by swiping on the screen.
When i swipe, the image changes so i see the next one etc..
I need to do this using a ViewPager .
Can anyone help me do it ?
This is my main activity code:
package com.example.gridviewgallery;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.app.ActionBarActivity;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
public class MainActivityGridView extends ActionBarActivity {
int columnIndex;
Cursor cursor = null;
#SuppressWarnings("deprecation")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity_grid_view);
String[] projection = {MediaStore.Images.Thumbnails._ID, MediaStore.Images.Thumbnails.DATA};
cursor = managedQuery(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, projection, null, null,
MediaStore.Images.Thumbnails.IMAGE_ID);
columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);
GridView gridview = (GridView) findViewById(R.id.gridView1);
gridview.setAdapter(new GridAdapter(getApplicationContext(), cursor, columnIndex));
gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id)
{
cursor.moveToPosition(position);
columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);
int i = cursor.getInt(columnIndex);
Intent intent = new Intent(view.getContext(), ImageActivity.class);
intent.putExtra("id", i);
intent.putExtra("pos", position);
startActivity(intent);
}
});
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
This is my second activity code :
package com.example.gridviewgallery;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.app.ActionBarActivity;
import android.view.MenuItem;
import android.widget.ImageView;
public class ImageActivity extends ActionBarActivity
{
MainActivityGridView magv;
int pos,id,columnIndex;
ImageView imageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image);
Intent intent = getIntent();
id = intent.getExtras().getInt("id");
pos = intent.getExtras().getInt("pos");
imageView = (ImageView) findViewById(R.id.imageView1);
imageView.setImageURI( Uri.withAppendedPath(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
String.valueOf(id)));
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
imageView.setPadding(8, 8, 8, 8);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
and this is my grid adapter class :
package com.example.gridviewgallery;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.v4.widget.CursorAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.ImageView;
public class GridAdapter extends CursorAdapter
{
//private Context mContext;
//private Cursor mCursor;
private int mColumnIndex;
//private ImageView imageView;
#SuppressWarnings("deprecation")
public GridAdapter(Context context, Cursor c, int ci) {
super(context, c);
mContext = context;
mCursor = c;
mColumnIndex = ci;
}
#Override
public void bindView(View convertView, Context context, Cursor curs)
{
ImageView imageView = (ImageView) convertView;
int id = curs.getInt(mColumnIndex);
imageView.setImageURI( Uri.withAppendedPath(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
String.valueOf(id)));
imageView.setLayoutParams(new GridView.LayoutParams(350, 350));
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
imageView.setPadding(8, 8, 8, 8);
//imageView = (ImageView) convertView.findViewById(R.id.image);
}
#Override
public View newView(Context context, Cursor curs, ViewGroup parent)
{
/*imageView = new ImageView(mContext);
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View retView = inflater.inflate(R.layout.grid_image, parent, false);
/* int id = curs.getInt(mColumnIndex);
imageView.setImageURI(Uri.withAppendedPath(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, String.valueOf(id)));
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);*/
//return retView;
return new ImageView(context);
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
public int mygetItemId(int position) {
return 0;
}
}
Thanks.
This question already has answers here:
Duplicates When Showing Installed Applications in Android
(2 answers)
Closed 7 months ago.
Hello guys can you help me show all installed apps on the main screen? Can you give me some source code of a simple grid view?
Look at this code.
package com.example.android.apis.view;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import java.util.List;
import com.example.android.apis.R;
public class Grid1 extends Activity {
GridView mGrid;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
loadApps(); // do this in onresume?
setContentView(R.layout.grid_1);
mGrid = (GridView) findViewById(R.id.myGrid);
mGrid.setAdapter(new AppsAdapter());
}
private List<ResolveInfo> mApps;
private void loadApps() {
Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
mApps = getPackageManager().queryIntentActivities(mainIntent, 0);
}
public class AppsAdapter extends BaseAdapter {
public AppsAdapter() {
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView i;
if (convertView == null) {
i = new ImageView(Grid1.this);
i.setScaleType(ImageView.ScaleType.FIT_CENTER);
i.setLayoutParams(new GridView.LayoutParams(50, 50));
} else {
i = (ImageView) convertView;
}
ResolveInfo info = mApps.get(position);
i.setImageDrawable(info.activityInfo.loadIcon(getPackageManager()));
return i;
}
public final int getCount() {
return mApps.size();
}
public final Object getItem(int position) {
return mApps.get(position);
}
public final long getItemId(int position) {
return position;
}
}
}
Source API demo..
I want to display full screen image by clicking on the grid view item.I am done with grid view but no idea how to do this. Please give me some idea to do this??
Thanks....!
mGridView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View v, int position,long id) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Image"+(position+1), Toast.LENGTH_SHORT).show();
Intent intent =new Intent(getApplicationContext(),MyImageViewActivity.class);
intent.putExtra("filename", i);
startActivity(intent);
}
});
MyGridView.java
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.KeyStore.LoadStoreParameter;
import com.myworkspace.R.menu;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.provider.ContactsContract.CommonDataKinds.Im;
import android.provider.MediaStore.Audio.Media;
import android.provider.SyncStateContract.Columns;
import android.sax.StartElementListener;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.Gallery;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
public class MyGridView extends Activity {
GridView mGridView;
int counter,i;
MyImageAdapter adapter = null;
ImageView imgVw;
Bitmap bitmap;
private Cursor cursor;
int columnIndex;
Integer[] mThumbsIds = {
R.drawable.image1,R.drawable.image2,
R.drawable.image3,R.drawable.image4,
R.drawable.image5,R.drawable.icon,
R.drawable.icon,R.drawable.icon,
R.drawable.icon
};
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.mygridacti);
ImageView imageview = (ImageView)findViewById(R.id.GalleryView);
mGridView = (GridView)findViewById(R.id.grdvw);
adapter = new MyImageAdapter(this);
adapter.notifyDataSetChanged();
mGridView.setAdapter(adapter);
mGridView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View v, int position,long id) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Image"+(position+1), Toast.LENGTH_SHORT).show();
Intent myIntent = new Intent(MyGridView.this, MyImageViewActivity.class);
Bundle bundle = new Bundle();
bundle.putInt("image", mThumbsIds[position]);
myIntent.putExtras(bundle);
startActivityForResult(myIntent, 0);
}
});
}
}
class MyImageAdapter extends BaseAdapter{
private Context mContext;
ImageView imageview;
int counter;
public MyImageAdapter(Context c){
mContext = c;
}
public MyImageAdapter(OnClickListener onClickListener) {
// TODO Auto-generated constructor stub
}
#Override
public int getCount() {
return mThumbsIds.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(final int position, final View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if(convertView == null){
imageview = new ImageView(mContext);
imageview.setLayoutParams(new GridView.LayoutParams(85,85));
imageview.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageview.setPadding(8, 8, 8, 8);
counter++;
}
else{
imageview = (ImageView) convertView;
}
imageview.setImageResource(mThumbsIds[position]);
return imageview;
}
private Integer[] mThumbsIds = {
R.drawable.image1,R.drawable.image2,
R.drawable.image3,R.drawable.image4,
R.drawable.image5,R.drawable.icon,
R.drawable.icon,R.drawable.icon,
R.drawable.icon};
}
You know how to show images in gridView. after this perform action of
grid.setOnItemClickListener(new OnItemClickListener(){
Intent intent = new Intent
(getApplicationContext(), ViewImage.class);
intent.putExtra("filename", fileName);
//here fileName is something which tells the next activity about "selected image"
//It can be ImageLocation/ImageName/ImageID/ImageURL
startActivity(intent);
}
ViewImage.class
//here I assume that you send the ImageFile in the extra.
//so, converted it into bitmap and showed in the ImageView
Bundle extras = getIntent().getExtras();
BitmapFactory.Options bfo = new BitmapFactory.Options();
bfo.inSampleSize = 2;
filename = extras.getString("filename");
mBitmap = BitmapFactory.decodeFile(filename, bfo);
imageview.setbitmap(mbitmap);
You should do something like this in your setOnItemClickListener, I m using your code and improving it
mGridView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View v, int position,long id) {
// use gridItem position to get the Image from mThumbsIds
int image = mThumsIds[position] // here I m passing the position of GridViewItem
Intent intent =new Intent(getApplicationContext(),MyImageViewActivity.class);
intent.putExtra("image", image);
startActivity(intent);
}
});
You do not need to pass Bundle if you are just sending a single Image ID
now in your ViewImage.class you can get the image from the intent using
I m using Glide to show you how to display this Image in ImageView
int image = getIntent().getIntExtra("image");
//now you can use this "image" variable to display image
ImageView imageView = findViewById(R.id.imageView);
Glide.with(this).load(image).into(imageView); // here pass you imageView in the last method as parameter
Hope this will help!
On itemClick, just pass the position
mGridView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View v, int position,long id) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Image"+(position+1), Toast.LENGTH_SHORT).show();
Intent myIntent = new Intent(MyGridView.this, MyImageViewActivity.class);
myIntent.putInt("image", position);
startActivityForResult(myIntent, 0);
}
});
In other Activity,
public class MyImageViewActivity extends AppCompatActivity {
Integer[] mThumbsIds = {
R.drawable.image1,R.drawable.image2,
R.drawable.image3,R.drawable.image4,
R.drawable.image5,R.drawable.icon,
R.drawable.icon,R.drawable.icon,
R.drawable.icon
};
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_question7_spinner_background);
Intent intent = getIntent();
int position = intent.getIntExtra("image", 0);
int imageId = mThumbsIds[position];
}
}