{"id":1937,"date":"2016-05-25T06:58:25","date_gmt":"2016-05-25T06:58:25","guid":{"rendered":"http:\/\/abhiandroid.com\/ui\/?page_id=1937"},"modified":"2019-06-12T07:48:40","modified_gmt":"2019-06-12T07:48:40","slug":"gallery","status":"publish","type":"page","link":"https:\/\/abhiandroid.com\/ui\/gallery","title":{"rendered":"Gallery Tutorial With Example In Android Studio"},"content":{"rendered":"<p>In Android, Gallery is a view used to show items in a center locked, horizontal scrolling list and user will select a view and\u00a0then user selected view will be shown in the center of the Horizontal list. The items in\u00a0Gallery are added using\u00a0Adapter just like in ListView or GridView.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1939\" src=\"\/ui\/wp-content\/uploads\/2016\/05\/Gallery-in-Android.jpg\" alt=\"Gallery in Android\" width=\"273\" height=\"367\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/05\/Gallery-in-Android.jpg 273w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/05\/Gallery-in-Android-223x300.jpg 223w\" sizes=\"auto, (max-width: 273px) 100vw, 273px\" \/><\/p>\n<p><span style=\"color: #ff0000;\"><strong>Important Note:<\/strong><\/span>\u00a0Gallery class was deprecated in API level 16. This widget is no longer supported. Other horizontally scrolling widgets include\u00a0HorizontalScrollView\u00a0and\u00a0ViewPager\u00a0from the support library.<\/p>\n<hr \/>\n<h4><strong>Basic\u00a0<\/strong><strong>Gallery<\/strong> XML Code<strong>:<\/strong><\/h4>\n<pre>&lt;Gallery\r\nandroid:id=\"@+id\/simpleGallery\"\r\nandroid:layout_width=\"fill_parent\"\r\nandroid:layout_height=\"wrap_content\"\/ &gt;\r\n<\/pre>\n<hr \/>\n<h4><strong>Adapter Used To Fill Images In Gallery:<\/strong><\/h4>\n<p>Adapters works as a bridge between AdapterView and data source. For filling data(images) in Gallery we need to use Adapters. BaseAdapter is parent adapter for all other adapters so we mainly used it for displaying data in Gallery list.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1613\" src=\"\/ui\/wp-content\/uploads\/2016\/04\/Adapter-in-Android.jpg\" alt=\"Adapter in Android\" width=\"615\" height=\"478\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/04\/Adapter-in-Android.jpg 615w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/04\/Adapter-in-Android-300x233.jpg 300w\" sizes=\"auto, (max-width: 615px) 100vw, 615px\" \/><\/p>\n<p><strong>BaseAdapter:<\/strong><\/p>\n<p>BaseAdapter is a common base class of a general implementation of an Adapter that can be used in ListView,\u00a0GridView, Gallery etc. Base Adapter can be extended to create a custom Adapter for displaying a custom list items.<\/p>\n<p><strong>Here is the code of Custom Adapter when we extends the BaseAdapter in that:<\/strong><\/p>\n<pre>public class CustomAdapter extends BaseAdapter {\r\n\r\n@Override\r\n\r\npublic int getCount() {\r\n\r\nreturn 0;\r\n\r\n}\r\n\r\n@Override\r\n\r\npublic Object getItem(int i) {\r\n\r\nreturn null;\r\n\r\n}\r\n\r\n@Override\r\n\r\npublic long getItemId(int i) {\r\n\r\nreturn 0;\r\n\r\n}\r\n\r\n@Override\r\n\r\npublic View getView(int i, View view, ViewGroup viewGroup) {\r\n\r\nreturn null;\r\n\r\n}\r\n<\/pre>\n<p>In the above code snippet we see the\u00a0overrided\u00a0methods of BaseAdapter which are used to set the data in a list, grid or a Gallery. From there we mainly used two functions getCount() and getView().<\/p>\n<ul>\n<li><strong>getView():<\/strong> This method called automatically for all items of Gallery (similar to list view in which getView() method called for each item of ListView)<\/li>\n<li><strong>getCount():<\/strong> This method returns the total number of items to be displayed in a list. It counts the value from array list size() method or an array\u2019s length.<\/li>\n<\/ul>\n<p>Read <a href=\"\/ui\/baseadapter-tutorial-example.html\">BaseAdapter tutorial<\/a> for more details.<\/p>\n<hr \/>\n<h4><strong>Steps For Implementation Of Android Gallery View:<\/strong><\/h4>\n<ol>\n<li>Get the reference of Gallery in class using findViewById() method, or you can also create an object dynamically.<\/li>\n<li>Create an array of Images and call the adapter using Gallery view\u2019s object.<\/li>\n<li>Create a Custom adapter class which extends BaseAdapter to bind the Gallery view with a series of ImageViews.<\/li>\n<\/ol>\n<hr \/>\n<h4><strong>Important Methods Of Gallery In Android:<\/strong><\/h4>\n<p>Let\u2019s we discuss some important methods of Gallery that may be called in order to manage the Gallery.<\/p>\n<p><span style=\"color: #008000;\"><strong>1. setAnimationDuration(int):<\/strong><\/span> This method is used to set the duration for how long a transition animation should run (in milliseconds) when layout has changed.<\/p>\n<p>Below we set the duration for how long a transition animation should run when layout has changed.<\/p>\n<pre>Gallery simpleGallery = (Gallery) findViewById(R.id.simpleGallery); \/\/ get the reference of Gallery\r\nsimpleGallery.setAnimationDuration(3000); \/\/ set 3000 milliseconds for animation duration between items of Gallery\r\n<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1941\" src=\"\/ui\/wp-content\/uploads\/2016\/05\/setAnimationDuration-in-Gallery-Android.gif\" alt=\"setAnimationDuration in Gallery Android\" width=\"306\" height=\"432\" \/><\/p>\n<p><span style=\"color: #008000;\"><strong>2. setSpacing(int):<\/strong><\/span> This method is used to set the spacing between items in a Gallery.<\/p>\n<p>Below we set the spacing between the items of Gallery.<\/p>\n<pre>Gallery simpleGallery = (Gallery) findViewById(R.id.simpleGallery); \/\/ get the reference of Gallery\r\nsimpleGallery.setSpacing(5); \/\/ set space between the items of Gallery\r\n<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1942\" src=\"\/ui\/wp-content\/uploads\/2016\/05\/spacing-in-Gallery-Android.jpg\" alt=\"spacing in Gallery Android\" width=\"261\" height=\"359\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/05\/spacing-in-Gallery-Android.jpg 261w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/05\/spacing-in-Gallery-Android-218x300.jpg 218w\" sizes=\"auto, (max-width: 261px) 100vw, 261px\" \/><\/p>\n<p><span style=\"color: #008000;\"><strong>3.setUnselectedAlpha(float):<\/strong><\/span> This method is used to set the alpha on the items that are not selected.<\/p>\n<p>Below we set the alpha value for unselected items of Gallery.<\/p>\n<pre>Gallery simpleGallery = (Gallery) findViewById(R.id.simpleGallery); \/\/ get the reference of Gallery\r\nsimpleGallery.setUnselectedAlpha(0.80f); \/\/ set 0.25 value for the alpha of unselected items of Gallery\r\n<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1943\" src=\"\/ui\/wp-content\/uploads\/2016\/05\/setUnselectedAlpha-in-Gallery-Android.jpg\" alt=\"setUnselectedAlpha in Gallery Android\" width=\"339\" height=\"218\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/05\/setUnselectedAlpha-in-Gallery-Android.jpg 339w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/05\/setUnselectedAlpha-in-Gallery-Android-300x193.jpg 300w\" sizes=\"auto, (max-width: 339px) 100vw, 339px\" \/><\/p>\n<hr \/>\n<h4><strong>Attributes of Gallery:<\/strong><\/h4>\n<p>Now let\u2019s we discuss some common attributes of a Gallery that helps us to configure it in our layout (xml).<\/p>\n<p><span style=\"color: #008000;\"><strong>1. id:<\/strong><\/span> id attribute is used to uniquely identify a Gallery.<\/p>\n<p>Below we set the id of the Gallery that is used to uniquely identify it.<\/p>\n<pre>&lt;Gallery\r\nandroid:id=\"@+id\/simpleGallery\"\r\nandroid:layout_width=\"fill_parent\"\r\nandroid:layout_height=\"wrap_content\"\/ &gt; &lt; !--\u00a0 id of Gallery used to uniquely identify it --&gt;\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>2. padding:<\/strong><\/span> This attribute is used to set the padding from left, right, top or bottom side of a Gallery.<\/p>\n<ul>\n<li>paddingRight: This attribute is used to set the padding from the right side of a Gallery.<\/li>\n<li>paddingLeft: This attribute is used to set the padding from the left side of a Gallery.<\/li>\n<li>paddingTop: This attribute is used to set the padding from the top side of a Gallery.<\/li>\n<li>paddingBottom: This attribute is used to set the padding from the bottom side of a Gallery.<\/li>\n<li>Padding: This attribute is used to set the padding from the all the side\u2019s of a Gallery.<\/li>\n<\/ul>\n<p>Below we set the 10dp padding from all the sides of a Gallery<\/p>\n<pre>&lt;Gallery\r\nandroid:id=\"@+id\/simpleGallery\"\r\nandroid:layout_width=\"fill_parent\"\r\nandroid:layout_height=\"wrap_content\"\r\nandroid:padding=\"10dp\"\/&gt; &lt;!--\u00a0 10dp padding from all the sides of Gallery --&gt;\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>3. background:<\/strong><\/span> This attribute is used to set the background of a Gallery. We can set a color or a drawable in the background of a Gallery:<\/p>\n<p>Below is the example code with explanation included in which we set the black color in the background of Gallery.<\/p>\n<pre>&lt;Gallery\r\nandroid:id=\"@+id\/simpleGallery\"\r\nandroid:layout_width=\"fill_parent\"\r\nandroid:layout_height=\"wrap_content\"\r\nandroid:background=\"#000\" \/&gt; &lt;!--\u00a0\u00a0 black background color of Gallery --&gt;\r\n<\/pre>\n<p><strong>Setting padding In Gallery In Java class:<\/strong><\/p>\n<pre>Gallery simpleGallery=(Gallery)findViewById(R.id.simpleGallery); \/\/ get the reference of Gallery\r\nsimpleGallery.setBackgroundColor(Color.BLACK); \/\/ set black color in the background of Gallery\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>4. animationDuration:<\/strong><\/span> This attribute is used to set the duration for how long a transition animation should run (in milliseconds) when layout has changed.\u00a0We can also set the animation duration programmatically means in java class by using setAnimationDuration(int).<\/p>\n<p>Below we set the duration for how long a transition animation should run when layout has changed.<\/p>\n<pre>&lt;Gallery\r\nandroid:id=\"@+id\/simpleGallery\"\r\nandroid:layout_width=\"fill_parent\"\r\nandroid:layout_height=\"wrap_content\"\r\nandroid:animationDuration=\"3000\" \/&gt;\r\n&lt;!-- set 100 milliseconds for animation duration between items of Gallery --&gt;\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>5. spacing:<\/strong><\/span> This attribute is used to set the spacing between items in a Gallery. We can also set the spacing between items programmatically means in java class by using setSpacing() method.<\/p>\n<p>Below we set the spacing between the items of Gallery.<\/p>\n<pre>&lt;Gallery\r\nandroid:id=\"@+id\/simpleGallery\"\r\nandroid:layout_width=\"fill_parent\"\r\nandroid:layout_height=\"wrap_content\"\r\nandroid:spacing=\"5dp\" \/&gt;\r\n&lt;!-- set 5dp space between the items of Gallery --&gt;\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>6. unselectedAlpha:<\/strong><\/span> This attribute is used to set the alpha on the items that are not selected.\u00a0Must be a floating point value, such as &#8220;5&#8221;. We can also set the alpha of items programmatically means in java class by using setUnselectedAlpha(float) method.<\/p>\n<p>Below we set the alpha value for unselected items of Gallery.<\/p>\n<pre>&lt;Gallery\r\nandroid:id=\"@+id\/simpleGallery\"\r\nandroid:layout_width=\"fill_parent\"\r\nandroid:layout_height=\"wrap_content\"\r\nandroid:unselectedAlpha=\"0.25\" \/&gt;\r\n&lt;!-- set 0.25 value for the alpha of unselected items of Gallery --&gt;\r\n<\/pre>\n<hr \/>\n<h4><strong>Gallery Example In Android Studio:<\/strong><\/h4>\n<p>Below is the example of Gallery in which we display a ImageView and Gallery with number of items(images). Firstly we create an array of images id\u2019s and then set the adapter to fill the data in the Gallery. In this we create custom adapter class in which we extends BaseAdapter class. We also perform setOnItemClickListener event so whenever a user click on any item of Gallery the selectable item(image) is displayed in the ImageView.<\/p>\n<p>Below you can download code, see final output and step by step explanation of example:<\/p>\n<p style=\"text-align: center;\"><a class=\"download\" href=\"https:\/\/github.com\/abhisheksaini4\/GalleryExample\" target=\"_blank\" rel=\"nofollow\">Download Code<\/a><a class=\"help\" title=\"Learn How To Download Code And Import In Android Studio\" href=\"\/androidstudio\/download-code-abhiandroid\" target=\"_blank\" rel=\"nofollow\"> ?<\/a><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1944\" src=\"\/ui\/wp-content\/uploads\/2016\/05\/Gallery-Example-in-Android-Studio.gif\" alt=\"Gallery Example in Android Studio\" width=\"303\" height=\"440\" \/><\/p>\n<p><span style=\"color: #008000;\"><strong>Step 1:\u00a0<\/strong><\/span>Create a new project and name it GalleryExample<\/p>\n<p><span style=\"color: #008000;\"><strong>Step 2:<\/strong><\/span>\u00a0Open res -&gt; layout -&gt;activity_main.xml (or) main.xml and add following code:<\/p>\n<pre>&lt;LinearLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\r\n    xmlns:tools=\"http:\/\/schemas.android.com\/tools\"\r\n    android:layout_width=\"match_parent\"\r\n    android:layout_height=\"match_parent\"\r\n    android:orientation=\"vertical\"\r\n    android:paddingBottom=\"@dimen\/activity_vertical_margin\"\r\n    android:paddingLeft=\"@dimen\/activity_horizontal_margin\"\r\n    android:paddingRight=\"@dimen\/activity_horizontal_margin\"\r\n    android:paddingTop=\"@dimen\/activity_vertical_margin\"\r\n    tools:context=\".MainActivity\"&gt;\r\n\r\n    &lt;!-- create a ImageView and Gallery --&gt;\r\n    &lt;ImageView\r\n        android:id=\"@+id\/selectedImageView\"\r\n        android:layout_width=\"fill_parent\"\r\n        android:layout_height=\"200dp\"\r\n        android:scaleType=\"fitXY\" \/&gt;\r\n\r\n    &lt;Gallery\r\n        android:id=\"@+id\/simpleGallery\"\r\n        android:layout_width=\"fill_parent\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:layout_marginTop=\"100dp\"\r\n        android:unselectedAlpha=\"0.80\" \/&gt;\r\n\r\n&lt;\/LinearLayout&gt;<\/pre>\n<p><span style=\"color: #008000;\"><strong>Step 3:<\/strong><\/span>\u00a0Open src -&gt; package -&gt; MainActivity.java<\/p>\n<p>In this step we open\u00a0MainActivity\u00a0and\u00a0add the code for initiate the ImageView and Gallery. Firstly we create an array of images id\u2019s and then set the adapter to fill the data in the Gallery. We also perform setOnItemClickListener event so whenever a user click on any item of Gallery the selectable item(image) is displayed in the ImageView.<\/p>\n<pre>package example.abhiandroid.galleryexample;\r\n\r\nimport android.graphics.Color;\r\nimport android.support.v7.app.AppCompatActivity;\r\nimport android.os.Bundle;\r\nimport android.view.Gravity;\r\nimport android.view.View;\r\nimport android.widget.AdapterView;\r\nimport android.widget.Gallery;\r\nimport android.widget.ImageView;\r\n\r\npublic class MainActivity extends AppCompatActivity {\r\n\r\n    Gallery simpleGallery;\r\n    CustomGalleryAdapter customGalleryAdapter;\r\n    ImageView selectedImageView;\r\n    \/\/ array of images\r\n    int[] images = {R.drawable.image1, R.drawable.image2, R.drawable.image3, R.drawable.image4, R.drawable.image5,\r\n            R.drawable.image6, R.drawable.image7, R.drawable.image8, R.drawable.image9, R.drawable.image10, R.drawable.image11,\r\n            R.drawable.image12, R.drawable.image13};\r\n\r\n    @Override\r\n    protected void onCreate(Bundle savedInstanceState) {\r\n        super.onCreate(savedInstanceState);\r\n        setContentView(R.layout.activity_main);\r\n        simpleGallery = (Gallery) findViewById(R.id.simpleGallery); \/\/ get the reference of Gallery\r\n        selectedImageView = (ImageView) findViewById(R.id.selectedImageView); \/\/ get the reference of ImageView\r\n        customGalleryAdapter = new CustomGalleryAdapter(getApplicationContext(), images); \/\/ initialize the adapter\r\n        simpleGallery.setAdapter(customGalleryAdapter); \/\/ set the adapter\r\n        simpleGallery.setSpacing(10);\r\n        \/\/ perform setOnItemClickListener event on the Gallery\r\n        simpleGallery.setOnItemClickListener(new AdapterView.OnItemClickListener() {\r\n            @Override\r\n            public void onItemClick(AdapterView&lt;?&gt; parent, View view, int position, long id) {\r\n                \/\/ set the selected image in the ImageView\r\n                selectedImageView.setImageResource(images[position]);\r\n\r\n            }\r\n        });\r\n    }\r\n}\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>Step 4:<\/strong><\/span>\u00a0Create a new class CustomGalleryAdapter.java inside package and add the following code.<\/p>\n<p>In this step we create a CustomGalleryAdapter in which we extend BaseAdapter and implement the overrided methods. In this we create a ImageView at run time in getView method and finally set the image in the ImageView.<\/p>\n<pre>package example.abhiandroid.galleryexample;\r\n\r\nimport android.content.Context;\r\nimport android.view.View;\r\nimport android.view.ViewGroup;\r\nimport android.widget.BaseAdapter;\r\nimport android.widget.Gallery;\r\nimport android.widget.ImageView;\r\n\r\npublic class CustomGalleryAdapter extends BaseAdapter {\r\n\r\n    private Context context;\r\n    private int[] images;\r\n\r\n    public CustomGalleryAdapter(Context c, int[] images) {\r\n        context = c;\r\n        this.images = images;\r\n    }\r\n\r\n    \/\/ returns the number of images\r\n    public int getCount() {\r\n        return images.length;\r\n    }\r\n\r\n    \/\/ returns the ID of an item\r\n    public Object getItem(int position) {\r\n        return position;\r\n    }\r\n\r\n    \/\/ returns the ID of an item\r\n    public long getItemId(int position) {\r\n        return position;\r\n    }\r\n\r\n    \/\/ returns an ImageView view\r\n    public View getView(int position, View convertView, ViewGroup parent) {\r\n\r\n        \/\/ create a ImageView programmatically\r\n        ImageView imageView = new ImageView(context);\r\n        imageView.setImageResource(images[position]); \/\/ set image in ImageView\r\n        imageView.setLayoutParams(new Gallery.LayoutParams(200, 200)); \/\/ set ImageView param\r\n        return imageView;\r\n    }\r\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In Android, Gallery is a view used to show items in a center locked, horizontal scrolling list and user will select a view and\u00a0then user selected view will be shown in the center of the Horizontal list. The items in\u00a0Gallery are added using\u00a0Adapter just like in ListView or GridView. Important Note:\u00a0Gallery class was deprecated in &hellip; <a href=\"https:\/\/abhiandroid.com\/ui\/gallery\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Gallery Tutorial With Example In Android Studio<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"open","ping_status":"closed","template":"home.php","meta":{"_acf_changed":false,"footnotes":""},"class_list":["post-1937","page","type-page","status-publish","hentry"],"acf":[],"psp_head":"<title>Gallery Tutorial With Example In Android Studio \u2013 Abhi Android<\/title>\r\n<meta name=\"description\" content=\"In Android, Gallery is a view used to show items in a center locked, horizontal scrolling list and user will select a view and then user selected view will be shown in the center of the Horizontal list.\" \/>\r\n<meta name=\"robots\" content=\"index,follow\" \/>\r\n<link rel=\"canonical\" href=\"https:\/\/abhiandroid.com\/ui\/gallery\" \/>\r\n","_links":{"self":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/pages\/1937","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/comments?post=1937"}],"version-history":[{"count":3,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/pages\/1937\/revisions"}],"predecessor-version":[{"id":2785,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/pages\/1937\/revisions\/2785"}],"wp:attachment":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/media?parent=1937"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}