Android Basic Button Callback Tutorial

Android callback is very useful when come to specific situation such as button click event. In android, it have many pre-callback method for different purpose such as oncreate, onpause, onresume and more. The callback mean the system will automatically activate the method when the event is occur, so user will dont need to call the method manually. In this tutorial, I will teach you create a callback event in the android application.

Creating a New Project

1. Open Android Studio IDE in your computer.
2. Create a new project and Edit the Application name to “CallbackExample”.
(Optional) You can edit the company domain or select the suitable location for current project tutorial. Then click next button to proceed.
3. Select Minimum SDK (API 15:Android 4.0.3 (IceCreamSandwich). I choose the API 15 because many android devices currently are support more than API 15. Click Next button.
4. Choose “Empty Activity” and Click Next button
5. Lastly, press finish button.

Create a new Interface

Add a new interface by right click the java package > new > new class. Copy and paste the following code in your class.

Add a new Class

Follow the previous step to create a class and paste the following code. This class is to customize the view so that we can use it in the xml layout.

Edit activity_main.xml layout

Use the customize view in your xml, follow by your package name and view class. Eg as following code.

Edit class

Go to your mainactivity class and this class will set the listener to the view and perform click event.

Run your project

After that, now can run your project in the android emulator or android device to test the callback click event.

(Android Callback)

Source Code

(Visited 967 times, 1 visits today)

Yong Loon Ng

Ng Yong Loon, better known as Kristofer is a software engineer and computer scientist who doubles up as an entrepreneur.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *