Your First Android App (Java)

In this lesson, you’ll create a simple app using Java that changes text and shows a Toast when a button is clicked. We’ll go step by step: New Project → Layout (XML) → MainActivity.java → Manifest → Gradle → Run & Test.

Big Picture

New Project

Empty Activity, Java, API 21+

UI (XML)

TextView + Button

Logic (Java)

Handle button click

Manifest

Declare MainActivity

Gradle

Build & dependencies

Run

Emulator / Device

Step 1 — Create a New Project

  1. Open Android StudioNew Project → choose Empty Activity.
  2. Name: HelloAndroid — Language: Java — Minimum SDK: API 21 (or higher).
  3. Click Finish and wait for Gradle Sync.
Note: If sync is slow, check internet or Gradle proxy settings (see Setup page troubleshooting).

Step 2 — Design the Layout (XML)

Open app/res/layout/activity_main.xml and replace its content with:

<!-- res/layout/activity_main.xml -->
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center"
    android:padding="16dp">

    <TextView
        android:id="@+id/tvMessage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello Android!"
        android:textSize="22sp"/>

    <Button
        android:id="@+id/btnClick"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click Me"
        android:layout_marginTop="12dp"/>

</LinearLayout>

What’s happening?

  • LinearLayout arranges children vertically and centers them.
  • TextView displays the greeting.
  • Button triggers an action when tapped.

Step 3 — Add Java Logic

Open app/java/<your.package>/MainActivity.java and use:

package com.example.helloandroid;

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

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private TextView tvMessage;
    private Button btnClick;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

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

        btnClick.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tvMessage.setText("Welcome to Android with Java!");
                Toast.makeText(MainActivity.this, "Button Clicked", Toast.LENGTH_SHORT).show();
            }
        });
    }
}

Explained:

  • onCreate() is called when the Activity starts. We set the layout and bind views.
  • setOnClickListener() listens for button taps.
  • Toast shows a lightweight popup message.

Step 4 — Manifest (Launcher Activity)

Verify AndroidManifest.xml has your MainActivity declared with a launcher intent:

<application
    android:allowBackup="true"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/Theme.AppCompat.Light.NoActionBar">

    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

</application>

Step 5 — Gradle Quick Check

Open app/build.gradle and confirm (versions are examples):

plugins { 
    id 'com.android.application' 
}

android {
    compileSdkVersion 34

    defaultConfig {
        applicationId "com.example.helloandroid"
        minSdkVersion 21
        targetSdkVersion 34
        versionCode 1
        versionName "1.0"
    }
}

dependencies {
    implementation 'androidx.appcompat:appcompat:1.7.0'
    implementation 'com.google.android.material:material:1.12.0'
}

Step 6 — Run the App

  • Start an emulator in AVD Manager (or connect a real device with USB debugging).
  • Click the green Run ▶ button in Android Studio.
  • Tap the button in the app → text changes and a Toast appears 🎉

Quick Flow (What Happens Internally)

App icon tapped └─> Android starts Launcher Activity (MainActivity) └─> onCreate() inflates activity_main.xml → creates Views └─> User taps Button → onClick listener runs └─> TextView text updates + Toast message shows

Optional Enhancements

  • Move the welcome string to res/values/strings.xml and reference with @string/….
  • Style colors in res/values/colors.xml and apply via styles.xml theme.
  • Change layout to ConstraintLayout (see the UI section for a guide).

Example: strings.xml

<resources>
    <string name="app_name">HelloAndroid</string>
    <string name="hello_text">Hello Android!</string>
</resources>

Troubleshooting

App not installing? Try Build → Clean Project then Rebuild. Ensure emulator/device is visible in Device Manager / adb devices.

Gradle sync failed? Check internet, retry sync, verify SDK path, or clear .gradle/ cache.

Button does nothing? Ensure you called setOnClickListener and IDs in Java match XML IDs.

Practice Tasks

  • Add another Button that resets the text back to “Hello Android!”.
  • Change the text color and size via XML attributes.
  • Show a Snackbar instead of a Toast (see UI → Snackbar).
Tip: Keep Activities small. As your app grows, move logic into helper classes or ViewModels (covered later).