alertDialog.getButton() method gives null pointer exception android

AndroidAndroid Alertdialog

Android Problem Overview


Iam planing to give create 3 buttons with layout_weight=1, not interested in custom dialog.So I have written below code.It is not working.Always yes button gives me null. Whats wrong in this code?

  AlertDialog dialog= new AlertDialog.Builder(this).create();
    		dialog.setIcon(R.drawable.alert_icon);
    		dialog.setTitle("title");
    		dialog.setMessage("Message");
    		dialog.setButton(AlertDialog.BUTTON_POSITIVE,"Yes", new DialogInterface.OnClickListener() {
    
    			@Override
    			public void onClick(DialogInterface arg0, int arg1) {
    											}
    		});
    		Button yesButton = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
    		Log.w("Button",""+yesButton);//here getting null
    		LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, 1f);
    		yesButton.setLayoutParams(layoutParams);
    		dialog.show();

Regards, Android developer.

Android Solutions


Solution 1 - Android

Take a look here for the answer : http://code.google.com/p/android/issues/detail?id=6360

As it says in comment #4 you must call show() on your dialog before you can access the buttons, they are not available beforehand. For an automatic solution on how to modify the buttons as soon as they are ready see Mickeys answer

Solution 2 - Android

This works for me :

AlertDialog alertDialog = new AlertDialog.Builder(this)
				.setMessage(message)
				.setCancelable(true)
				.setPositiveButton("Yes",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
							//do smthng
						})
				.setNegativeButton("No", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int id) {
						//do snthn
					}
				}).create();
		
		alertDialog.setOnShowListener(new OnShowListener() {
			@Override
			public void onShow(DialogInterface dialog) {					//
				Button positiveButton = ((AlertDialog) dialog)
						.getButton(AlertDialog.BUTTON_POSITIVE);
				positiveButton.setBackgroundDrawable(getResources()
						.getDrawable(R.drawable.btn_default_holo_dark));
				
				Button negativeButton = ((AlertDialog) dialog)
						.getButton(AlertDialog.BUTTON_NEGATIVE);
				positiveButton.setBackgroundDrawable(getResources()
						.getDrawable(R.drawable.btn_default_holo_dark));
			}
		});
		
		alertDialog.show(); 

only in this order, call alertDialog.setOnShowListener after create()

Solution 3 - Android

Thanks wieux. But for new developers understanding purpose I am re-writing code below

AlertDialog dialog= new AlertDialog.Builder(this).create();             
dialog.setIcon(R.drawable.alert_icon);             
dialog.setTitle("title");            
dialog.setMessage("Message");             
dialog.setButton(AlertDialog.BUTTON_POSITIVE,"Yes", new DialogInterface.OnClickListener() {                 
    @Override                 
    public void onClick(DialogInterface arg0, int arg1) {                                                
    }             
}); 
dialog.show(); 
Button yesButton = dialog.getButton(AlertDialog.BUTTON_POSITIVE);             
Log.w("Button",""+yesButton); //here getting null             
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, 1f);             
yesButton.setLayoutParams(layoutParams);        

Solution 4 - Android

Here is useful Kotlin extensions functions to achieve what you want with an automatic solution :

import android.content.DialogInterface.BUTTON_NEGATIVE
import android.content.DialogInterface.BUTTON_POSITIVE
import androidx.appcompat.app.AlertDialog

fun AlertDialog.onPositiveButtonClick(action: () -> Unit) {
    onButtonClick(BUTTON_POSITIVE, action)
}

fun AlertDialog.onNegativeButtonClick(action: () -> Unit) {
    onButtonClick(BUTTON_NEGATIVE, action)
}

fun AlertDialog.onButtonClick(whichButton: Int, action: () -> Unit) {
    fun onButtonClick() = getButton(whichButton).setOnClickListener { action() }

    if (isShowing) {
        onButtonClick()
    } else {
        setOnShowListener {
            onButtonClick()
        }
    }
}

How to use :

alertDialog.onPositiveButtonClick { /* DO WHAT YOU WANT HERE */ }

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionADITView Question on Stackoverflow
Solution 1 - AndroidvieuxView Answer on Stackoverflow
Solution 2 - AndroidMickey TinView Answer on Stackoverflow
Solution 3 - AndroidADITView Answer on Stackoverflow
Solution 4 - AndroidDamienLView Answer on Stackoverflow