Android Hello World Example Project File Structure

This article will show you how to use android studio to create android Hello World example. It also explains the main example files and their role and relation.

1. Create android hello world example steps.

You can read article Android Development Environment Setup first if you do not know.

  1. Open android studio from start up menu.
    android studio startup menu
  2. Click Start a new Android Studio project.
    android studio create new project
  3. Input android project settings such as application name, domain name and project save directory in next dialog.
    android project settings
  4. Choose default activity for this project. Select ” Empty Activity ” in this example.
    add android project default activity
  5. Input activity name and it’s layout name, use default values here.
    activity name and layout settings
  6. Click Finish buton to complete the setup wizard.

2. Android project file explanation.

Now the new android project has been created completely, you can see there has a lot of files in left panel tree. The most important three file are AndroidManifest.xml , MainActivity.java and activity_main.xml, we will explain them one by one.

The relationship between above three files.

AndroidManifest.xml MainActivity activity_main.xml relation

android project file list tree panel

  1. AndroidManifest.xml : When app start, android system will read this file first, it is a xml file which includes the default activity information. Android will create the activity component configured in it. From this file we can see the default activity is MainActivity. If you want android to use another activity as startup activity, you can change the settings here.
  2. MainActivity.java : Double click this file in left panel, you can see it extends android.support.v7.app.AppCompatActivity class, and it should override onCreate(Bundle savedInstanceState) method. This method will be executed when this activity is called.
    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        }
    }
    

    setContentView(R.layout.activity_main) in above code is used to set the content view layout file. The layout file will control how to display the UI components in activity view. And it is referenced with R.layout.activity_main. activity_main is just the layout file name.

    Android tools create the R class dynamically before compiling the java source code, it include all the resource files such as layout, UI components, image, string, id etc. You can use it to reference all the resources.

  3. activity_main.xml : This file located at res / layout folder. It defines what components to display and how to display them in referenced activity view. Double click this file, you can see the content in right panel, it has Design view and Text view, you can click bottom Design and Text tab to switch. Our example activity_main.xml has been customized to below. We add id to the TextView component, set it’s layout width and height value to match_parent, change the text, text color, size and style.
    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.dev2qa.www.helloworldexampleproject.MainActivity">
    
        <TextView
            android:id="@+id/helloWorldTxt"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="Hello World from dev2qa.com"
            android:textColor="@android:color/holo_green_dark"
            android:textSize="36sp"
            android:textStyle="bold"
            android:visibility="visible"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.298" />
    
    </android.support.constraint.ConstraintLayout>
  4. Right click MainActivity.java, click ” Run MainActivity ” menu from the popup menu list or click the Run button in top tool bar to execute the app.
    click run mainactivity to execute android app
  5. Select the virtual device that you have already added. Then wait for it to startup, after some time, you can see the app displayed in a device simulator.
    android virtual device select list
    hello world example simulator output

3. How to get the TextView component in java code.

From above activity_main.xml, we add a id attribute to TextView component. If you want to get that component in java code, you can use below code.

 TextView textView = (TextView) this.findViewById(R.id.helloWorldTxt);

Change java code to below for MainActivity onCreate method.

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        TextView tv = (TextView) this.findViewById(R.id.helloWorldTxt);
        tv.setText("dev2qa.com is a good tutorial website.");
        tv.setTextColor(Color.RED);
    }

Do not forget import below classes.
import android.graphics.Color;
import android.widget.TextView;

Now run MainActivity.java again, you can find text content and color changed in device simulator.

custom textview text and color output in device simulator

4. Create android apk file.

  1. Click ” Build —> Build APK ” in Android Studio tool bar.
    build android apk executable file
  2. After building you can see a popup dialog at right bottom. Click the link to show it in explorer.
    build android apk show in explorer
  3. Generally the apk file saved at your android ouput folder like below.
    HelloWorldExampleProject\app\build\outputs\apk
(Visited 433 times, 2 visits today)
READ :   Spring Boot Hello World Example In STS

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.