Pop Ups(Toast, AlertDialog, Layout) Tutorial for Android

Toast, AlertDialog Box

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface.OnClickListener;
import android.widget.Toast;

public class MyPopUps {

	public static void ShowToast(Context context, String msg){
		Toast.makeText( context,
                		msg,
                		Toast.LENGTH_LONG ).show()
                		;
	}

	public static void ShowAlertBox(Context context, String msg){
		Builder failureDialog = new AlertDialog.Builder(context)
									.setTitle("Dialog Title")
									.setMessage(msg)
									.setPositiveButton("OK",null)
									;
		failureDialog.show();
	}

	public static void ShowAlertBox(Context context, int msg){
		Builder failureDialog = new AlertDialog.Builder(context)
									.setTitle("Dialog Title")
									.setMessage(msg)
									.setPositiveButton("OK",null)
									;
		failureDialog.show();
	}

	public static void ShowAlertBox(Context context, String msg, String neutralActionName, OnClickListener neutralClickListener){
		Builder failureDialog = new AlertDialog.Builder(context)
									.setTitle("Dialog Title")
									.setMessage(msg)
									.setPositiveButton("Positive Action - OK",null)
									.setNeutralButton("Neutral Action - Save", neutralClickListener)
									.setNegativeButton("Negative Button - Cancel", null)
									;
		failureDialog.show();
	}

	public static void ShowAlertBox(Context context, int msg, String neutralActionName, OnClickListener neutralClickListener){
		Builder failureDialog = new AlertDialog.Builder(context)
									.setTitle("Dialog Title")
									.setMessage(msg)
									.setPositiveButton("Positive Action - OK",null)
									.setNeutralButton("Neutral Action - Save", neutralClickListener)
									.setNegativeButton("Negative Button - Cancel", null)
									;
		failureDialog.show();
	}

}

Calling Functions

MyPopUps.ShowAlertBox(Splash.this, "Neutral Alert", "Save",
				  	new android.content.DialogInterface.OnClickListener(){
						@Override
						public void onClick(DialogInterface dialog,int which) {
							MyPopUps.ShowToast(Splash.this, "Saved");
						}
				  	}
				  );

MyPopUps.ShowAlertBox(Splash.this, R.string.app_name);

Layout

We can also make customized popups by using Relative Linear Layouts and setting their Visible property Gone and visible as required.

RelativeLayout rltInfoDialog;
rltInfoDialog = (RelativeLayout) findViewById(R.id.rlt_InfoDialog);
rltInfoDialog.setVisibility(View.GONE);
// ------------------------- //
public void hideInfoBox(){
	rltInfoDialog.setVisibility(View.GONE);
	tvTitle.setText("");
	tvDescription.setText("");
}
// ------------------------- //
public void showInfoBox(string title, string description){
	rltInfoDialog.setVisibility(View.VISIBLE);
	tvTitle.setText(title);
	tvDescription.setText(description);
}
// ------------------------- //
Call showInfoBox() and hideInfoBox() methods
where required to show and hide info box.

Custom Dialog
See https://mfarhan133.wordpress.com/2010/10/14/custom-dialog-tutorial-for-android/

Advertisements
This entry was posted in Android, Tutorials and tagged , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s