Android – Gallery Project

Agenda we will learn android basics with this project.

Project Description – Open android image gallery, pick image from gallery and show it on android image view.

Start by creating a new android project and giving it some name.

Some Basics – 

Every app project must have an AndroidManifest.xml file (with precisely that name) at the root of the project source set.

One of the most important part of above snippet is:

here we are saying that is the launching screen or very first screen/activity of our app.

The manifest file describes essential information about your app to the Android build tools, the Android operating system, and Google Play.

The manifest file is required to declare the following:

  • The app’s package name, which usually matches your code’s namespace. The Android build tools use this to determine the location of code entities when building your project. When packaging the app, the build tools replace this value with the application ID from the Gradle build files, which is used as the unique app identifier on the system and on Google Play.
  • The components of the app, which include all activities, services, broadcast receivers, and content providers. Each component must define basic properties such as the name of its Kotlin or Java class. It can also declare capabilities such as which device configurations it can handle, and intent filters that describe how the component can be started.
  • The permissions that the app needs in order to access protected parts of the system or other apps. It also declares any permissions that other apps must have if they want to access content from this app.
  • The hardware and software features the app requires, which affects which devices can install the app from Google Play.

An Android activity is one screen of the Android app’s user interface.

import statements in an Android application

In an Android application class file which extends the Activity class, you might see the following import statements:

Difference Between Package And Import – 
The statement “package” is used to indicate that the class being implemented is part of a group of related classes placed in the same folder, in our example:
indicates that the class is in a folder named “myapplication”, and this folder is inside the folder “example”, and so on.
On the other hand, the statement “import” is used to indicate that the class being implemented uses a class or a group of classes from some “package”. This instruction helps to locate the libraries used in our class.
In our example:

AppCompatActivity – Base class for activities that use action bar features(? you would have seen in some android apps top right there are menu options), indicates that the class being implemented

can inherit features from AppCompatActivity. What features? Obviously some life cycle methods like onCreate(), onResume(), onPause() being some of the features.

Bundles is used to share data between activities , and to save state of app in oncreate(Bundle savedInstanceState) method so that app will come to know where it was stopped

Now let’s look at class implementation:

Let’s try to understand what is going on:

Here we can see inheritance in action. class MainActivity inherits from AppCompatActivity, which inherits from Activity and so on. Our class will be able to override in built method of AppCompatActivity and Activity.

Here we can see we are overriding onCreate function. Now the question is what is onCreate()?

When you launch android activity/screen this function works like initialization point.

This function is called very first time on activity load. It means if you press home button and then again resume the app, onCreate will not be called.

Also this function uses the bundle parameter from which app can resume it’s previous state in case it was force terminated.

Let’s drag drop a ImageView and a Button. I have also set a default background image on image view named “ic_launcher_foreground”.  It’s just simple drag drop and below is xml code:

At this point of time the output is:

Some important points and concepts from xml:

androidx.constraintlayout.widget.ConstraintLayout : We are following constraint based or if you are from iOS background say it autolayout or if you are from react native it’s Flex or if you are from flutter it’s Box layout.

android:id : This must be unique, so that we can refer it in our code.

wrap_content : It means the view/button/image width or height should be enough to wrap content completely.

For our button constraints are as below:

layout_constraintTop_toBottomOf=”@+id/textView”: That means button will start from the bottom of textView.

For our text view constraints are as per below:

layout_constraintBottom_toBottomOf=”parent” : That means space in relative to bottom of container.

layout_constraintLeft_toLeftOf=”parent” : That means space in relative to left of container.

Similarly we can say for right and top.

This is a very basic example of constrained based system, which we will do in detail at some point of time.

When user will clicks the button we will open content gallery, choose image and show it on Image View.

Have some tea, relax and we will start with implementing gallery part.

First of all we need to create a variable for button, action for opening a gallery and  image view.

Let’s create button object, image view object and initialize it in onCreate():

At this point if you run app you will see image view with basic default image in it and “hello world” and a button named “Choose Image”. If you click on button you should see “clicked” being print on log cat console under debug area. All Good till now.

Let’s open image gallery on click of our button:

As we are using:

We have to implement callback:

Complete file looks like this:

And here is the complete source code.

iOSDose is best android training provider. Join us to learn android app development.


React Native Day 10 State

Use state to store the data your current page needs in your controller-view.

State is mutable

In general, you should initialize state in the constructor, and then call setState when you want to change it.

Constructor means initializer.

Create a new project by typing command in terminal-

react-native init StateExample.

then open App.js file, and replace code with below.

Let’s Understand the code:

This is the syntax of constructor, this is the best place to initialize states. We can initialize state with default values :).

This is the render method to display UI:

We must make sure we use current context(this (similar to java or similar to “self” in swift)).

And this is our Button:

This part says that on the click on button call function this.onPressLearnMore().


This is the output just when app launch, with default values of myState and “i”.

This is the output after clicking on “Click Here”, at this point onClickLearnMore() function is called:

And Finally below is the output after dismiss “alert”: