Full screen image gallery Android - android

Sorry for my english. There is a mini-gallery, when you click on an image from the gallery it should (picture) to open in full screen. App just throws by pressing the image. what am I doing wrong?
MainActivity
public class MainAcTwo extends Activity {
#SuppressWarnings("deprecation")
Gallery gallery;
ImageView bigimage;
#SuppressWarnings("deprecation")
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.two);
gallery=(Gallery) findViewById(R.id.gallery);
gallery.setAdapter(new ImageAdapter(this));
gallery.setOnItemClickListener(new OnItemClickListener() {
#TargetApi(Build.VERSION_CODES.HONEYCOMB) #SuppressLint("NewApi") public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
long imageId = ImageAdapter.ThumbsIds[position];
Intent fullScreenIntent = new Intent(v.getContext(), FullScreenImage.class);
fullScreenIntent.putExtra(MainAcTwo.class.getName(), imageId);
MainAcTwo.this.startActivity(fullScreenIntent);
}
});
}
}
ImageAdapter
public class ImageAdapter extends BaseAdapter implements SpinnerAdapter {
private Context context;
public ImageAdapter(Context context) {
// TODO Auto-generated constructor stub
this.context = context;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return ThumbsIds.length;
}
#Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
#Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getView(final int position, View convertView, ViewGroup arg2) {
// TODO Auto-generated method stub
ImageView imageView=null;
if(convertView == null) {
imageView = new ImageView(context);
imageView.setLayoutParams(new Gallery.LayoutParams(215, 200));
imageView.setPadding(8, 8, 8, 8);
}else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(ThumbsIds[position]);
return imageView;
}
public static Integer[] ThumbsIds={
R.drawable.abs_icla,
R.drawable.abs_dog,
R.drawable.abs_flow,
R.drawable.abs_neb,
R.drawable.abs_rad
};
}
FullScreenImage
public class FullScreenImage extends Activity {
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.full_image);
Intent intent = getIntent();
long imageId = (Long) intent.getExtras().get(FullScreenImage.class.getName());
ImageView imageView = (ImageView) findViewById(R.id.fullImage);
imageView.setLayoutParams( new ViewGroup.LayoutParams(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT));
imageView.setImageResource((int) imageId);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
}
}

clean you project if it does not work
imageView.setLayoutParams( new ViewGroup.LayoutParams(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT));
change it to
imageView.setLayoutParams( new LinearLayout.LayoutParams(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT));

Related

how to get image from gridview list when i click image, i'm using picasso library

i want when i click image from gridview, image send to new layout (Details Image)
this is MainActivity
public class MainActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));
gridview.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Intent i = new Intent(getApplicationContext(), SingleViewActivity.class);
i.putExtra("arg3", arg2);
startActivity(i);
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "okey", Toast.LENGTH_SHORT).show();
}
});
}
ImageAdapter
public class ImageAdapter extends BaseAdapter {
public static final String URL ="http://api.androidhive.info/json/movies/";
Context mContext;
int mThumbIds = 18;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
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(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(95, 95));
// imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(4, 4, 4, 4);
} else {
imageView = (ImageView) convertView;
}
Picasso.with(this.mContext)
.load(URL + position +".jpg")
// .placeholder(R.drawable.loader).error(R.drawable.ic_launcher).fit()
.into(imageView);
// imageView.setImageResource(mThumbIds[position]);
return imageView;
}
// references to our images }
in here i try get image from gridview, i try use intent, it's not work
Details Image
public class SingleViewActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.single_view);
Intent i = getIntent();
int position = i.getExtras().getInt("arg3");
ImageAdapter imageAdapter = new ImageAdapter(this);
ImageView imageView = (ImageView) findViewById(R.id.SingleView);
imageView.setImageResource(imageAdapter.getItemViewType(position));}}
can anyone help me
Two points:
Pass the URL of the image you selected from MainActivity to SingleViewActivity.
Use ImageLoader, and do not modify the width/heigth otherwise the memory cache will miss.
Others the cache will do everything for you.

How to get the view refrence in local method

I M clicking an image on a grid view and trying to hide the images in it neighboe recursively but it is not happening. Code reaches the if statement but cant hide image
public class GridLayoutActivity extends Activity {
GridView gridView;
public Integer[] mThumbIds = {
R.drawable.right, R.drawable.right,
R.drawable.right, R.drawable.left,
R.drawable.left, R.drawable.left,
R.drawable.left, R.drawable.up,
R.drawable.left, R.drawable.up,
R.drawable.down, R.drawable.right,
R.drawable.up, R.drawable.right,
R.drawable.up
};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.grid_layout);
gridView = (GridView) findViewById(R.id.grid_view);
// Instance of ImageAdapter Class
gridView.setAdapter(new ImageAdapter(this));
/**
* On Click event for Single Gridview Item
* */
gridView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
recursive(position,v);
}
private void recursive(int position,View v) {
// TODO Auto-generated method stub
/* MobileArrayAdapter adapter=new MobileArrayAdapter(getBaseContext(), position);
System.out.println("Adapter set");
gridView.setAdapter(adapter);*/
System.out.println(v);
String imageName = getResources().getResourceEntryName(mThumbIds[position]);
System.out.println(imageName);
/*String up = getResources().getResourceEntryName(mThumbIds[position-3]);
String down = getResources().getResourceEntryName(mThumbIds[position+3]);
String right = getResources().getResourceEntryName(mThumbIds[position+1]);
String left = getResources().getResourceEntryName(mThumbIds[position-1]);
*/
if(imageName.equalsIgnoreCase("up"))//&&!down.equalsIgnoreCase("down"))
{
Toast.makeText(getApplicationContext(), "UP", Toast.LENGTH_SHORT).show();
ImageView imageView=(ImageView)v;
imageView.setVisibility(View.INVISIBLE);
try{
recursive(position-3, v);
}
catch(Exception e)
{
e.printStackTrace();
}
}
if(imageName.equalsIgnoreCase("down"))//&&!up.equalsIgnoreCase("up"))
{
ImageView imageView=(ImageView)v;
imageView.setVisibility(View.INVISIBLE);
}
if(imageName.equalsIgnoreCase("left"))//&&!right.equalsIgnoreCase("right"))
{System.out.println("M HERE");
ImageView imageView=(ImageView)v;
imageView.setVisibility(View.INVISIBLE);
}
if(imageName.equalsIgnoreCase("right"))//&&!left.equalsIgnoreCase("left"))
{
ImageView imageView=(ImageView)v;
imageView.setVisibility(View.INVISIBLE);
}
}
});
}
/*
class MobileArrayAdapter extends ArrayAdapter
{
Context context2;
int position ;
public MobileArrayAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
// TODO Auto-generated constructor stub
position =textViewResourceId;
context2=context;
}
#Override
public View getView(int positionn, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ImageView imageView = new ImageView(context2);
imageView.setImageResource(mThumbIds[position]);
imageView.setVisibility(View.INVISIBLE);
return super.getView(position, convertView, parent);
}
}*/
}

Creating Image Gallery using Drawable images in res folder

I am new to android. I have a requirement now. I need to add around 10 images in the res/drawable folder and on running the app i should display this images on a listView.and on selecting any of the image i should display this image in the new activity should be able to zoom in and zoom out. Please help me out to figure out this with the sample code.
Thanks in advance.
this example for create Gallery, select one and set to selectedImageView.
so after that you can do everything with selectedImageView.
public class MyActivity extends Activity{
private int selectedImagePosition = 0;
private ImageView selectedImageView;
private List<Drawable> drawables;
private Gallery gallery;
#Override
public void onCreate(Bundle savedInstanceState) {
selectedImageView = (ImageView) view.findViewById(R.id.selected_imageview);
getDrawablesList();
gallery = (Gallery) view.findViewById(R.id.Gallery);
gallery.setAdapter(new ImageAdapter(getActivity().getApplicationContext()));
gallery.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, final int position, long id) {
setSelectedImage(selectedImagePosition);
}
});
private void getDrawablesList() {
drawables = new ArrayList<Drawable>();
drawables.add(getResources().getDrawable(R.drawable.res1));
drawables.add(getResources().getDrawable(R.drawable.res2));
drawables.add(getResources().getDrawable(R.drawable.res3));
drawables.add(getResources().getDrawable(R.drawable.res4));
}
private void setSelectedImage(int selectedImagePosition) {
BitmapDrawable bd = (BitmapDrawable) drawables.get(selectedImagePosition);
Bitmap b = Bitmap.createScaledBitmap(bd.getBitmap(), (int) (bd.getIntrinsicHeight() * 0.9), (int) (bd.getIntrinsicWidth() * 0.7), false);
selectedImageView.setImageBitmap(b);
selectedImageView.setScaleType(ScaleType.FIT_XY);
}
You can use a GalleryView
Refer to the link below for more help:
http://mobiforge.com/designing/story/understanding-user-interface-android-part-3-more-views
private ImageView selectedImageView;
private TextView _nameTextView;
private Gallery gallery;
Integer[] imageIDs = { R.drawable.hbath, R.drawable.hfood,
R.drawable.hmedicine, R.drawable.htherapy, R.drawable.htoilet,
R.drawable.hother };
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
selectedImageView = (ImageView) findViewById(R.id.imageSwitcher1);
_nameTextView = (TextView) findViewById(R.id.NameTextView);
gallery = (Gallery) findViewById(R.id.gallery1);
gallery.setAdapter(new ImageAdapter(this));
gallery.setOnItemClickListener(new OnItemClickListener() {
gallery.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int position, long arg3) {
// TODO Auto-generated method stub
if (position == 0) {
selectedImageView
.setImageResource(R.drawable.hbathbackground);
_nameTextView.setText("Toilet");
} else if (position == 1) {
selectedImageView
.setImageResource(R.drawable.hfoodbackground);
_nameTextView.setText("Food");
} else if (position == 2) {
selectedImageView
.setImageResource(R.drawable.hmedicinebackground);
_nameTextView.setText("Medicine");
} else if (position == 3) {
selectedImageView
.setImageResource(R.drawable.htherapybackground);
_nameTextView.setText("Therapy");
} else if (position == 4) {
selectedImageView
.setImageResource(R.drawable.htoiletbackground);
_nameTextView.setText("Bath");
} else if (position == 5) {
selectedImageView
.setImageResource(R.drawable.hotherbackground);
_nameTextView.setText("Other");
}
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
and ImageAdapter
public class ImageAdapter extends BaseAdapter {
private Context context;
private int itemBackground;
public ImageAdapter(Context c) {
context = c;
// ---setting the style---
TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);
itemBackground = a.getResourceId(
R.styleable.Gallery1_android_galleryItemBackground, 0);
a.recycle();
}
// ---returns the number of images---
public int getCount() {
return imageIDs.length;
}
// ---returns the ID of an item---
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
// ---returns an ImageView view---
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(context);
imageView.setImageResource(imageIDs[position]);
imageView.setBackgroundColor(0xFF000000);
// imgView.setImageBitmap(bitmap);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setLayoutParams(new Gallery.LayoutParams(155, 235));
imageView.setBackgroundResource(R.drawable.customborder2);
return imageView;
}
}

Android : Starting new activity using grid view.

I have used BaseAdapter for grid view, in getView method of BaseAdapter I am starting new activity using context of main activity, but when i tap on icon i getting message application not found for 1 sec and then new activity starts, in log cat I get error PakageManager$PakageNotFoundEception. Thanks in advance for your help.
public class ImageAdapter extends BaseAdapter{
private Context mContext;
ArrayList<Integer> imgid;
Uri uri;
public ImageAdapter(Context c, ArrayList<Integer> imgid2 ) {
super();
mContext = c;
imgid=imgid2;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return imgid.size();
}
#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, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ImageView imageView;
if (convertView == null) { // if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
// imageView.setClickable(true);
imageView.setPadding(8, 8, 8, 8);
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.d("onClick","position ["+position+"]");
try {
final Intent intent= new Intent("settings");
intent.setClass(mContext, listAvtivity.class); //*******
if(imgid.get(position)==R.drawable.setting4){mContext.startActivity(intent);}
if(imgid.get(position)==R.drawable.google){uri = Uri.parse("http://www.google.com");}
if(imgid.get(position)==R.drawable.yahoo){uri = Uri.parse("http://www.yahoo.com");}
if(imgid.get(position)==R.drawable.skype){uri = Uri.parse("http://www.skype.com");}
Intent intent1 = new Intent(Intent.ACTION_VIEW, uri);
mContext.startActivity(intent1);
} catch (Exception e) {
// TODO: handle exception
Log.e("onClick Error: ",e.toString());
}
}
});
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(imgid.get(position));
return imageView;
}
}
Don't use View.OnClickListener inside your getView method. Use OnItemClickListener and add it to your GridView to get notified when the user clicks on an item.

How can I change Image on Gridview Runtime?

I have one GridView with 3 Column and 3 Rows I want to change Image when User Click any two Images.
for Example I Click First Row 1 and Column 3 Image and Secondly I Click on Row 3 and Column 2 show now i want to change this two Images like Swap the Image How is it Possible ?
public class MainActivity extends Activity {
/** Called when the activity is first created. */
GridView gridView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
gridView = (GridView)findViewById(R.id.gridviewmy);
gridView.setAdapter(new ImageAdapter(this));
final ImageAdapter im = new ImageAdapter(this);
gridView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
// TODO Auto-generated method stub
int i=0; int j=0;
if( i != 0){
j=arg2;
System.out.println("First Click "+j);
}else{
i=arg2;
System.out.println("Second Click "+i);
}
im.getItem(arg2);
//im.changeImage();
Toast.makeText(MainActivity.this, ""+arg2, Toast.LENGTH_SHORT).show();
System.out.println("AdapterView "+arg0);
System.out.println("View "+arg1);
System.out.println("Integer "+arg2);
System.out.println("long "+arg3);
}
});
}
}
class ImageAdapter extends BaseAdapter{
private Context mContext;
ImageView iView;
public ImageAdapter(Context c){
this.mContext = c;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return mThumbIds.length;
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
System.out.println("Item Is :-"+mThumbIds[position].toString());
return position;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
System.out.println("Geting Id of Item "+mThumbIds[position]);
if(iView != null){
iView.setImageResource(mThumbIds[0]);
Toast.makeText(mContext, "Call", Toast.LENGTH_SHORT).show();
}
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if( convertView == null){
iView = new ImageView(mContext);
iView.setLayoutParams(new GridView.LayoutParams(85, 85));
iView.setScaleType(ImageView.ScaleType.CENTER_CROP);
iView.setPadding(8,8,8,8);
}else{
iView = (ImageView)convertView;
}
iView.setImageResource(mThumbIds[position]);
return iView;
}
private Integer[] mThumbIds = {
R.drawable.a_bhaibij, R.drawable.a_dashera, R.drawable.a_dipawali,
R.drawable.a_gandhi, R.drawable.a_holi, R.drawable.a_indepe,
R.drawable.a_janmastmi, R.drawable.a_kite, R.drawable.a_newyear
};
public void changeImage(){
iView.setImageResource(mThumbIds[5]);
}
}
Swaping the images in the GridView is very simple.What you have to do is
1* Store the cliked position,where you want to perform the swaping .
2* By using those two values perform the swap operation on mThumbIds array.
3* Finally invoke the notifyDataSetChanged() method on the Adapter object i.e im.notifyDataSetChanged();
public class MainActivity extends Activity {
/** Called when the activity is first created. */
int i=0;
int firstClick,secondClick;
GridView gridView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
gridView = (GridView)findViewById(R.id.gridviewmy);
gridView.setAdapter(new ImageAdapter(this));
final ImageAdapter im = new ImageAdapter(this);
gridView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
// TODO Auto-generated method stub
i++;
if( i %2!=0){
firstClick=arg2;
}else{
secondClick=arg2;
Integer help=new Interger(mThumbIds[firstClick]);
mThumbIds[firstClick]=mThumbIds[secondClick];
mThumbIds[secondClick]=help;
notifyDataSetChanged();
System.out.println("Second Click "+i);
}
}
});
}
}
class ImageAdapter extends BaseAdapter{
private Context mContext;
ImageView iView;
public ImageAdapter(Context c){
this.mContext = c;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return mThumbIds.length;
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
System.out.println("Item Is :-"+mThumbIds[position].toString());
return position;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
System.out.println("Geting Id of Item "+mThumbIds[position]);
if(iView != null){
iView.setImageResource(mThumbIds[0]);
Toast.makeText(mContext, "Call", Toast.LENGTH_SHORT).show();
}
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if( convertView == null){
iView = new ImageView(mContext);
iView.setLayoutParams(new GridView.LayoutParams(85, 85));
iView.setScaleType(ImageView.ScaleType.CENTER_CROP);
iView.setPadding(8,8,8,8);
}else{
iView = (ImageView)convertView;
}
iView.setImageResource(mThumbIds[position]);
return iView;
}
private Integer[] mThumbIds = {
R.drawable.a_bhaibij, R.drawable.a_dashera, R.drawable.a_dipawali,
R.drawable.a_gandhi, R.drawable.a_holi, R.drawable.a_indepe,
R.drawable.a_janmastmi, R.drawable.a_kite, R.drawable.a_newyear
};
}
I think this may solve you problem.
All the best.
Also do the following for updating the grid view images to complete the swap operation:
im.notifyDataSetChanged();
gridView.setAdapter(im);
gridView.invalidateViews()
notifyDataSetChanged(); did not work for me. eclipse gave an error.
so instead of searching for the real solution, if there is one, I just reloaded the java page.
Of course I am saving the state of the images in the gridview (adapter) in internal storage in a file named graphics. So on reload of the java page it repaints with correct images.
It works.

Categories

Resources