I wish to develop an Slide Show application in android. My requirement is, i had a set of 20 (not a constant) images in my drawable folder (not a External Storage). Now i show this set of images with in a certain constat time limit. I had a code for slideshow for android basic sample in that code how can i set time interval. My code is,
SlideShow.java:
package com.fsp.slideview;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.Gallery.LayoutParams;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher;
public class SlideShow extends Activity implements
AdapterView.OnItemSelectedListener, ViewSwitcher.ViewFactory {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.slide_show);
mSwitcher = (ImageSwitcher) findViewById(R.id.switcher);
mSwitcher.setFactory(this);
mSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_in));
mSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_out));
Gallery g = (Gallery) findViewById(R.id.gallery);
g.setAdapter(new ImageAdapter(this));
g.setOnItemSelectedListener(this);
}
public void onItemSelected(AdapterView parent, View v, int position, long id) {
mSwitcher.setImageResource(mImageIds[position]);
}
public void onNothingSelected(AdapterView parent) {
}
public View makeView() {
ImageView i = new ImageView(this);
i.setBackgroundColor(0xFF000000);
i.setScaleType(ImageView.ScaleType.FIT_CENTER);
i.setLayoutParams(new ImageSwitcher.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
return i;
}
private ImageSwitcher mSwitcher;
public class ImageAdapter extends BaseAdapter {
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds.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(mThumbIds[position]);
i.setAdjustViewBounds(true);
i.setLayoutParams(new Gallery.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
i.setBackgroundResource(R.drawable.picture_frame);
return i;
}
private Context mContext;
}
private Integer[] mThumbIds = { R.drawable.img1, R.drawable.img2,
R.drawable.img2, R.drawable.img3, R.drawable.img4, R.drawable.img5,
R.drawable.img6, R.drawable.img7 };
private Integer[] mImageIds = { R.drawable.img1, R.drawable.img1,
R.drawable.img2, R.drawable.img3, R.drawable.img4, R.drawable.img5,
R.drawable.img6, R.drawable.img7 };
}
and the xml file is, slide_show.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageSwitcher
android:id="#+id/switcher"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" />
<Gallery
android:id="#+id/gallery"
android:layout_width="fill_parent"
android:layout_height="60dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:background="#55000000"
android:gravity="center_vertical"
android:spacing="16dp" />
</RelativeLayout>
final Handler mHandler = new Handler();
int delay = 1000; // delay for 1 sec.
int period = 8000; // repeat every 4 sec.
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
mHandler.post(mUpdateResults);
}
}, delay, period);
}
Related
I'm working on a project that contains some images in gallery, I want to add text with button that changes respective to selected image. How can I add this?
This is the code:
import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
public class GalleryView extends Activity {
Integer[] pics = {
R.drawable.antartica1,
R.drawable.antartica2,
R.drawable.antartica3,
R.drawable.antartica4,
R.drawable.antartica5,
R.drawable.antartica6,
R.drawable.antartica7,
R.drawable.antartica8,
R.drawable.antartica9,
R.drawable.antartica10
};
ImageView imageView;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Gallery ga = (Gallery)findViewById(R.id.Gallery01);
ga.setAdapter(new ImageAdapter(this));
imageView = (ImageView)findViewById(R.id.ImageView01);
ga.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Toast.makeText(getBaseContext(),
"You have selected picture " + (arg2+1) + " of Antartica",
Toast.LENGTH_SHORT).show();
imageView.setImageResource(pics[arg2]);
}
});
}
public class ImageAdapter extends BaseAdapter {
private Context ctx;
int imageBackground;
public ImageAdapter(Context c) {
ctx = c;
TypedArray ta = obtainStyledAttributes(R.styleable.Gallery1);
imageBackground = ta.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 1);
ta.recycle();
}
public int getCount() {
return pics.length;
}
public Object getItem(int arg0) {
return arg0;
}
public long getItemId(int arg0) {
return arg0;
}
public View getView(int arg0, View arg1, ViewGroup arg2) {
ImageView iv = new ImageView(ctx);
iv.setImageResource(pics[arg0]);
iv.setScaleType(ImageView.ScaleType.FIT_XY);
iv.setLayoutParams(new Gallery.LayoutParams(150,120));
iv.setBackgroundResource(imageBackground);
return iv;
}
}
}
Xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Gallery
android:id="#+id/Gallery01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"></Gallery>
<ImageView
android:id="#+id/ImageView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></ImageView>
</LinearLayout>
well you can make your own Customised gallery in which every image can be viewed in a grid view followed by a text view below the thumbnail and a button (may be needed for deleting the image).
To view the images dynamically from the memory card you have to implement ArrayList or you can take other oferings of collection framework ..
i did the same and it worked with mine , hopefully will work with you too.
Any ideas of how to make tis galley view look nicer? as its not the best looking at the moment. Any help would be much appreciated and it can be anything from sizing to borders and even layout :)
The .xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Gallery android:id="#+id/gallery1" android:layout_width="fill_parent"
android:layout_height="wrap_content"></Gallery>
<ImageView android:id="#+id/ImageView01"
android:layout_gravity="center_vertical|center_horizontal"
android:layout_width="wrap_content" android:layout_height="wrap_content"></ImageView>
</LinearLayout>
My Activity:
package kevin.erica.box;
import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
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.Gallery;
import android.widget.ImageView;
public class App2Activity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gallery);
Gallery g = (Gallery) findViewById(R.id.gallery1);
g.setAdapter(new ImageAdapter(this));
g.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(#SuppressWarnings("rawtypes") AdapterView parent, View v, int position, long id) {
ImageView imageview = (ImageView) findViewById(R.id.ImageView01);
imageview.setImageResource(mImageIds[position]);
}
});
}
public Integer[] mImageIds = {
R.drawable.lemon,
R.drawable.kevin,
R.drawable.mirror
};
public class ImageAdapter extends BaseAdapter {
int mGalleryItemBackground;
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
TypedArray a = obtainStyledAttributes(R.styleable.HelloGallery);
mGalleryItemBackground = a.getResourceId(
R.styleable.HelloGallery_android_galleryItemBackground, 0);
a.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 i = new ImageView(mContext);
i.setImageResource(mImageIds[position]);
i.setLayoutParams(new Gallery.LayoutParams(150, 100));
i.setScaleType(ImageView.ScaleType.FIT_XY);
i.setBackgroundResource(mGalleryItemBackground);
return i;
}
}
}
you can change the whole shape of Gallery by using coverflow one ,if you want or stack with yours and adjaust it .
check this link :
http://www.inter-fuser.com/2010/02/android-coverflow-widget-v2.html
hope this help
I have three buttons Image , Videos , Audios. I want to change the views in gallery with the audios , videos , images resources when corresponding button clicked so that only related image of the buttons appear in gallery.
My code is here:
XML layout:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Audios" >
</Button>
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Videos" >
</Button>
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Images" >
</Button>
</LinearLayout>
<Gallery
android:id="#+id/Gallery01"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</Gallery>
</LinearLayout>
<ImageView
android:id="#+id/ImageView01"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</ImageView>
Java file:
public class GalleryView extends Activity {
Integer[] pics = {
R.drawable.cloudy,
R.drawable.hazy,
R.drawable.mostlycloudyday,
R.drawable.partlycloud,
R.drawable.sunny,
R.drawable.sunrain,
R.drawable.thunderstorm,
R.drawable.weathercloudy,
};
ImageView imageView;
Button mAudio;
Button mVideo;
Button mImages;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mAudio=(Button) findViewById(R.id.button1);
Gallery ga = (Gallery)findViewById(R.id.Gallery01);
ga.setAdapter(new ImageAdapter(this));
imageView = (ImageView)findViewById(R.id.ImageView01);
ga.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int pos,
long arg3) {
Toast.makeText(getBaseContext(),
"You have selected picture " + (pos+1) + "of Gallery",
Toast.LENGTH_SHORT).show();
imageView.setImageResource(pics[pos]);
}
});
}
public class ImageAdapter extends BaseAdapter {
private Context ctx;
int imageBackground;
public ImageAdapter(Context c) {
ctx = c;
TypedArray ta = obtainStyledAttributes(R.styleable.Gallery1);
imageBackground = ta.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 1);
ta.recycle();
}
#Override
public int getCount() {
return pics.length;
}
#Override
public Object getItem(int arg0) {
return arg0;
}
#Override
public long getItemId(int arg0) {
return arg0;
}
#Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
ImageView iv = new ImageView(ctx);
iv.setImageResource(pics[arg0]);
iv.setScaleType(ImageView.ScaleType.FIT_XY);
iv.setLayoutParams(new Gallery.LayoutParams(150,120));
iv.setBackgroundResource(imageBackground);
return iv;
}
}
}
Anybody please help me with this,
Thanks.
For the changing the image in gallery, you must change the Resources image id in array. e.g change image for videos in int[] video_pics and so on.
import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.Toast;
public class GalleryView extends Activity implements OnClickListener {
int[] image_pics = { R.drawable.cloudy, R.drawable.hazy, R.drawable.mostlycloudyday, R.drawable.partlycloud,
R.drawable.sunny, R.drawable.sunrain, R.drawable.thunderstorm, R.drawable.weathercloudy, };
int[] video_pics = { R.drawable.cloudy, R.drawable.hazy, R.drawable.mostlycloudyday, R.drawable.partlycloud,
R.drawable.sunny, R.drawable.sunrain, R.drawable.thunderstorm, R.drawable.weathercloudy, };
int[] audio_pics = { R.drawable.cloudy, R.drawable.hazy, R.drawable.mostlycloudyday, R.drawable.partlycloud,
R.drawable.sunny, R.drawable.sunrain, R.drawable.thunderstorm, R.drawable.weathercloudy, };
ImageView imageView;
Button mAudio;
Button mVideo;
Button mImages;
ImageAdapter galleryAdapter;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mAudio = (Button) findViewById(R.id.button1);
mVideo = (Button) findViewById(R.id.button2);
mImages = (Button) findViewById(R.id.button3);
mAudio.setOnClickListener(this);
mVideo.setOnClickListener(this);
mImages.setOnClickListener(this);
Gallery ga = (Gallery) findViewById(R.id.Gallery01);
galleryAdapter = new ImageAdapter(this);
galleryAdapter.setResourseArray(image_pics);
ga.setAdapter(galleryAdapter);
imageView = (ImageView) findViewById(R.id.ImageView01);
ga.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3) {
Toast.makeText(getBaseContext(), "You have selected picture " + (pos + 1) + "of Gallery", Toast.LENGTH_SHORT)
.show();
imageView.setImageResource(pics[pos]);
}
});
}
#Override
public void onClick(View view) {
if (view == mAudio) {
if (galleryAdapter != null) {
galleryAdapter.setResourseArray(audio_pics);
galleryAdapter.notifyDataSetChanged();
}
} else if (view == mVideo) {
if (galleryAdapter != null) {
galleryAdapter.setResourseArray(video_pics);
galleryAdapter.notifyDataSetChanged();
}
} else if (view == mImages) {
if (galleryAdapter != null) {
galleryAdapter.setResourseArray(image_pics);
galleryAdapter.notifyDataSetChanged();
}
}
}
public class ImageAdapter extends BaseAdapter {
private Context ctx;
int imageBackground;
private int[] resourseArray = null;
public ImageAdapter(Context c) {
ctx = c;
TypedArray ta = obtainStyledAttributes(R.styleable.Gallery1);
imageBackground = ta.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 1);
ta.recycle();
}
#Override
public int getCount() {
return resourseArray.length;
}
public int[] getResourseArray() {
return resourseArray;
}
public void setResourseArray(int[] resourseArray) {
this.resourseArray = resourseArray;
}
#Override
public Object getItem(int arg0) {
return arg0;
}
#Override
public long getItemId(int arg0) {
return arg0;
}
#Override
public View getView(int position, View arg1, ViewGroup arg2) {
ImageView iv = new ImageView(ctx);
iv.setImageResource(resourseArray[position]);
iv.setScaleType(ImageView.ScaleType.FIT_XY);
iv.setLayoutParams(new Gallery.LayoutParams(150, 120));
iv.setBackgroundResource(imageBackground);
return iv;
}
}
}
Button.onClickEvent -> ga.setSelection(int postion);
I have a layout with an ImageSwitcher, a thumnail gallery, and a TextSwitcher. I would like the TextSwitcher to update text based on which thumbnail is selected. I have tried using a baseadapter to update the TextSwitcher in a similar manner to how the ImageSwitcher updates the images in the ImageView, but I am running into a wall here. Any help would be much appreciated! Thank you!
JAVA:
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.TextSwitcher;
import android.widget.TextView;
import android.widget.ViewSwitcher;
import android.widget.Gallery.LayoutParams;
public class ImageSwitch1 extends Activity implements
AdapterView.OnItemSelectedListener, ViewSwitcher.ViewFactory {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.imageswitcher);
TextSwitcher mTextSwitcher = (TextSwitcher) findViewById(R.id.textSwitcher1);
mSwitcher = (ImageSwitcher) findViewById(R.id.switcher);
mSwitcher.setFactory(this);
mSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_in));
mSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_out));
mTextSwitcher.setFactory(this);
mTextSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_in));
mTextSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_out));
Gallery g = (Gallery) findViewById(R.id.gallery);
g.setAdapter(new ImageAdapter(this));
g.setOnItemSelectedListener(this);
}
public void onNothingSelected(AdapterView parent) {
}
public View makeView() {
ImageView i = new ImageView(this);
i.setBackgroundColor(0xFF000000);
i.setScaleType(ImageView.ScaleType.FIT_CENTER);
i.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
return i;
}
private TextSwitcher mTextSwitcher;
public class TextAdapter extends BaseAdapter{
public TextAdapter(Context c){
mContext = c;
}
public int getCount(){
return mThumbIds.length;
}
public Object getItem(int position){
return position;
}
public long getItemId(int position){
return position;
}
public View getView(int position, View convertView, ViewGroup parent){
TextView t = new TextView(mContext);
t.setText(mText[position]);
t.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL);
t.setTextSize(36);
return t;
}
private Context mContext;
}
private ImageSwitcher mSwitcher;
public class ImageAdapter extends BaseAdapter {
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds.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(mThumbIds[position]);
i.setAdjustViewBounds(true);
i.setLayoutParams(new Gallery.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
i.setBackgroundResource(R.drawable.picture_frame);
return i;
}
private Context mContext;
}
public void onItemSelected(AdapterView parent, View v, int position, long id) {
mSwitcher.setImageResource(mImageIds[position]);
mTextSwitcher.setText(mThumbIds[position]);
}
private Integer[] mThumbIds = {
R.drawable.image1_thumb, R.drawable.image2_thumb, R.drawable.image3_thumb,
R.drawable.image4_thumb, R.drawable.image5_thumb};
private Integer[] mImageIds = {
R.drawable.image1, R.drawable.image2, R.drawable.image3,
R.drawable.image4, R.drawable.image5};
private Integer[] mText = {
R.string.item1, R.string.item2, R.string.item3, R.string.item4, R.string.item5
};
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout android:id="#+id/FrameLayout1" android:layout_height="match_parent" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent">
<ImageSwitcher android:id="#+id/switcher"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
</ImageSwitcher>
<Gallery android:id="#+id/gallery"
android:background="#55000000"
android:layout_width="match_parent"
android:gravity="center_vertical"
android:spacing="16dp" android:unselectedAlpha="0.5" android:layout_height="80dp" />
<TextSwitcher android:layout_width="match_parent" android:id="#+id/textSwitcher1" android:layout_height="wrap_content"></TextSwitcher>
</FrameLayout>
You should be passing a string to mTextSwitcher.setText(mThumbIds[position]); instead change it to mTextSwitcher.setText(getText(mText[position]));
In the below code i have created a gallery object and also have included a image view,on click of image how to get the image name and load it in image view.I have also included main.xml
package HelloGallery.com;
import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
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.Gallery;
import android.widget.ImageView;
import android.widget.Toast;
public class HelloGalleryActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final ImageView iv=(ImageView) findViewById(R.id.imageView1);
iv.setVisibility(View.INVISIBLE);
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) {
Toast.makeText(HelloGalleryActivity.this, "" + position, Toast.LENGTH_SHORT).show();
iv.setVisibility(View.VISIBLE);
iv.setsource("android:src="#drawable/"+imagename); //how to do this
}
});
}
public class ImageAdapter extends BaseAdapter {
int mGalleryItemBackground;
private Context mContext;
private Integer[] mImageIds = {
R.drawable.sample_1,
R.drawable.sample_2,
R.drawable.sample_3
};
public ImageAdapter(Context c) {
mContext = c;
TypedArray a = obtainStyledAttributes(R.styleable.HelloGallery);
mGalleryItemBackground = a.getResourceId(
R.styleable.HelloGallery_android_galleryItemBackground, 0);
a.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 i = new ImageView(mContext);
i.setImageResource(mImageIds[position]);
i.setLayoutParams(new Gallery.LayoutParams(150, 100));
i.setScaleType(ImageView.ScaleType.FIT_XY);
i.setBackgroundResource(mGalleryItemBackground);
return i;
}
/* public View getView(int arg0, View arg1, ViewGroup arg2) {
// TODO Auto-generated method stub
return null;
}*/
}
}
Main.mxml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello"
/>
<Gallery xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gallery"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<ImageView android:layout_width="wrap_content" android:id="#+id/imageView1" android:layout_height="wrap_content" ></ImageView>
</LinearLayout>
Try this,
Drawable d=getResources().getDrawable(R.drawable.icon);
iv.setImageDrawable(d);