view
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 theGallery
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.