Android how to make ok button on dialog not all caps - java

For an alert dialog in Android, how do you make the positive button not have all capital letters. The text is "OK" instead of "Ok".

The accepted solution above won't work in Lollipop and above. Here's the working solution.
After showing the dialog, I'm setting the button all caps false. Make sure you do it after dialog.show(). Else, you'll get Null Pointer Exception.
AlertDialog.Builder builder = new AlertDialog.Builder(MyActivity.this);
builder.setTitle("Title");
builder.setMessage("message");
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//Do Something
}
});
AlertDialog dialog = builder.create();
dialog.show();
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setAllCaps(false);

Use the DialogInterface.BUTTON_POSITIVE or DialogInterface.BUTTON_NEGATIVE to customize the action buttons.
val builder = MaterialAlertDialogBuilder(requireContext())
builder.setTitle(getString(R.string.alert_title))
builder.setMessage(getString(R.string.alert_msg))
builder.setPositiveButton(getString(R.string.action_yes)) { _, _ ->
// todo: your action
}
builder.setNegativeButton(getString(R.string.action_no), null)
val dialog = builder.create()
dialog.show()
dialog.getButton(DialogInterface.BUTTON_POSITIVE).isAllCaps = false
dialog.getButton(DialogInterface.BUTTON_NEGATIVE).isAllCaps = false

You can set it to be anything you want - Eg.:
AlertDialog.Builder builder = new AlertDialog.Builder(MyActivity.this);
builder.setTitle("Title");
builder.setMessage("message");
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
return;
}
});
AlertDialog dialog = builder.create();
dialog.show();
Reference: http://developer.android.com/reference/android/app/AlertDialog.Builder.html#setPositiveButton(int, android.content.DialogInterface.OnClickListener)

Or even :
builder.setPositiveButton("Ok", null);
For the basic usage.

using androidx.appcompat.app.AalertDialog fixed for me.

Related

Programmatically accessing AlertDialog PositiveButton

I am using an AlertDialog to ask the user to enter a numerical value on a long press of a view. This uses the Android soft-keyboard. For a better user experience I would like the keyboard "Enter" button to programmatically click the Alert Dialog's positive button and run it's onClick. This is really awkward because I can't find any reference to the positive button in the dialog objects. Code to illustrate:
customStakeView.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View view) {
final AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Custom Stake");
customStakeSet = false;
// Set up the input
final EditText input = new EditText(context);
input.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
// Specify the type of input expected;
input.setInputType(InputType.TYPE_CLASS_NUMBER);
input.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View view, int keyCode, KeyEvent keyEvent) {
if(keyEvent.getAction() == KeyEvent.ACTION_DOWN){
if(!input.getText().toString().equals("")){
switch (keyCode){
case KeyEvent.KEYCODE_ENTER:
//Positive Button Outcome
}
}
}
return false;
}
});
builder.setView(input);
// Set up the buttons
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
String newStake = input.getText().toString();
if (!newStake.equals("")) {
newStake = newStake.replaceAll("[^\\d.]", ""); //strip down to currency format
customStake = new Stake(newStake);
customStakeSet = true;
deselectAll();
selectCustomStake();
InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(input.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.show();
return true;
}
});
I've caught the KeyEvent, and if this was a button added via XML, or even defined with a variable, I would easily be able to do
button.performClick();
but AlertDialog doesn't seem to have such a reference
EDIT:
From documentation, use getButton(whichButton)
Gets one of the buttons used in the dialog. Returns null if the specified button does not exist or the dialog has not yet been fully created (for example, via show() or create()).
whichButton can be BUTTON_POSITIVE or any other button you have specified.
Below is a screenshot of it.
You aren't catching AlertDialog returned by .create() method. getButton() is not available to builder, but to AlertDialog object.
builder.setPositiveButton(...);
// you're missing this
final AlertDialog alertDialog = builder.create();
// then, use it like this
alertDialog.getButton(DialogInterface.Button_POSITIVE).performClick();

How do I detect when user closes ussd dialog in Android?

I'm running ussd code from my code. After it returns the results the user will press the ok button to close the the dialog. I want to detect when the user does this.
Let's assume that this is your dialog code:
AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.AppCompatAlertDialogStyle);
builder.setTitle("dialog's title");
builder.setMessage("dialogs's text");
builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//do stuff when user presses ok button
}
});
builder.setNeutralButton("CANCEL", null); //same here just add the listener
AlertDialog dialog = builder.create();
dialog.show();
//you can use neutralButton as well

Change the background of the AlertDialog

i want to know how to change the design of my AlertDialog, the buttons the background.
thank you.
new AlertDialog.Builder(this)
.setTitle("Exit")
.setMessage("Do you want to exit")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
finish();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// do nothing
}
})
.setIcon(android.R.drawable.ic_menu_zoom)
.show();
}
AlertDialog.Builder alertDialog_ImageSelector=new AlertDialog.Builder(mcontext);
View imageGuideLineInfo=getActivity().getLayoutInflater().inflate(R.layout.your_alyout,null);
alertDialog_ImageSelector.setView(imageGuideLineInfo);
TextView tv_guidelines=(TextView)imageGuideLineInfo.findViewById(R.id.tv_guidelines);
final AlertDialog adinfo=alertDialog_ImageSelector.create();
adinfo.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));//here you can change the background of alertDialog.
adinfo.show();
if you want to change the background of buttons then you need to create the buttons in your_view.xml and bind them here and setBackground.
use this android inbuilt theme
AlertDialog.Builder builder = new AlertDialog.Builder(this, AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);

Alert dialog not working correctly - android

I am using an alert dialog to ask the user for confirmation of an item addition to an array. I am printing the array size before and after the alert dialog and it seems that both printings are done before the alert dialog shows up.
Log.d("before", ""+wayPoints.length);
AlertDialog.Builder ab = new AlertDialog.Builder(getContext());
ab .setTitle("Add entry");
ab .setMessage("Are you sure you want to add this entry?");
ab .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{wayPoints = ArrayHandler.addAtIndex(wayPoints, node, 1);}
});
ab .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// do nothing
}
});
ab.setIcon(android.R.drawable.ic_dialog_alert);
ab.show();
Log.d("after", ""+wayPoints.length);
The ArrayHandler.addAtIndex method handles the addition of a new item to the provided array.
The problem is it is printing both logging lines before the alert dialog shows up. I need the alert dialog to be done before the printing of the second log.
This is correct behavior. The show method does not wait or block until the dialog is dismissed. Code execution continues after the show method immediately after the dialog is shown. (Actually, the dialog isn't even displayed yet - that happens after you yield control).
You already have setPositiveButton and setNegativeButton handlers, just put the code you want to execute in those handlers when user presses a button.
If you want certain code to run when the dialog is closed regardless of which button is tapped, use setOnDismissListener and put your code there.
Use:
AlertDialog alert1 = ab.create();
alert.show();
Instead of:
ab.show();
Try this
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder( context);  
// set title
alertDialogBuilder.setTitle("Your Title");
  // set dialog message
alertDialogBuilder .setMessage("Click yes to exit!") .setCancelable(false) .setPositiveButton("Yes",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, close
// current activity
MainActivity.this.finish(); }
}) .setNegativeButton("No",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, just close
// the dialog box and do nothing
dialog.cancel();
}
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
wayPoints = ArrayHandler.addAtIndex(wayPoints, node, 1);
Log.d("after", ""+wayPoints.length);
} });
});  
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();  
// show it
alertDialog.show();

How to disable positive and negative button if the checkbox is unchecked in java?

I'm trying to do alert message by disable ok and cancel button if the checkbox is unchecked.
reconfirm.java:
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(reconfirm.this);
LayoutInflater layoutInflater
= (LayoutInflater)getBaseContext()
.getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.popup, null);
alertDialogBuilder.setView(popupView);
CheckBox check= (CheckBox)findViewById(R.id.checkBox1);
if (check.isChecked() ) {
AlertDialog dialog = null;
((AlertDialog)dialog).getButton(DialogInterface.BUTTON_POSITIVE).setVisibility(View.INVISIBLE);
}
alertDialogBuilder.setPositiveButton("OK",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent intObj = new Intent(getApplicationContext(),
agree.class);
startActivity(intObj);
}
});
alertDialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent intObj = new Intent(getApplicationContext(),
IntentExampleActivity.class);
startActivity(intObj);
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
This is answered already here--How to disable / enable dialog negative positive buttons?
after dialog.show use below code
if(your_condition_true)
dialog.getButton(AlertDialog.BUTTON1).setEnabled(false); //BUTTON1 is positive button
Try Using setEnabled() and setClickable method for buttons.
here is the doc
Try following code snippets. Hope you get some idea from this :)
if (check.isChecked()) {
alertDialogBuilder.getButton(Dialog.BUTTON_POSITIVE).setEnabled(false);
alertDialogBuilder.getButton(Dialog.BUTTON_NEGATIVE).setEnabled(false);
}
where dialog is object of AlertDialog.

Categories

Resources