Android demo application with button click event

Following source code describes a simple demo application which shows a button which when clicked prints a message in logcat. You can customise this application as per need to handle the button click event by implementing necessary code inside click handler.

Below is the code for activity layout which uses just a single button which displays text “ClickButton” on it.

$ vim app/src/main/res/layout/activity_main.xml 
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.demoapp.clickbutton.MainActivity">

    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="ClickButton" />

</RelativeLayout>

Following is the code of AndroidManifest.xml

$ vim app/src/main/AndroidManifest.xml 
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.demoapp.clickbutton">

    <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/AppTheme">
        <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>

As we can see in above MainActivity.java will be the launchable activity of this app,

$ vim app/src/main/java/com/demoapp/clickbutton/MainActivity.java 
package com.demoapp.clickbutton;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.os.Build;
import android.widget.Button;

import com.demoapp.clickbutton.R;

public class MainActivity extends AppCompatActivity {

    Button btn;
    String LOG_TAG = "MainActivity";

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

        btn = (Button) findViewById(R.id.btn);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.e(LOG_TAG, "Build.TYPE :: " + Build.TYPE);
            }
        });
    }
}

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

Leave a Comment