Home » Android » Android Applications » Create an Android Demo App with Button Click Event: Java and Kotlin Guide

Create an Android Demo App with Button Click Event: Java and Kotlin Guide

Creating an Android app with a button click event is essential for any beginner. In this guide, you will learn how to implement it in Java and Kotlin, along with setting up the necessary configuration in the AndroidManifest.xml file.

1. Setting Up the Project

Start by creating a new project in Android Studio and select Empty Activity. This will generate the required files, including AndroidManifest.xml, which is essential for your app to run.

2. AndroidManifest.xml Configuration

Before diving into the code, it’s important to understand the role of AndroidManifest.xml. This file defines essential information about your app, such as the package name, permissions, and activities. Below is the basic configuration for a simple button click app:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.buttonclickdemo">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        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>

</manifest>

This manifest file contains details about the MainActivity, which is the entry point of your application. You can see that it launches when the app starts (LAUNCHER category).

3. Designing the Layout (XML)

Now, define a button in your layout file activity_main.xml:

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Click Me"
    android:layout_gravity="center" />

This places a button in the center of your activity.

4. Handling Button Click in Java

Let’s add functionality to the button in MainActivity.java:

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 {

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

        Button button = findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "Button Clicked!", Toast.LENGTH_SHORT).show();
            }
        });
    }
}

This Java code sets up a click listener for the button. When the button is clicked, a Toast message saying “Button Clicked!” will be displayed.

5. Handling Button Click in Kotlin

For Kotlin users, the equivalent click handler is more concise. Here’s the MainActivity.kt file:

import android.os.Bundle
import android.widget.Button
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val button: Button = findViewById(R.id.button)
        button.setOnClickListener {
            Toast.makeText(this, "Button Clicked!", Toast.LENGTH_SHORT).show()
        }
    }
}

This Kotlin code achieves the same functionality as the Java version.

6. Testing Your Application

Once you’ve written the code, run the app on an emulator or physical device. Upon clicking the button, a message saying “Button Clicked!” will be shown.

Building a basic Android app with a button click event is a great way to start Android development. By understanding how to set up AndroidManifest.xml, define a button in the layout, and handle the click event in both Java and Kotlin, you have taken the first step towards creating interactive Android applications.

As we can see in above code, we have declared a button “btn” and have set its OnClickListner which gets called when someone clicks on this button.

You can download the entire application source code from github from https://github.com/lynxbee/button-click-event-handler-android-app