Creating Image Gallery using Drawable images in res folder - android

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;
}
}

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.

Full screen image gallery 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));

getting image id from ImageAdapter

I'am new to android and I've a project of making a grid view of images and onclick on image it shows it , thats the ImageAdapter class code
public class ImageAdapter extends BaseAdapter{
private static LayoutInflater inflater = null;
private Activity activity;
private String mode = "";
int[] images=null ;
public ImageAdapter(Activity act, String mode , int[] images){
inflater = (LayoutInflater) act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
activity = act;
this.mode = mode;
this.images= images ;
}
public ImageView getImage(int pos)
{
ImageView im = (ImageView) getItem(pos);
return im ;
}
#Override
public int getCount() {
return images.length;
}
#Override
public Object getItem(int position) {
return new Integer(images[position]);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View view, ViewGroup parent) {
if(mode.equalsIgnoreCase("grid")){
if (view == null) {
view = inflater.inflate(R.layout.each_image1, null);
}
ImageView iv = (ImageView)view.findViewById(R.id.imageView);
iv.setImageResource(images[position]);
} else if(mode.equalsIgnoreCase("gallery")){
if (view == null) {
view = inflater.inflate(R.layout.each_image_gallery, null);
}
ImageView iv = (ImageView)view.findViewById(R.id.imageView);
iv.setImageResource(images[position]);
}
return view;
}
}
thats my grid activity
public class GridActivity extends Activity {
GridView grid = null;
public static ImageAdapter adapter1 ;
public static ImageAdapter adapter2 ;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.grid);
adapter1= new ImageAdapter(GridActivity.this, "grid" , Images.images1);
adapter2 = new ImageAdapter(GridActivity.this, "grid" , Images.images2);
Intent i = this.getIntent();
if (i!=null)
{
String unique = i.getExtras().getString("Unique");
if (unique.equals("islam"))
{
Toast.makeText(GridActivity.this, "islam", Toast.LENGTH_LONG).show();
grid = (GridView)findViewById(R.id.gridView1);
grid.setAdapter(adapter1);
}
if (unique.equals("natural"))
{
Toast.makeText(GridActivity.this, "nat", Toast.LENGTH_LONG).show();
grid = (GridView)findViewById(R.id.gridView1);
grid.setAdapter(adapter2);
}
}
grid.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int pos,long id) {
Intent i = new Intent(GridActivity.this, imgPrevActivity.class);
i.putExtra("selectedIntex", pos);
startActivity(i);
Toast.makeText(GridActivity.this,"ddd",Toast.LENGTH_LONG).show();
}
});
}
}
and this is the activity where it supposes to show any clicked on image
public class imgPrevActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.each_image1);
ImageView im = (ImageView)findViewById(R.id.imageView);
int pos = getIntent().getExtras().getInt("selectedIntex");
// ImageAdapter adapter = new ImageAdapter(imgPrevActivity.this, "image prev", null);
long Id= GridActivity.adapter1.getItemId(pos);
im.setImageResource((int) Id);
}
}
i've tried to get the position of image clicked on from the grid
then getting the Id from the position
and setting the image view to that ID
but it doesnt work !!
thats where the images are put in arrays
public class Images {
public static int[] images1 = {
R.drawable.buds, R.drawable.cherry_34,
R.drawable.clouds_2, R.drawable.coffee_beans_2,
R.drawable.death_valley_sand_dunes
};
public static int[] images2= {
R.drawable.morning_glory_pool,
R.drawable.pink_flowers, R.drawable.sun_flower,
R.drawable.sunrise_3, R.drawable.yellow_rose_3,
};
}
You have to pass your image resource id with intent. The list item id of adapter is useless because you have new Activity on the screen.
grid.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int pos,long id) {
Intent i = new Intent(GridActivity.this, imgPrevActivity.class);
i.putExtra("selectedIntex", grid.getAdapter().getItem(pos);
startActivity(i);
Toast.makeText(GridActivity.this,"ddd",Toast.LENGTH_LONG).show();
}
});
and change your code in your ImgPreviewActivity:
public class imgPrevActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.each_image1);
ImageView im = (ImageView)findViewById(R.id.imageView);
int pos = getIntent().getIntExtra("selectedIntex", 0);
im.setImageResource(pos);
}
}
I think it should work

Android viewflipper and gallery

I am trying to develop a application which contain a gallery with view flipper.
Gallery shows all the images.What I want to do is, the image which is shown in the view flipper should be highlighted in the gallery.
Currently I am able to highlight it on onClick.But not able to make communication between both.
I tried using following method
viewflipper.getDisplayedChild();
but it doesn't work.
following is my code
public class SliderActivity extends Activity
{
int mFlipping = 0 ;
public int currentimageindex=0;
Timer timer;
TimerTask task;
ImageView slidingimage;
android.widget.ViewFlipper flipper;
Gallery gallery;
private int[] IMAGE_IDS =
{
R.drawable.android0, R.drawable.android1, R.drawable.android2,R.drawable.android3,R.drawable.android4
};
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.mygame);
flipper = (android.widget.ViewFlipper) findViewById(R.id.flipper1);
for(int i=0;i<IMAGE_IDS.length;i++)
{
// This will create dynamic image view and add them to ViewFlipper
ImageView image = new ImageView(getApplicationContext());
image.setBackgroundResource(IMAGE_IDS[i]);
flipper.addView(image);
}
gallery=(Gallery)findViewById(R.id.gallery1);
gallery.setAdapter(new ImageAdapter(this));
if(mFlipping==0){
/** Start Flipping */
flipper.startFlipping();
mFlipping=1;
//mButton.setText(R.string.str_btn_stop);
}
else{
/** Stop Flipping */
flipper.stopFlipping();
mFlipping=0;
// mButton.setText(R.string.str_btn_start);
}
}
public class ImageAdapter extends BaseAdapter
{
int mGalleryItemBackground;
private Context mContext;
public ImageAdapter(Context c)
{
mContext = c;
TypedArray a = obtainStyledAttributes(R.styleable.Theme);
mGalleryItemBackground = a.getResourceId(
R.styleable.Theme_android_galleryItemBackground,0);
a.recycle();
}
public int getCount()
{
return IMAGE_IDS.length;
}
public Object getItem(int position)
{
return position;
}
public long getItemId(int position)
{
return position;
}
public View getView(int position,View convertView, ViewGroup parent)
{
ImageView i = new ImageView(mContext);
i.setImageResource(IMAGE_IDS[position]);
i.setLayoutParams(new Gallery.LayoutParams(150, 100));
i.setScaleType(ImageView.ScaleType.FIT_XY);
i.setBackgroundResource(mGalleryItemBackground);
return i;
}
}
public void onClick(View v) {
finish();
android.os.Process.killProcess(android.os.Process.myPid());
}
}
If I understand your question... I think you need to use setOnItemSelectedListener...
gallery.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapter, View gallery,
int position, long arg3) {
myPosition = position;
//tell your flipper something useful here using the current position of the gallery
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
And talk to your flipper from in the onItemSelected callback.

Why can't i get the text written in EditText?

public class RenkKorluguTesti extends Activity {
/** Called when the activity is first created. */
ImageView imageView1;
EditText editText1=null;
Button button1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.galeri);
imageView1 = (ImageView) findViewById(R.id.imageView1);
editText1 = (EditText) findViewById(R.id.editText1);
button1 = (Button) findViewById(R.id.button1);
Gallery gallery = (Gallery) findViewById(R.id.gallery);
gallery.setAdapter(new ImageAdapter(this));
gallery.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position, long id) {
switch(position) {
case 0:{
imageView1.setImageResource(R.drawable.plate01);
break;
}
case 1: imageView1.setImageResource(R.drawable.plate02); break;
case 2: imageView1.setImageResource(R.drawable.plate03); break;
}
//Toast.makeText(RenkKorluguTesti.this, "" + position, Toast.LENGTH_SHORT).show();
}
});
}
public void tikla(View v) {
if(editText1.getText().toString()=="12")
editText1.setText("yov");
//Toast.makeText(RenkKorluguTesti.this, "Birinci testi geƧtiniz.",Toast.LENGTH_SHORT).show();
}
public class ImageAdapter extends BaseAdapter {
int mGalleryItemBackground;
private Context mContext;
private Integer[] mImageIds = {
R.drawable.plate01,
R.drawable.plate02,
R.drawable.plate03,
};
public ImageAdapter(Context c) {
mContext = c;
TypedArray attr = mContext.obtainStyledAttributes(R.styleable.RenkKorluguTesti);
mGalleryItemBackground = attr.getResourceId(
R.styleable.RenkKorluguTesti_android_galleryItemBackground, 0);
attr.recycle();
}
public int getCount() {
return mImageIds.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(mContext);
imageView.setImageResource(mImageIds[position]);
imageView.setLayoutParams(new Gallery.LayoutParams(150, 100));
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setBackgroundResource(mGalleryItemBackground);
return imageView;
}
}
}
I want to make a Toast if the text written is equal 12 in EditText box.
I used editText1.getText().toString() method but it seems not working.
What's wrong? Could you please check public void tikla() method? Thx.
On Java you cannot do this kind of comparation:
if(editText1.getText().toString()=="12")
You need to do:
if(editText1.getText().toString().equalsIgnoreCase("12"))
or
if(editText1.getText().toString().compareTo("12")==0)
This comparison in your code is not correct.
(editText1.getText().toString()=="12")
Comparing Strings is done via equals() not == operator.
You can do something like this:
if (editText1.getText().toString().compareTo("12") == 0)
using the java.lang.String.compareTo(java.lang.String) method.

Categories

Resources