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 the "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 the "Next" button.
Android App
Step 5
Add Activity name and click "Finish".
Android App
Step 6
Add images in the Drawable file. The below template shows how to add the image file. Go to the app and right-click. The “Show in Explorer” option will appear. Click on the path and add an 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. <ImageView
  7. android:id="@+id/selected"
  8. android:layout_width="fill_parent"
  9. android:layout_height="fill_parent"
  10. android:layout_above="@+id/gallery_relative_layout"
  11. android:layout_marginLeft="30dip"
  12. android:layout_marginRight="30dip"
  13. android:layout_marginTop="30dip" />
  14. <View
  15. android:layout_width="match_parent"
  16. android:layout_height="1dp"
  17. android:background="#770000ff"
  18. android:layout_marginTop="300dp"
  19. android:layout_above="@+id/gallery_relative_layout" />
  20. <RelativeLayout
  21. android:id="@+id/gallery_relative_layout"
  22. android:layout_width="fill_parent"
  23. android:layout_height="200dip"
  24. android:layout_alignParentBottom="true"
  25. android:orientation="horizontal"
  26. android:paddingTop="20dp">
  27. <verticalScrollView
  28. android:id="@+id/hor_scroll_view"
  29. android:layout_width="match_parent"
  30. android:layout_height="wrap_content"
  31. >
  32. <LinearLayout
  33. android:id="@+id/gallery"
  34. android:layout_width="wrap_content"
  35. android:layout_height="wrap_content"
  36. android:orientation="horizontal" >
  37. <ImageView
  38. android:id="@+id/image1"
  39. android:layout_width="wrap_content"
  40. android:layout_height="wrap_content"
  41. android:src="@drawable/a"
  42. android:onClick="biggerView"/>
  43. <ImageView
  44. android:id="@+id/image8"
  45. android:layout_width="wrap_content"
  46. android:layout_height="wrap_content"
  47. android:src="@drawable/aa"
  48. android:onClick="biggerView"/>
  49. <ImageView
  50. android:id="@+id/image2"
  51. android:layout_width="wrap_content"
  52. android:layout_height="wrap_content"
  53. android:src="@drawable/b"
  54. android:onClick="biggerView"/>
  55. <ImageView
  56. android:id="@+id/image3"
  57. android:layout_width="wrap_content"
  58. android:layout_height="wrap_content"
  59. android:src="@drawable/c"
  60. android:onClick="biggerView"/>
  61. <ImageView
  62. android:id="@+id/image4"
  63. android:layout_width="wrap_content"
  64. android:layout_height="wrap_content"
  65. android:src="@drawable/d"
  66. android:onClick="biggerView"/>
  67. <ImageView
  68. android:id="@+id/image5"
  69. android:layout_width="wrap_content"
  70. android:layout_height="wrap_content"
  71. android:src="@drawable/e"
  72. android:onClick="biggerView"/>
  73. <ImageView
  74. android:id="@+id/image6"
  75. android:layout_width="wrap_content"
  76. android:layout_height="wrap_content"
  77. android:src="@drawable/f"
  78. android:onClick="biggerView"/>
  79. <ImageView
  80. android:id="@+id/image7"
  81. android:layout_width="wrap_content"
  82. android:layout_height="wrap_content"
  83. android:src="@drawable/g"
  84. android:onClick="biggerView"/>
  85. </LinearLayout>
  86. </verticalScrollView>
  87. </RelativeLayout>
  88. </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 the menu bar and click "Make a project" or press ctrl+f9 to debug the errors.
Android App
Step 11
Then, click the "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 a Gallery Android application using Android Studio.
Android App
Android App