Android (Java) Tutorial

Welcome! This track teaches Android app development using Java. You’ll set up Android Studio, understand the project structure, learn Activities, layouts, resources, and build your first app.

Your First App (Java): “Hello Android”

Step 1: Create a New Project

Open Android Studio, go to File → New → New Project. Choose Empty Activity, give your project a name (HelloAndroid), set the language to Java, and choose a Minimum SDK (API 21 is recommended). After you click Finish, Android Studio will generate the starter files for you.

Step 2: Design the Layout

The layout is written in XML. It defines how your screen looks. Here, we’re placing a TextView (to show text) and a Button.

<!-- XML layout for the main screen -->
<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>

Explanation: - LinearLayout arranges items vertically. - TextView shows static text. - Button lets users interact by clicking.

Step 3: Add Java Code

Now let’s connect the layout with logic. In MainActivity.java, we reference the button and text, then set a click listener to change the text and show a Toast message.

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();
            }
        });
    }
}

Explanation: - findViewById() connects UI elements to Java code. - setOnClickListener() runs code when the button is clicked. - Toast is a small popup message.

Step 4: Declare in Manifest

<application
    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>

The AndroidManifest.xml declares all components of your app. Here we mark MainActivity as the launcher activity with an intent filter.

Step 5: Gradle Setup

android {
    compileSdkVersion 34

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

Explanation: - compileSdkVersion tells Gradle which Android APIs you can use. - minSdkVersion is the oldest Android version your app supports. - targetSdkVersion is the version you’ve tested your app against.

Step 6: Run Your App

  • Start an emulator from AVD Manager, or connect a real Android device.
  • Click the green ▶ Run button in Android Studio.
  • Tap the button → text changes and a Toast appears 🎉
Tip: If you see the text change and Toast on button click, your setup is correct!