I'm using photo gallery in my project and I can inflate image into gallery with my adapter. If I want to add text (2-3 words as name of image) on bottom of image what should I do? Is there ny reference abut it?
Thanks so much.
I'm using photo gallery in my project and I can inflate image into gallery with my adapter. If I want to add text (2-3 words as name of image) on bottom of image what should I do? Is there ny reference abut it?
Thanks so much.
Create and return a custom view object from the getView
method of your adapter. You could inflate an XML containing an ImageView
and a TextView
inside a LinearLayout
having vertical orientation.
FIRST : create gallery_items layout which has textview and imageview ,
then you have to inflate the layout in getView method with use of ViewHolder class ,
as below :
public class GalleryPhotoWithText extends Activity {
private Gallery galleryView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
galleryView = (Gallery)findViewById(R.id.galleryid);
galleryView.setAdapter(new ImageAdapter(this));
}
public class ImageAdapter extends BaseAdapter {
private Activity activity;
private LayoutInflater inflater=null;
public ImageAdapter(Activity a) {
activity = a;
inflater = (LayoutInflater)activity.getSystemService
(Context.LAYOUT_INFLATER_SERVICE); }
public int getCount() {
return data.length; }
public Object getItem(int position) {
return position; }
public long getItemId(int position) {
return position; }
public class ViewHolder{
public TextView text;
public ImageView image; }
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
ViewHolder holder;
if(convertView==null){
vi = inflater.inflate(R.layout.gallery_items, null);
holder=new ViewHolder();
holder.text=(TextView)vi.findViewById(R.id.textView1);
holder.image=(ImageView)vi.findViewById(R.id.image);
vi.setTag(holder); }
else
holder=(ViewHolder)vi.getTag();
holder.text.setText(name[position]);
final int stub_id=data[position];
holder.image.setImageResource(stub_id);
return vi; }
private int[] data = {
R.drawable.image1, R.drawable.image2,
R.drawable.image3, R.drawable.image4,
};
private String[] name = {
"first pic name", "second pic name",
"third pic name", "fourth pic name",
}; } }
Hope this help