This tutorial will show you how to create a UI for your Android app. In this tutorial you will do the following:

Prerequisites:

We recommend you've programmed in Java before.

The first step to making a new application is to start a new project.

New project window

Conveniently, when you first open Android Studio it will ask you to start a new project.

To get started click the "Start a new Android Studio project" button

New Project window when all projects are closed

Create Android Project

Create Android Project window with example inputs

Target Android Devices

Target Android Devices window with API 27 selected

Add an Activity to Mobile

Add an Activity to Mobile to choose what kind of application to build

Configure Activity

Configure Activity window to set activity name

Hit Finish and you're good to go!

But wait....

....what's this error?

An error on API compatiblility!?

Fixing the error we saw in the last step is easy, we just need to set up an Android Virtual Device (AVD) for our app to run on

Open AVD manager

AVD Manager Menu Path

Android Virtual Device Manager

Create New AVD - Select Hardware

Select Hardward panel to choose screen size

API Selection

System Image panel, showing API options

Android Virtual Device detail screen

Android Virtual Device customization

Back to the Android Virtual Device Manager

Virtual device manager with an AVD listed

Back to the main page

AVD selection window

Android App running in AVD and the error message is gone!

Huh. Looks kind of boring, doesn't it?
Let's see if we can't fix that, shall we?

Now that we have all the background set up its time to start making your GUI.
The first step is to open up the graphical layout so you can place buttons more easily.
This is found either along the top bar, as "activity_main.xml," or in the project filedraw under app > res > layout > activity_main.xml.

activity_main.xml locations

Once you have your activity_main.xml screen open it should look like the below:

activity_main.xml screen layout

On the lefthand side of this picture are quick links to various kinds of elements while the rightside consists of a blueprint of the layout.
In the blueprint right now you can see that there is one element already, the "Hello world!" text that can be seen in the previous section and is added by default.
The zig-zag lines indicate that this text has constraints on it to keep it a relative distance to each of the edges indicated by the lines.

For this tutorial we're going to be implementing this from scratch so delete this text by clicking on it and pressing the backspace button.

Adding your first GUI element

ur blueprint and then dragging your button until dotted lines show up, as in the picture below.

Adding a button element to the blueprint and letting Android Studio take care of constraints

Blueprint with constraints automatically added

Blueprint with attribute panel

You can also customize your button via its attributes panel. This pannel is shown to the right of the blueprint when you select your button.
This can be used to change things about the button such as the text therein, which is controlled via the TextView > text attribute.
One attribute you should change is the ID up at the top of the pannel. This is how we'll be referring to this element in our code so it's best to pick something descriptive. I'm going to change mine to goButton.

A quick word on constraints

Constraints make your application look good.
They determine how close or far elements are to each other and to the sides.
They also can be tricky to set correctly in complex layouts.

I won't be going into great detail about how to set constraints in this tutorial. Instead I'll be largely using automatically set constraints. There are other, more detailed tutorials about how to make pretty GUIs I recommend you check out.

All that said, constraints are controlled by defining relationships between elements (dragging from the little circle on the side of an element to another element), and using the attributes panel to define how you want them to be enforced.

There are three types of constraints used in Android

  1. Fixed: The width of the element is fixed at all times and the element floats relative to other objects
  2. Wrap content: The element will expand to fit its contents and float relative to other objects
  3. Math constraints: The element will expand as much as possible to fill all the space it can between it and other objects.

More details on constraints can be found at https://developer.android.com/training/constraint-layout/

Check your GUI out!

Press the "Run" button (or hit cntl-R) and select your AVD and your app should will run.

Your button in your GUI!

It's exciting, but isn't that GUI a little bit boring?
Let's make it a tiny bit more exciting in the next step, shall we?

In order to fix how boring our app currently is let's add in some cute cat pictures! (Note: feel free to use puppy pictures instead)

Setup

First off, we need to create an assets folder to keep our photos of kittens. We do this by right-clicking on the "app" in the project explorer and navigating to New > Folder > Assets Folder.

Create Assets folder menu

This brings up a dialogue where you have nothing to do but press "Finish". Super simple, right?

Create Assets folder dialogue

But wait, there's more!
While you can stop here, it's always best to keep folders so you can be organized. So let's make a directory inside our new assets folder. To do this, right-click on the assets folder and navigate to New > Directory and give it a name. Mine's going to be kitten_pictures.
This folder should now show up in our app directory tree.

App directory tree with kitten_pictures folder

Storing pictures to your project

Adding pictures to your folders is super simple: You just drag them to it. This may not work directly from your browser but you can drag from your desktop and they will be copied over. A quick dialogue will show up to ask if you want to change their name or update references to them, but you can generally just click "OK" for this and they'll be added in.
Android Studio may open up the photos for you to edit them but you can just close them again if it does so.

Add 5 or so photos to your assets folder – we'll be using them later on

Adding an ImageView to your layout

Adding an ImageView element to your layout is similar to adding a button, but with one added step.
First, find the ImageView element from your palette. You can do this under all, or you can select just images.
Next, click and drag the "ImageView" element from the palette to your layout, just like you did with the Button element. As before, it's recommended you try to take advantage of the auto-constraints added by Android Studio, as seen below.

Adding an ImageView element to the layout

When you let got it will pop up and give you a list of Resources to choose from. These are, for our purpose, essentially placeholder images so just pick "ic_launcher."

Choosing a default resource image for our ImageViwer

You'll now see the ImageView show up in your layout. Click on it and on the attributes pannel to the right you can give it a name. Mine is named "kittenView" since that is what it will be used for. As with the button element, this is how we'll be referring to this element in our code so it's best to pick something descriptive.

ImageView with attribute panel

Hit the run button and we can see the default image show up in our AVD.

Default image in AVD

Making your image show in your ImageView

Now that we have the placeholder, it's time to fill it with stuff.
That means it's time to code!

Open your MainActivity.java file and find your OnCreate function. It should be the only one there so it should be easy to find.
This function is run when this view first appears. If you want anything run whenever you launch your app, such as setting an image in your ImageView, it should go here.

To display your image there are three aspects that need to be taken care of.

  1. Access the ImageView that you want to put the picture in. This is done by selecting it by the ID we assigned earlier.

    • ImageView mKittenView = (ImageView) findViewById(R.id.kittenView);
  2. Load your targetted image from assets

    • InputStream image_stream = getAssets().open(path_to_picture_in_assets)
  3. Set the ImageView to display your image

    • mKittenView.setImageBitmap(BitmapFactory.decodeStream(image_stream))

Put these all together in your code, referencing your own ImageView and photos, and you should have a brand new app to run.

Push the run button and you'll see your new app pop up with an image and a button!

Hopefully you picked cute animal pictures, too, so your app is this cute

But wait, what about that button? Is it really not going to do anything?

It's kind of weird that we have a button that does nothing, right? So let's make it do something. The first step is to set up the hook that will be called when the button is clicked.

Programatic hooks

In order to implement a button click, we need to make a method when the button is clicked. This method should be in MainActivity.java and be a void-type as well as taking a View as an argument. The basic outline is as below:

public void onClickGoButton(View v){
    //Add your code here
}

Enabling button click method

Now that we have the method that will execute, even if it is just an outline, we can set up the connection between the button element and this hook.
To do so, pull up the layout file, activity_main.xml, again. Click on your button element and look for "onClick" in its attributes. From the dropdown menu select your hook function that you wrote above.

Selecting onClick action for Button element

Connecting you ImageView element with your Button element

The final step is making your button do something interesting when you click it.

How about making it so it changes the photo to something random?

In this tutorial we've covered how to set up Android Studio and an Android Virtual Device. Also, we've programmed our first GUI application that displays images and has a button that you can click!

On your own you should've put in all the code as well as making the button change to a random photo.