android - get resource id from gallery - android

So i have a program with a Gallery (made with pictures from the resources folder), a Button and an ImageView
I want to choose one of the images from the gallery, press the button, and change the ImageView bitmap to display the one selected from the gallery...
I have the changing methods from activity to activity all fixed... but i'm just having trouble getting the id of the image picked, the integer, drawable, long, string... or whatever it helps...
Here's the code:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gallery);
Gallery g = (Gallery) findViewById(R.id.gallery);
g.setAdapter(new ImageAdapter(this));
g.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position, long id) {
//CODE TO KNOW IF PICKED THE 1ST OR 2ND IMAGE
//Toast.makeText(Galeria.this, v.getId() + "", Toast.LENGTH_SHORT).show();
//Toast.makeText(Galeria.this, parent.getItemIdAtPosition(position) + "", Toast.LENGTH_SHORT).show();
//Toast.makeText(Galeria.this, id + "", Toast.LENGTH_SHORT).show();
}
});
}
public class ImageAdapter extends BaseAdapter {
int mGalleryItemBackground;
private Context mContext;
private Integer[] mImageId = {
R.drawable.image1, //first image
R.drawable.image2, //second image
};
public ImageAdapter(Context c) {
mContext = c;
TypedArray a = c.obtainStyledAttributes(R.styleable.Gallery1);
mGalleryItemBackground = a.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 0);
a.recycle();
}
public int getCount()
return mImageId.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(mImageId[position]);
i.setLayoutParams(new Gallery.LayoutParams(400, 225));
i.setScaleType(ImageView.ScaleType.FIT_XY);
i.setBackgroundResource(mGalleryItemBackground);
return i;
}
}
The three Toast there on the OnItemClick void are my attempts of showing the id... but only showing the position of the image from the gallery.
Is there any other solution? I've read something about the dynamics ids... but could understand well...
Where should I put the code to get my desired id??
Any Help? Thanks.

g.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position, long id) {
//CODE TO KNOW IF PICKED THE 1ST OR 2ND IMAGE
int imageId = (Integer) (parent.getItemAtPosition(position));
myImageView.setImageResource(imageId);
//Toast.makeText(Galeria.this, v.getId() + "", Toast.LENGTH_SHORT).show();
//Toast.makeText(Galeria.this, parent.getItemIdAtPosition(position) + "", Toast.LENGTH_SHORT).show();
//Toast.makeText(Galeria.this, id + "", Toast.LENGTH_SHORT).show();
}
});
In your adapter getItem should return the object of the dataset
public Object getItem(int position) {
return (mImageId[position]);
}

You can do something like this to get the image view resource as drawable on your itemClickListener :
(ImageView)view.getBackground()
This will return image as drawable of selected image view's resource.

Related

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.

Loading images from web into image gallery

I am using the following to code make an image gallery, but I would like to use images from the web instead of from the SD card. Is there a way to do this? Currently the code requires and integer value for the image. Or do I have to download the images to the SD first?
public class viewimages extends Activity {
private Gallery gallery;
private ImageView imgView;
private Integer[] Imgid = {
R.drawable.a_1, R.drawable.a_2, R.drawable.a_3, R.drawable.a_4
};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.viewimages);
imgView = (ImageView)findViewById(R.id.ImageView01);
imgView.setImageResource(Imgid[0]);
gallery = (Gallery) findViewById(R.id.examplegallery);
gallery.setAdapter(new AddImgAdp(this));
gallery.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position, long id) {
imgView.setImageResource(Imgid[position]);
}
});
}
public class AddImgAdp extends BaseAdapter {
int GalItemBg;
private Context cont;
public AddImgAdp(Context c) {
cont = c;
TypedArray typArray = obtainStyledAttributes(R.styleable.GalleryTheme);
GalItemBg = typArray.getResourceId(R.styleable.GalleryTheme_android_galleryItemBackground, 0);
typArray.recycle();
}
public int getCount() {
return Imgid.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 imgView = new ImageView(cont);
imgView.setImageResource(Imgid[position]);
imgView.setLayoutParams(new Gallery.LayoutParams(80, 70));
imgView.setScaleType(ImageView.ScaleType.FIT_XY);
imgView.setBackgroundResource(GalItemBg);
return imgView;
}
}
}
You can do something like this:
URL url = new URL("http://www.website.com/image.jpg");
Bimtap bmp = BitmapFactory.decodeStream(url.openConnection()
.getInputStream());
Then you'll need to change where you're doing "setImageResource" to "setImageBitmap" and supply the 'bmp' object once it has been downloaded and loaded up.
[Edit]
It's also important to make sure your android manifest has internet permissions.
<uses-permission android:name="android.permission.INTERNET" />
[Edit for comment question]
If you want to do this for multiple images, and the images aren't based on an id_number (basically if the images aren't something like 0001.jpg, 0002.jpg, etc.) create a string ArrayList that will hold the names of all the items, and then iterate through it. E.g.,
List<String> imageNames = new ArrayList<String>();
imageNames.add("image.jpg");
imageNames.add("thisPhoto.jpg");
//etc. until you've added all images
for (int i = 0; i < imageNames.size(); i++){
URL url = new URL("http://www.website.com/" + imageNames.get(i));
Bimtap bmp = BitmapFactory.decodeStream(url.openConnection()
.getInputStream());
}
Hope that helps

Android: making gallery infinite loop of images

I'm using a gallery in my project in which I have added four images and I want it to be infinite from both right side and left side. How do I accomplish this?
The main idea is that in your getView method, you have to use
position = position % imagesArray.length;
if (position < 0)
position = position + imagesArray.length;
imagesArray is the array that holds the images in your res folder. For example:
public class CircularGallery extends Activity {
/** Called when the activity is first created. */
private Integer[] imagesArray = { R.drawable.picture1, R.drawable.picture2, R.drawable.picture3, R.drawable.picture4, R.drawable.picture5, R.drawable.picture6 , R.drawable.picture7 };
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Gallery g = (Gallery) findViewById(R.id.gallery);
g.setAdapter(new ImageAdapter(this));
g.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position, long id) {
if (position >= imagesArray.length) {
position = position % imagesArray.length;
}
Toast.makeText(CircularGallery.this, "" + position, Toast.LENGTH_SHORT).show();
}
});
}
public class ImageAdapter extends BaseAdapter {
int mGalleryItemBackground;
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);
mGalleryItemBackground = a.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 0);
a.recycle();
}
public int getCount() {
return Integer.MAX_VALUE;
}
public Object getItem(int position) {
if (position >= imagesArraylength) {
position = position % mImageIds.length;
}
return position;
}
public long getItemId(int position) {
if (position >= imagesArray.length) {
position = position % imagesArray.length;
}
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView i = new ImageView(mContext);
if (position >= imagesArray.length) {
position = position % imagesArray.length;
}
i.setImageResource(imagesArray[position]);
i.setLayoutParams(new Gallery.LayoutParams(80, 80));
i.setScaleType(ImageView.ScaleType.FIT_XY);
i.setBackgroundResource(mGalleryItemBackground);
return i;
}
public int checkPosition(int position) {
if (position >= imagesArray.length) {
position = position % imagesArray.length;
}
return position;
}
}}
Also, some developers have done such a functionality and you can find sources on their blogs
http://abhinavasblog.blogspot.com/2011/09/android-infinite-looping-gallery.html
http://blog.blundellapps.com/infinite-scrolling-gallery/
if you want to set image showing on right side, just set g.setSelection(image)
My first guess is to change adapter data, i.e. if you detect that you are on the "right edge", then get your first image and add it to the end, then take second image and so on...

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.

how to display video thumbnails?

i am trying to show my arraylist value in my tablayout. now i am successfully passed two values in my two tabhost it's working. i have three tabhost 2tab host working fine. so i am trying show my stored path value video thumb. how to show my stored path value to video thumb nail? i am trying to show but i am getting error
error is:
The type of the expression must be an array type but it resolved to ArrayList<String>
line:
imgVw.setImageBitmap(getImage(tabview.videoList[position]));
full source code:
public class video extends Activity {
//set constants for MediaStore to query, and show videos
//flag for which one is used for images selection
private Gallery _gallery;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mavideo);
//set GridView for gallery
_gallery = (Gallery) findViewById(R.id.videoGrdVw);
//set gallery adapter
setGalleryAdapter();
}
private void setGalleryAdapter() {
_gallery.setAdapter(new VideoGalleryAdapter(this));
}
//
private class VideoGalleryAdapter extends BaseAdapter
{private Context mContext;
public VideoGalleryAdapter(Context c)
{
mContext = c;
}
public int getCount()
{
return tabview.videoList.size();
}
public Object getItem(int position)
{
return position;
}
public long getItemId(int position)
{
return position;
}
public View getView(int position, View convertView, ViewGroup parent)
{
ImageView imgVw= new ImageView(mContext);;
try
{
if(convertView!=null)
{
imgVw= (ImageView) convertView;
}
imgVw.setImageBitmap(getImage(tabview.videoList[position]));
imgVw.setLayoutParams(new Gallery.LayoutParams(96, 96));
imgVw.setPadding(8, 8, 8, 8);
}
catch(Exception ex)
{
System.out.println("StartActivity:getView()-135: ex " + ex.getClass() +", "+ ex.getMessage());
}
return imgVw;
}
// Create the thumbnail on the fly
private Bitmap getImage(int id) {
Bitmap thumb = MediaStore.Video.Thumbnails.getThumbnail(
getContentResolver(),
id, MediaStore.Video.Thumbnails.MICRO_KIND, null);
return thumb;
}
}
}
note:
parxmlactivity.java this is my main class here i am passing my value using below code:
sdcardImages.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position, long id) {
Intent intent = new Intent(ParxmlActivity.this, tabview.class);
intent.putExtra("spec",model_List.get(position).spec);
intent.putStringArrayListExtra("imageList", model_List.get(position).imageList);
intent.putStringArrayListExtra("videoList", model_List.get(position).videoList);
startActivity(intent);
}
});
and i am getting this passed value in below code in tabview.java class file:
tab_intent=tabview.this.getIntent().getExtras();
spec=tab_intent.getString("spec");
imageList = tab_intent.getStringArrayList("imageList");
videoList = tab_intent.getStringArrayList("videoList");

Categories

Resources