Monday, 10 December 2018

Concept of "Callback" or listener in android | Example of Callback or listener in Android

I am going to a basic example of custom callback method in android. We will follow step by step. First of all open Android studio and create a new project and open MainActivity.java class and than click on activity_main.xml  layout and do the following:-

Step 1:-
<?xml version="1.0" encoding="utf-8"?>
<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:id="@+id/name"
        android:layout_margin="20dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="==" />

    <Button
        android:id="@+id/next"
        android:gravity="center"
        android:layout_marginTop="60dp"
        android:layout_gravity="bottom"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Next" />


</LinearLayout>
Step 2 Open MainActivity.java and do the code like following:-
public class MainActivity extends AppCompatActivity {
    TextView tvName;
    Button next;

    @Override    
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tvName = (TextView) findViewById(R.id.name);
        next = (Button) findViewById(R.id.next);
        next.setOnClickListener(new View.OnClickListener() {
            @Override            
public void onClick(View view) {
                startActivity(new Intent(MainActivity.this,ClassA.class));
            }
        });
        ClassB.setCallback(new onDataUpdateListner() {
            @Override            
public void getData(String name, String roll) {
                Toast.makeText(MainActivity.this,"Android00---",Toast.LENGTH_LONG).show();
                tvName.setText("Name="+name+"\n"+"Roll="+roll);
            }
        });
    }

}
Step 3 now create interface which will carry the callback method like following Here i created 2 interface :-
public interface onDataUpdateListner {
     //void setData();     
void getData(String name,String roll);
}

public interface onDataUpdateListnerA {
     void getDataA(String name, String roll);
}
Step 4 Now create 2 class ClassA and ClassB and code will be like following:-
ClassA:-
class_a.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=".MainActivity">

    <TextView
        android:id="@+id/roll"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="RollNumber"
        android:gravity="center_vertical"
        android:textAlignment="center"
        android:layout_margin="20dp"/>

    <Button
        android:id="@+id/next"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/roll"
        android:layout_centerHorizontal="true"
        android:gravity="center"
        android:text="Next" />

</RelativeLayout>

public class ClassA extends AppCompatActivity {
    TextView tvRoll;
    Button next;

    @Override    
protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.class_a);
        tvRoll = (TextView) findViewById(R.id.roll);
        next = (Button) findViewById(R.id.next);
        next.setOnClickListener(new View.OnClickListener() {
            @Override            
public void onClick(View view) {
                startActivity(new Intent(ClassA.this, ClassB.class));
            }
        });


        ClassB.setCallbackA(new onDataUpdateListnerA() {
            @Override            
public void getDataA(String name, String roll) {
                tvRoll.setText("Name=" + name + "\n" + "Roll=" + roll);
            }
        });
    }

}
ClassB:-
class_b.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=".MainActivity">

    <EditText
        android:id="@+id/enter_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:hint="Name" />

    <EditText
        android:id="@+id/enter_roll"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/enter_name"
        android:layout_margin="20dp"
        android:hint="Roll Number" />

    <Button
        android:id="@+id/ok"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/enter_roll"
        android:layout_centerHorizontal="true"
        android:gravity="center"
        android:text="Ok" />

</RelativeLayout>

public class ClassB extends AppCompatActivity {
    EditText edName, edRoll;
    Button ok;
    public static onDataUpdateListner onDataUpdateListner;
    public static onDataUpdateListnerA onDataUpdateListnerA;

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

        edName = (EditText) findViewById(R.id.enter_name);
        edRoll = (EditText) findViewById(R.id.enter_roll);
        ok = (Button) findViewById(R.id.ok);
        ok.setOnClickListener(new View.OnClickListener() {
            @Override           
 public void onClick(View view) {
                if(ClassB.onDataUpdateListner != null && ClassB.onDataUpdateListnerA!= null) {
                    onDataUpdateListner.getData(edName.getText().toString(),edRoll.getText().toString());
                    onDataUpdateListnerA.getDataA(edName.getText().toString(),edRoll.getText().toString());
                }

                finish();
            }
        });
    }

    public static void setCallbackA(onDataUpdateListnerA callbackA) {
        onDataUpdateListnerA = callbackA;
    }
    public static void setCallback(onDataUpdateListner listner ) {
        onDataUpdateListner = listner;
    }
}
Output:-

No comments:

Post a Comment