Hello Guys,
Many time you have a requirement to display multiple images same like our device / mobile phone gallery.
You can display images with 3 ways.
1) Using Gallery Widget
2) Using Horizontal Scroll View
3) Using View Pager
Here I provide sample code of Gallery Widget.
Sample Code:
File: activity_gallery.xml
<Gallery xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gallery1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/bg"/>
File: GalleryActivity.java
package com.sneha.displayimagedemo;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
public class GalleryActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gallery);
// Reference the Gallery view
Gallery g = (Gallery) findViewById(R.id.gallery1);
g.setSpacing(2);
// Set the adapter to our custom adapter (below)
g.setAdapter(new ImageAdapter(this));
}
public class ImageAdapter extends BaseAdapter
{
public ImageAdapter(Context c)
{
mContext = c;
}
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.setScaleType(ImageView.ScaleType.FIT_CENTER);
return i;
}
private Context mContext;
private Integer[] mImageIds =
{
R.drawable.android_1,
R.drawable.android_2,
R.drawable.android_3,
R.drawable.android_4,
R.drawable.android_5,
R.drawable.android_6,
R.drawable.android_7,
R.drawable.android_8,
R.drawable.android_9,
R.drawable.android_10
};
}
}
Output:
Download Full Source Code: GalleryWidgetExample
Many time you have a requirement to display multiple images same like our device / mobile phone gallery.
You can display images with 3 ways.
1) Using Gallery Widget
2) Using Horizontal Scroll View
3) Using View Pager
Here I provide sample code of Gallery Widget.
Sample Code:
File: activity_gallery.xml
<Gallery xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gallery1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/bg"/>
File: GalleryActivity.java
package com.sneha.displayimagedemo;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
public class GalleryActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gallery);
// Reference the Gallery view
Gallery g = (Gallery) findViewById(R.id.gallery1);
g.setSpacing(2);
// Set the adapter to our custom adapter (below)
g.setAdapter(new ImageAdapter(this));
}
public class ImageAdapter extends BaseAdapter
{
public ImageAdapter(Context c)
{
mContext = c;
}
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.setScaleType(ImageView.ScaleType.FIT_CENTER);
return i;
}
private Context mContext;
private Integer[] mImageIds =
{
R.drawable.android_1,
R.drawable.android_2,
R.drawable.android_3,
R.drawable.android_4,
R.drawable.android_5,
R.drawable.android_6,
R.drawable.android_7,
R.drawable.android_8,
R.drawable.android_9,
R.drawable.android_10
};
}
}
Output:
Download Full Source Code: GalleryWidgetExample
No comments:
Post a Comment