comment 0

Simple AlertDialog with Highlight Button

Hi Comrades !! :woot: , last friday my friend in the office ask me how to highlighting button in alertdialog. So I write this simple tutorial. In this tutorial, I want to show you an alert box with custom highlight button. In this case I want to highlighting the button_negative. So, Here is the codes :fufu:

1. Android Layout Files
first , the file res/layout/activity_main.xml

<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" >
 
   <Button
       android:id="@+id/alertButton"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_alignParentTop="true"
       android:layout_centerHorizontal="true"
       android:text="Show Alert Box" />
 
</RelativeLayout>

Sponsored links


2. Main Activity
second, the MainActivity.java

package com.r3m1ck.highlightbutton;
 
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
 
public class MainActivity extends Activity {
 
	final Context context = this;
	private Button button;
 
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
 
		button = (Button) findViewById(R.id.alertButton);
 
 
 
 
		//add button listener
		button.setOnClickListener(new OnClickListener() {
 
			public void onClick(View v) {
				// TODO Auto-generated method stub
				AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
 
				//set alertdialog title
				alertDialogBuilder.setTitle("Alert Box Dialog");
 
				//set alertdialog message
				alertDialogBuilder
					.setMessage("Do you want to exit ??")
					.setCancelable(false)
					.setPositiveButton("Yeah!!", new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							// TODO Auto-generated method stub
							//close this simple app
							Toast.makeText(getApplicationContext(), 
									"You exit the simple app", Toast.LENGTH_SHORT).show();
							MainActivity.this.finish();
						}
					})
					.setNegativeButton("Nope", new DialogInterface.OnClickListener() {
 
						@Override
						public void onClick(DialogInterface dialog, int which) {
							// TODO Auto-generated method stub
							//close the dialog
							Toast.makeText(getApplicationContext(), 
									"You clicked on Nope", Toast.LENGTH_SHORT).show();
							dialog.cancel();
						}
					});
 
 
					// create alert dialog
					AlertDialog alertDialog = alertDialogBuilder.create();
 
					// show the alert dialog
					alertDialog.show();
 
					//set focus highlight on cancel button
			        Button negative = ((AlertDialog) alertDialog)
			                .getButton(AlertDialog.BUTTON_NEGATIVE);
			        negative.setFocusable(true);
			        negative.setFocusableInTouchMode(true);
			        negative.requestFocus();
			}
 
		});
	}
}

3. Demo
Run the app,
in_app_first_launch

When Alert Dialog show, the hightlight will automaticaly in button_negative
show_dialog_with_highlight_button

When Button Negative clicked,
when_negative_button_clicked

When Button_Positive clicked,
when_positive_button_clicked

Happy Coding Comrades !! :musik:

Leave a Reply

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