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
- Open Android Studio → New Project → choose Empty Activity.
- Name: HelloAndroid — Language: Java — Minimum SDK: API 21 (or higher).
- Click Finish and wait for Gradle Sync.
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?
LinearLayoutarranges children vertically and centers them.TextViewdisplays the greeting.Buttontriggers 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.Toastshows 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)
Optional Enhancements
- Move the welcome string to
res/values/strings.xmland reference with@string/…. - Style colors in
res/values/colors.xmland apply viastyles.xmltheme. - 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
Buttonthat resets the text back to “Hello Android!”. - Change the text color and size via XML attributes.
- Show a
Snackbarinstead of aToast(see UI → Snackbar).