How to extend a Dialog class and write it’s button click on your activity using Interfaces in Android?

By | February 13, 2013

Hello all…….

This tutorial is all about writing your own Dialogs and implementing the click or touch on any of it’s views in another class (say another activity).
This is primarily useful if we have a common dialog and have to handle it’s button click in different ways in different activities.

Here what we will do is
1. Create a class that extends the Dialog.
2. The dialog contains a button , but the click of this button to be handled inside our activity.
3. Write an interface that will be implemented by the activity that handles the click.
So how do we do that..

Interfaces in Android Demo

This is the class that extends the Dialog along with the interface definition.

package com.coderzheaven.interfacedemo;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;

public class MyDialog extends Dialog {

	public MyDialog(Context context, myOnClickListener myclick) {
		super(context);
		this.myListener = myclick;
	}

	
	public myOnClickListener myListener;
        
        // This is my interface //
	public interface myOnClickListener {
		void onButtonClick();
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.alert);

		Button btn = (Button) findViewById(R.id.btn);
		btn.setOnClickListener(new android.view.View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				myListener.onButtonClick(); // I am giving the click to the
											// interface function which we need
											// to implements where we call this
											// class

			}
		});
	}

}

Now the activity that creates the object of the above class, thereby creating a dialog and calls it’s button click.

package com.coderzheaven.interfacedemo;

import com.coderzheaven.interfacedemo.MyDialog.myOnClickListener;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;

public class MainActivity extends Activity {

	public myOnClickListener myListener;

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

		// create new onclicklistener interface //
		myListener = new myOnClickListener() {
			@Override
			public void onButtonClick() {
				System.out.println("I am clicking the button in the dialog");
				Toast.makeText(getApplicationContext(),
						"I am clicking the button in the dialog",
						Toast.LENGTH_LONG).show();
			}
		};

		MyDialog mydialog = new MyDialog(this, myListener);
		mydialog.show();

	}

}

This is the layout for the Dialog.
Alert.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/parent"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#ffffff"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_gravity="left|center"
        android:gravity="left|center"
        android:padding="8dip"
        android:text="Implementing common dialog button click in your own class using interfaces"
        android:textColor="#000000"
        android:textSize="20sp"
        android:textStyle="bold" />
    
    
    <Button
        android:id="@+id/btn"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_gravity="left|center"
        android:text="Click Me"
        android:textSize="20sp"
        android:textStyle="bold" />

</LinearLayout>

Done. Now you have successfully implemented a dialog’s button click on another activity.

3 thoughts on “How to extend a Dialog class and write it’s button click on your activity using Interfaces in Android?

  1. Pingback: How to create transparency in your activity or dialog in android - different methods?

Leave a Reply to James Cancel reply

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