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 
[bash] <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="" xmlns: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> [/bash]

Following is the code of AndroidManifest.xml

$ vim app/src/main/AndroidManifest.xml 
[bash] <?xml version="1.0" encoding="utf-8"?> <manifest xmlns: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> [/bash]

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

$ vim app/src/main/java/com/demoapp/clickbutton/ 
[bash] package com.demoapp.clickbutton; import; 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(; btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Log.e(LOG_TAG, "Build.TYPE :: " + Build.TYPE); } }); } } [/bash]

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

Leave a Comment