Gallery Android App Using Android Studio

Introduction

In this article, I will show you how to create a Gallery Android App using Android Studio. A gallery, whether in a building or on the internet, is a place where photographs are on display.

Requirements

Steps to be followed -

Follow these steps to create a Gallery Android app. I have included the source code below.

Step 1

Open Android Studio and start a New Android Studio Project.

Android App

Step 2

You can choose your application name and choose where your project is stored on the location. If you wish to use C++ for coding the project, mark the "Include C++ support", and click "Next" button.

Android App

Step 3

Now, select the version of Android and select the target Android devices. We need to choose the SDK level which plays an important role to run the application.

Android App

Step 4

Now, add the activity and click "Next" button.

Android App

 Step 5

Add Activity name and click "Finish".

Android App

Step 6

Add images in Drawable file. The below template shows how to add the image file. Go to app and right click. The “Show in Explorer” option will appear. Click on the path and add image in the main folder.

Android App

Android App

Step 7

The below template is shown after adding the image file.

Android App

Step 8

Go to activity_main.xml. This XML file contains the designing code for an Android app.

Android App

The XML code is given below.

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:layout_width="match_parent"  
  3.     android:layout_height="match_parent"  
  4.     android:background="@android:color/white"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <ImageView  
  8.         android:id="@+id/selected"  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="fill_parent"  
  11.         android:layout_above="@+id/gallery_relative_layout"  
  12.         android:layout_marginLeft="30dip"  
  13.         android:layout_marginRight="30dip"  
  14.         android:layout_marginTop="30dip" />  
  15.   
  16.     <View  
  17.         android:layout_width="match_parent"  
  18.         android:layout_height="1dp"  
  19.         android:background="#770000ff"  
  20.         android:layout_marginTop="300dp"  
  21.         android:layout_above="@+id/gallery_relative_layout" />  
  22.     <RelativeLayout  
  23.         android:id="@+id/gallery_relative_layout"  
  24.         android:layout_width="fill_parent"  
  25.         android:layout_height="200dip"  
  26.         android:layout_alignParentBottom="true"  
  27.         android:orientation="horizontal"  
  28.         android:paddingTop="20dp">  
  29.         <verticalScrollView  
  30.             android:id="@+id/hor_scroll_view"  
  31.             android:layout_width="match_parent"  
  32.             android:layout_height="wrap_content"  
  33.            >   
  34.             <LinearLayout  
  35.                 android:id="@+id/gallery"  
  36.                 android:layout_width="wrap_content"  
  37.                 android:layout_height="wrap_content"  
  38.                 android:orientation="horizontal" >  
  39.                 <ImageView  
  40.                     android:id="@+id/image1"  
  41.                     android:layout_width="wrap_content"  
  42.                     android:layout_height="wrap_content"  
  43.                     android:src="@drawable/a"  
  44.                     android:onClick="biggerView"/>  
  45.                 <ImageView  
  46.                     android:id="@+id/image8"  
  47.                     android:layout_width="wrap_content"  
  48.                     android:layout_height="wrap_content"  
  49.                     android:src="@drawable/aa"  
  50.                     android:onClick="biggerView"/>  
  51.                 <ImageView  
  52.                     android:id="@+id/image2"  
  53.                     android:layout_width="wrap_content"  
  54.                     android:layout_height="wrap_content"  
  55.                     android:src="@drawable/b"  
  56.                     android:onClick="biggerView"/>  
  57.                 <ImageView  
  58.                     android:id="@+id/image3"  
  59.                     android:layout_width="wrap_content"  
  60.                     android:layout_height="wrap_content"  
  61.                     android:src="@drawable/c"  
  62.                     android:onClick="biggerView"/>  
  63.                 <ImageView  
  64.                     android:id="@+id/image4"  
  65.                     android:layout_width="wrap_content"  
  66.                     android:layout_height="wrap_content"  
  67.                     android:src="@drawable/d"  
  68.                     android:onClick="biggerView"/>  
  69.                 <ImageView  
  70.                     android:id="@+id/image5"  
  71.                     android:layout_width="wrap_content"  
  72.                     android:layout_height="wrap_content"  
  73.                     android:src="@drawable/e"  
  74.                     android:onClick="biggerView"/>  
  75.                 <ImageView  
  76.                     android:id="@+id/image6"  
  77.                     android:layout_width="wrap_content"  
  78.                     android:layout_height="wrap_content"  
  79.                     android:src="@drawable/f"  
  80.                     android:onClick="biggerView"/>  
  81.                 <ImageView  
  82.                     android:id="@+id/image7"  
  83.                     android:layout_width="wrap_content"  
  84.                     android:layout_height="wrap_content"  
  85.                     android:src="@drawable/g"  
  86.                     android:onClick="biggerView"/>  
  87.             </LinearLayout>  
  88.         </verticalScrollView>  
  89.     </RelativeLayout>  
  90. </RelativeLayout>   

Step 9

Go to Main Activity.java. This Java program is the backend language for Android.

Android App

The java code is given below

  1. package abu.image;  
  2. import android.os.Bundle;  
  3. import android.app.Activity;  
  4. import android.util.Log;  
  5. import android.view.Menu;  
  6. import android.view.View;  
  7. import android.widget.ImageView;  
  8. public class MainActivity extends Activity {  
  9.     ImageView im;  
  10.     @Override  
  11.     protected void onCreate(Bundle savedInstanceState) {  
  12.         super.onCreate(savedInstanceState);  
  13.         setContentView(R.layout.activity_main);  
  14.     }  
  15.     public void biggerView(View v) {  
  16.         im = (ImageView) findViewById(R.id.selected);  
  17.         switch (v.getId()) {  
  18.             case R.id.image1:  
  19.                 im.setImageResource(R.drawable.a);  
  20.                 break;  
  21.             case R.id.image2:  
  22.                 im.setImageResource(R.drawable.b);  
  23.                 break;  
  24.             case R.id.image3:  
  25.                 im.setImageResource(R.drawable.c);  
  26.                 break;  
  27.             case R.id.image4:  
  28.                 im.setImageResource(R.drawable.d);  
  29.                 break;  
  30.             case R.id.image5:  
  31.                 im.setImageResource(R.drawable.e);  
  32.                 break;  
  33.             case R.id.image6:  
  34.                 im.setImageResource(R.drawable.f);  
  35.                 break;  
  36.             case R.id.image7:  
  37.                 im.setImageResource(R.drawable.g);  
  38.                 break;  
  39.             case R.id.image8:  
  40.                 im.setImageResource(R.drawable.aa);  
  41.                 break;  
  42.         }  
  43.     }  
  44.     @Override  
  45.     public boolean onCreateOptionsMenu(Menu menu) {  
  46.         // Inflate the menu; this adds items to the action bar if it is present.  
  47.         getMenuInflater().inflate(R.menu.main, menu);  
  48.         return true;  
  49.     }  
  50. }  

Step 10

Now, go to menu bar and click "Make project" or press ctrl+f9 to debug the errors.

Android App

Step 11

Then, click "Run" button or press shift+f10 to run the project. And choose the virtual machine and click OK.

Android App

Conclusion

We have successfully created Gallery Android application using Android Studio.

Android App

Android App

Android App

Up Next
    Ebook Download
    View all
    Learn
    View all