Paramclasses Academy - Blog

Tutorials Details

←
Post 4 of 40
→

How to Create Your First Android App in Java: A Step-by-Step ?

November 2, 2026

Introduction

Creating your first Android application is an important step in learning Android development. Android Studio provides all the tools required to design application screens, write Java code, and run applications on Android devices.

In this tutorial, we will create a simple Android application using Java and XML.

Our application will display a welcome message and a button. When the user clicks the button, the application will display a message on the screen.

Through this practical project, you will learn how to:

  • Create an Android project in Android Studio.

  • Understand the basic Android project structure.

  • Design an application screen using XML.

  • Write Java code inside MainActivity.

  • Connect XML components with Java code.

  • Handle button click events.

  • Run and test your first Android application.

By the end of this tutorial, you will have a working Android application.

1. What Are We Going to Build?

First, let's understand our project.

We will create a simple application named My First App.

The application will contain:

  • A heading that displays "Welcome to Android".

  • A TextView that displays a short description.

  • A Button with the text "Click Me".

  • A message that appears when the user clicks the button.

For example, when the application opens, the user will see a welcome screen.

When the user clicks the button, the application will display:

"Congratulations! Your First Android App is Working."

This small project will help you understand how Java and XML work together in Android development.

2. Create a New Android Studio Project

First, open Android Studio.

Follow these steps:

Step 1: Open Android Studio.

Step 2: Click on New Project.

Step 3: Select the Empty Views Activity template.

Make sure you select a Views-based template because we are using XML layouts and Java.

Step 4: Click Next.

Step 5: Enter the project details:

  • Name: MyFirstApp

  • Package Name: com.example.myfirstapp

  • Language: Java

  • Minimum SDK: Select a suitable available Android version.

Step 6: Click Finish.

Android Studio will create the project.

Wait until Gradle synchronization is completed before moving forward.

Important: If Android Studio generates Kotlin code, check your selected template and language settings. This tutorial uses Java.

3. Understand the Important Project Files

After creating the project, Android Studio will display the project structure.

For this application, we will mainly work with two files.

MainActivity.java

MainActivity.java contains the Java code that controls the behavior of our application.

We will use this file to:

  • Load the application layout.

  • Connect Java code with XML components.

  • Handle button clicks.

  • Display messages to the user.

activity_main.xml

This file contains the XML layout of our application screen.

We will use it to create:

  • A welcome heading.

  • A description.

  • A button.

Java controls the application logic, while XML defines the user interface in this project.

4. Design the Application Screen Using XML

Now let's design our application screen.

Open:

app > res > layout > activity_main.xml

Remove the existing layout content and add the following XML code:





    

    

    

Understanding the XML Code

Let's understand the important parts of this layout.

1. LinearLayout

android:orientation="vertical"

LinearLayout is a ViewGroup that arranges its child components in a single direction.

The vertical orientation places components one below another.

2. layout_width and layout_height

android:layout_width="match_parent"
android:layout_height="match_parent"

These properties define the width and height of the layout.

match_parent tells the layout to use the available size of its parent.

3. gravity

android:gravity="center"

This property positions the child components in the center of the LinearLayout.

4. TextView


 

TextView is used to display text on the application screen.

5. EditText and Button

We are not using EditText in this project. Instead, we are using a Button to perform an action when the user clicks it.

6. android

android:id="@+id/btnClick"

The ID uniquely identifies the Button in the layout.

We will use this ID in Java to access the Button.

7. textSize and textColor

These properties control the size and color of displayed text.

The sp unit is commonly used for text size, while dp is commonly used for layout dimensions and spacing.

5. Write Java Code in MainActivity.java

Now that our application screen is ready, we need to add functionality.

Open:

app > java > your_package_name > MainActivity.java

Add the following Java code.

Keep the package declaration generated by Android Studio at the top of your file.

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    Button btnClick;

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

        setContentView(R.layout.activity_main);

        btnClick = findViewById(R.id.btnClick);

        btnClick.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Toast.makeText(
                    MainActivity.this,
                    "Congratulations! Your First Android App is Working.",
                    Toast.LENGTH_LONG
                ).show();

            }
        });
    }
}

If Android Studio has already added some imports, do not add duplicate import statements.

If your generated Activity contains extra edge-to-edge setup code, use the simple Activity structure above for this example and remove unused imports.

6. Understand the Java Code Step by Step

Now let's understand how the application works.

Step 1: Import Required Classes

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

These import statements allow us to use the required Android classes.

  • Bundle is used to pass saved Activity state information.

  • View is the base class for UI components.

  • Button represents a clickable button.

  • Toast displays a short message.

  • AppCompatActivity provides an Activity with compatibility support.

Step 2: Create the MainActivity Class

public class MainActivity extends AppCompatActivity {

Here, we create a class named MainActivity.

It extends AppCompatActivity, which allows our class to behave as an Android Activity.

An Activity represents a screen or an entry point for user interaction in an Android application.

Step 3: Declare the Button Variable

Button btnClick;

Here, we declare a Button variable.

This variable will hold the reference to the Button defined in our XML layout.

Step 4: Understand the onCreate() Method

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

The onCreate() method is called when the Activity is created.

It is commonly used to initialize the screen and set up application components.

Step 5: Load the XML Layout

setContentView(R.layout.activity_main);

This statement loads the activity_main.xml layout on the screen.

Without loading the correct layout, we cannot access its UI components using the IDs defined in that layout.

Step 6: Connect the Button with Java

btnClick = findViewById(R.id.btnClick);

This is an important statement.

The findViewById() method finds a UI component using its ID.

Our XML Button has this ID:

@+id/btnClick

Java accesses the same component using:

R.id.btnClick

Now the Java variable btnClick refers to the Button displayed on the screen.

Step 7: Handle the Button Click

btnClick.setOnClickListener(new View.OnClickListener() {

This statement sets a click listener on the Button.

A click listener allows us to execute code when the user clicks a UI component.

Step 8: Display a Toast Message

Toast.makeText(
    MainActivity.this,
    "Congratulations! Your First Android App is Working.",
    Toast.LENGTH_LONG
).show();

This code displays a Toast message.

  • MainActivity.this provides the Activity context.

  • The second argument contains the message.

  • Toast.LENGTH_LONG controls how long the message is displayed.

  • show() displays the Toast.

Now our button is working!

7. Run Your First Android Application

After completing the XML and Java code, it's time to run the application.

Follow these steps:

Step 1: Save your files.

Step 2: Open Device Manager in Android Studio.

Step 3: Start an existing Android Emulator or create a new virtual device.

Step 4: Wait until the emulator starts completely.

Step 5: Select your emulator from the device selection menu.

Step 6: Click the Run button in Android Studio.

Step 7: Wait for the application to build and install.

Your application should now open on the emulator.

Click the "Click Me" button.

You should see the following message:

"Congratulations! Your First Android App is Working."

Congratulations! You have successfully created your first Android application using Java and XML.

8. How Does Java Communicate with XML?

This is one of the most important concepts in traditional Android development.

XML defines the user interface, while Java controls its behavior.

For example:

In XML, we create a Button:


 

In Java, we access the same Button:

Button btnClick = findViewById(R.id.btnClick);

Then we handle its click event:

btnClick.setOnClickListener(view -> {
    Toast.makeText(
        MainActivity.this,
        "Button Clicked",
        Toast.LENGTH_SHORT
    ).show();
});

The complete process is:

  1. XML creates the Button.

  2. The Button receives a unique ID.

  3. Java accesses the Button using findViewById().

  4. Java attaches a click listener.

  5. The listener executes code when the user clicks the Button.

This is how Java and XML work together in a traditional Android application.

9. Common Errors and Their Solutions

While creating your first Android application, you may encounter some errors.

Let's understand a few common problems.

Error 1: Cannot Resolve Symbol R

Possible reason:

The project may have an XML error or an incomplete build.

Solution:

  • Check your XML layout for errors.

  • Wait for Gradle synchronization.

  • Build the project again.

  • Check that your resource files are in the correct folders.

Do not manually import android.R for your application resources.

Error 2: Button Is Not Working

Possible reason:

The Button ID in XML and Java may not match.

Solution:

Check the XML ID:

android:id="@+id/btnClick"

Check the Java reference:

findViewById(R.id.btnClick);

Both IDs must match.

Error 3: Application Crashes

Possible reasons:

  • Incorrect view ID.

  • Wrong layout loaded.

  • Null reference.

  • An exception in application code.

Solution:

Open Logcat and check the error message.

Look for the exception and identify the line where the problem occurs.

Error 4: XML Layout Is Not Displaying

Possible reason:

The wrong layout file may be loaded.

Solution:

Check this statement:

setContentView(R.layout.activity_main);

Make sure the activity_main.xml file exists inside the res/layout folder.

Error 5: Gradle Build Failed

Possible reasons:

  • Missing dependencies.

  • Internet connection issues.

  • Incorrect build configuration.

Solution:

Read the Build output, identify the actual error, and fix the relevant configuration.

10. Practice Tasks

Now try these small changes yourself to improve your understanding.

Task 1: Change the heading from "Welcome to Android" to "My First Application".

Task 2: Change the button text from "Click Me" to "Start".

Task 3: Change the Toast message to "Hello! I am learning Android Development".

Task 4: Change the heading text size from 26sp to 30sp.

Task 5: Change the background color of the application screen.

These exercises will help you understand how XML properties and Java code affect your application.

11. Conclusion

In this tutorial, we created our first Android application using Java and XML.

We learned how to create a new Android Studio project, design an application screen using LinearLayout, add TextView and Button components, connect XML views with Java using findViewById(), and handle button clicks using setOnClickListener().

We also learned how to display Toast messages, run applications using an emulator, and troubleshoot common development errors.

The most important concept to remember is that XML defines the user interface, while Java handles the application logic.

In the next tutorial, we will learn Java basics for Android development, including variables, data types, and operators.

12. Frequently Asked Questions (FAQs)

Q1. How do I create my first Android application in Android Studio?

Open Android Studio, create a new Empty Views Activity project, select Java, design your XML layout, write Java code, and run the application on an emulator or real device.

Q2. Which programming language is used in this tutorial?

We use Java for application logic and XML for designing the user interface.

Q3. What is MainActivity.java?

MainActivity.java is a Java Activity class that manages the behavior and interaction of an application screen.

Q4. What is activity_main.xml?

It is an XML layout file that defines the user interface of the Activity in our example.

Q5. What is findViewById() in Android?

findViewById() is used to find a UI component using its ID so that it can be accessed from Java code.

Q6. What is setOnClickListener() in Android?

setOnClickListener() is used to execute code when a user clicks a View, such as a Button.

Q7. What is Toast in Android?

Toast is a component used to display a short message to the user.

Q8. Can I run my first Android application without a physical phone?

Yes. You can use the Android Emulator provided by Android Studio to run and test your application.


13. SEO Details

SEO Title: How to Create Your First Android App in Java Step by Step

Meta Description: Learn how to create your first Android application using Java and XML in Android Studio. Follow this beginner-friendly guide to design a screen, handle button clicks, and run your app.

Focus Keyword: Create First Android App in Java

Related Keywords:

  • Android App Development for Beginners

  • First Android App Tutorial

  • Android Studio Java Tutorial

  • Create Android App Using Java

  • Android XML Layout Tutorial

  • MainActivity Java Explained

  • Android Button Click Example

  • Android Studio Beginner Project

Suggested URL Slug: create-first-android-app-java

 

←
Post 4 of 40
→