This example shows how to implement callback method in android.
Algorithm:
1.) Create a new project by File-> New -> Android Project name it CallbackImplementation.
2.) Create and write following into scr/SubClass.java:
package com.example.callbackimplementation; public class SubClass { interface MyCallbackClass{ void callbackReturn(); } MyCallbackClass myCallbackClass; void registerCallback(MyCallbackClass callbackClass){ myCallbackClass = callbackClass; } void doSomething(){ //do something here //call callback method myCallbackClass.callbackReturn(); } }
3.) Write following into main.xml:
<LinearLayout 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" android:orientation="vertical" tools:context=".MainActivity" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <Button android:id="@+id/callsubclass" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Call sub-class" /> <TextView android:id="@+id/result" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>
4.) Run for output.
Steps:
1.) Create a project named CallbackImplementation and set the information as stated in the image.
Build Target: Android 4.4
Application Name: CallbackImplementation
Package Name: com.example.CallbackImplementation
Activity Name: CallbackImplementationActivity
2.) Open CallbackImplementationActivity.java file and write following code there:
package com.example.callbackimplementation; import com.example.callbackimplementation.SubClass.MyCallbackClass; import android.os.Bundle; import android.app.Activity; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; public class CallbackImplementationActivity extends Activity implements MyCallbackClass{ Button buttonCallSubClass; TextView textResult; SubClass mySubClass; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); buttonCallSubClass = (Button)findViewById(R.id.callsubclass); textResult = (TextView)findViewById(R.id.result); mySubClass = new SubClass(); mySubClass.registerCallback(this); buttonCallSubClass.setOnClickListener(new OnClickListener(){ @Override public void onClick(View arg0) { mySubClass.doSomething(); }}); } @Override public void callbackReturn() { textResult.setText("Callback function called"); } }
3.) Compile and build the project.
Output
Learn How to Position the Toast Message
How To Use Custom Fonts In Android
How To Fix Flickering In SpriteGame
How To Use Build-in Android Themes In Your Application
How To Get Installed Activity List
Sort The File In File Explorer By Implementing Comparator in Android Development
How To Get FileExplorer History In Android Development
Learn How To Create ListView From SQLite Database In Android Development