Home » Android » core » view » Gallery View without pictures

About Ilias Tsagklis

Ilias Tsagklis
Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor at Java Code Geeks.

Gallery View without pictures

This is an example of Galery view on Android without any pictures. Gallery View is one of Android’s widgets. It provides a list of scrollables elements, much like the normal ListView or even a HorizontalScrollView.

In order to use Gallery View with custom elements follow these steps:

  • Create a Layout xml file with describing the layout of the Gallery elements
  • Add a Gallery compoenent of the layout of your main activity (main.xml)
  • Create a new class that extends the BaseAdapter
  • Inside the new class, override the getView to load up the custom elements for the Gallery view
Let’s see how the code looks like:
public class GalleryExampleActivity extends Activity {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		// Find our gallery view and set the adapter for it
		Gallery gallery = (Gallery) findViewById(R.id.gallery);
		gallery.setAdapter(new newGalleryAdapter(this));
	}

	// The new custom Gallery Adapter for our new layout

	private class newGalleryAdapter extends BaseAdapter {
		private Context context; // needed to create the view

		public newGalleryAdapter(Context c) {
			Context = c;
		}

		// set to three for this example
		// for normal use this should be set to the length of the data
		// structure that contains the items to be displayed
		public int getCount() {
			return 3;

		}

		public Object getItem(int position) {
			return position;
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			View v;

			// just a simple optimiztaion -
			// we only inflate a new layout if we don't have one to reuse

			if (convertView == null)
				v = LayoutInflater.from(context).inflate(R.layout.gallery_item,
					parent, false);
			else
				v = convertView;

			// Now that we have our new custom View, find out widgets in the
			// view and set them
			TextView tv = (TextView) v.findViewById(R.id.textView);

			tv.setText(String.valueOf(position));
			CheckBox cb = (CheckBox) v.findViewById(R.id.checkBox);

			// this just alternates what the checkbox states are
			if (position % 2 == 0)
				cb.setChecked(true);
			else
				cb.setChecked(false);
			return v;

		}

	}

}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/gallery_item">
	<TextView android:id="@+id/textView" android:layout_width="wrap_content" android:text="TextView" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="14dp"></TextView>
	<ImageView android:layout_width="wrap_content" android:id="@+id/imageView" android:src="@drawable/icon" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_alignParentRight="true" android:layout_marginRight="22dp"></ImageView>
	<CheckBox android:id="@+id/checkBox" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_marginLeft="24dp"></CheckBox>  
</RelativeLayout>

 
This was an example of how to use Gallery View in Android without pictures.

Related Article:

Reference: Android Tutorial: Gallery View without Pictures from our JCG partner Isaac Taylor at the Programming Mobile blog.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

Leave a Reply

avatar

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  Subscribe  
Notify of