Hi everyone i have a alert box with two text box's, and here the problem is the alert dialog was disappearing when user clicks outside of that pop up or the Alert dialog is disappearing when user clicks Ok button too.
So please help me in this regards
Thanks in advance...
final AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Sign In Failed");
final EditText input1=new EditText(MainActivity.this);
final EditText input2=new EditText(MainActivity.this);
input1.setHint("eNTER name1");
input2.setHint("Enter Name2");
LinearLayout linearLayout= new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
builder.setMessage("Invalid username or password");
linearLayout.addView(input1);
linearLayout.addView(input2);
builder.setView(linearLayout);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.show();
because by default it is Cancelable
Add this after builder.setView(linearLayout) -
builder.setCancelable(false);
UPDATE
As per your code snippet below-
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
on positive button ("OK") click, you are setting dialog.cancel() Don't do this, you should set some action as you required on positive button click.
See This :
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel(); // close the current dialog
}
});
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//Perform any Intent Action or perform validation as you want
}
});
UPDATE 2
Just copy & paste below code - working perfectly
final EditText input1 = new EditText(MainActivity.this);
final EditText input2 = new EditText(MainActivity.this);
input1.setHint("Enter name1");
input2.setHint("Enter Name2");
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.addView(input1);
linearLayout.addView(input2);
final AlertDialog builder = new AlertDialog.Builder(MainActivity.this)
.setTitle("Sign In Failed")
.setCancelable(false)
.setMessage("Invalid username or password").setView(linearLayout).setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
}).create();
builder.show();
builder.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (input1.length() <= 0) {
Toast.makeText(MainActivity.this, "Please Enter Name", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "OK", Toast.LENGTH_SHORT).show();
builder.dismiss();
}
}
});
You need to set outside touch false try this:
setCanceledOnTouchOutside(false);
Simply set cancelable false:
.setCancelable(false)
Related
I want my alert dialog box to trigger and stay showing an error message if the user leaves the name field empty and clicks ok but my dialogue box disappears even if the user does not fill anything and click ok.Here is my code. Please suggest me the corrections I need to make.
save2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final dbmanager db= new dbmanager(cgpa3.this);
final AlertDialog.Builder alert = new AlertDialog.Builder(cgpa3.this);
// alert.setTitle("Enter a name");
alert.setMessage("Enter student Name");
// Set an EditText view to get user input
final EditText input = new EditText(cgpa3.this);
alert.setView(input);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String value = input.getText().toString();
if(value.isEmpty()){
Animation shake = AnimationUtils.loadAnimation(cgpa3.this, R.anim.shake);
input.startAnimation(shake);
input.setError("Please enter student name");
}
else
{db.addRecord1(value,textView39.getText(),textView40.getText(),no_of_sem);
}
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Canceled.
}
});
alert.show();
};
});
use this one,
final EditText editText;
final AlertDialog.Builder alert = new AlertDialog.Builder(DemoActivity.this);
alert.setTitle("Enter a name");
alert.setMessage("Enter student Name");
alert.setCancelable(false);
editText = new EditText(DemoActivity.this);
alert.setView(editText);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
});
final AlertDialog dialogs = alert.create();
dialogs.show();
dialogs.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String value = editText.getText().toString();
if (value.isEmpty()) {
editText.setError("Please enter student name");
}
else{
dialogs.dismiss();
}
}
});
dialogs.getButton(AlertDialog.BUTTON_NEGATIVE).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
dialogs.dismiss();
}
});
if(TextUtils.isEmpty(input.getText().toString().trim())){
Animation shake = AnimationUtils.loadAnimation(cgpa3.this, R.anim.shake);
input.startAnimation(shake);
input.setError("Please enter student name");
}
else{
db.addRecord1(value,textView39.getText(),textView40.getText(),no_of_sem);
}
if working plese aprove
i'm trying to make an EditText field that onClick opens a numberPicker so the user can input a number.
this is my code:
tipET.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
final NumberPicker numberPicker = new NumberPicker(getContext());
numberPicker.setMaxValue(1000);
numberPicker.setMinValue(0);
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setView(numberPicker);
builder.setTitle("tip");
builder.setPositiveButton("ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
Log.d(TAG, "TIP NUMBER IS:" + numberPicker.getValue());
num = numberPicker.getValue();
}
});
builder.setNegativeButton("cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.dismiss();
}
});
builder.create();
builder.show();
}
});
tipET.setText(String.valueOf(num));
for some reason tipET text stays the same after i input another number, why is that?
also for the first time i click on the EditText the keyboard pops up, and only on the second click my NumberPicker opens, how can i prevent that?
Add tipET.setText(String.valueOf(num)); to the onClick method for the positive button, right below num = numberPicker.getValue();, like this:
tipET.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
final NumberPicker numberPicker = new NumberPicker(getContext());
numberPicker.setMaxValue(1000);
numberPicker.setMinValue(0);
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setView(numberPicker);
builder.setTitle("tip");
builder.setPositiveButton("ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
Log.d(TAG, "TIP NUMBER IS:" + numberPicker.getValue());
num = numberPicker.getValue();
// update the text of the EditText here
tipET.setText(String.valueOf(num));
}
});
builder.setNegativeButton("cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.dismiss();
}
});
builder.create();
builder.show();
}
});
So without having to create a custom dialog as I currently have a number of dialog layouts and don't want to have to do it for each one, is there a way to prevent this dialog from closing when the positive button is pressed and the EditText is empty?
Currently it closes the dialog every time I hit enter and there is nothing in the EditText field.
public AlertDialog webpageDialog() {
AlertDialog.Builder webpageDialogBuilder = new AlertDialog.Builder(context);
LayoutInflater inflater = LayoutInflater.from(context);
webpageDialogBuilder.setView(inflater.inflate(R.layout.dialog_webpage, null))
.setNegativeButton(context.getString(R.string.cancel), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
final AlertDialog webpageDialog = webpageDialogBuilder.create();
webpageDialog.setButton(DialogInterface.BUTTON_POSITIVE, context.getString(R.string.enter), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
EditText webpageInput = (EditText) webpageDialog.findViewById(R.id.dw_et_webpage_address);
Log.d(TAG, "Positive on click");
if(TextUtils.isEmpty(webpageInput.getText().toString())){
Log.d(TAG, "Edit text empty");
webpageInput.setError(context.getString(R.string.error_web_required));
} else {
Log.d(TAG, "Edit text not empty");
ms.setUriString("http://" + webpageInput.getText().toString());
ms.returnWithResult(1);
dialog.cancel();
}
Log.d(TAG, "Returning");
}
});
Log.d(TAG, "Returning dialog");
return webpageDialog;
}
this is how I'm doing it.
AlertDialog.Builder builder = new AlertDialog.Builder(this);
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
EditText yourEditText = new EditText(this);
layout.addView(yourEditText);
builder.setView(layout);
// Set up the buttons
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
final AlertDialog dialog = builder.create();
dialog.show();
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if ("".equals(yourEditText.getText().toString().trim())) {
//this will stop your dialog from closing
yourEditText.setError("This field is required!");
return;
}
//you logic here
dialog.dismiss();
}
});
I hope following line can help,
webpageDialogBuilder.setCancelable(false);
I have an android app that has two buttons. So basically what I am trying to do is when a user clicks the 'Share Button' on the page, an alert dialog box will appear that will prompt the user to enter their email address. Here is what I have so far. I've tried an EditView but it hasn't been working well for me.
Thanks in advance!
package com.colors;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
final Context context = this;
private Button button;
private Button shareButton;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
shareButton = (Button) findViewById(R.id.shareButton);
// add button listener for Welcome Message.
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
context);
// set the title of the Alert Dialog
alertDialogBuilder.setTitle("Welcome!");
// set dialog message
alertDialogBuilder
.setMessage("Program Description here...")
.setCancelable(false)
.setNegativeButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
// if no is clicked, just close
// the dialog box and do nothing
dialog.cancel();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
});
// Button listener for Share Button
shareButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
context);
// set the title of the Alert Dialog
alertDialogBuilder.setTitle("Share");
// set dialog message
alertDialogBuilder
.setMessage("Would like to a user input here.")
.setCancelable(false)
.setPositiveButton("Send!",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
// if no is clicked, just close
// the dialog box and do nothing
dialog.cancel();
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
// if no is clicked, just close
// the dialog box and do nothing
dialog.cancel();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
});
}
}
This may helpful to you.
shareButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
AlertDialog.Builder al1 = new Builder(
AgriListView.this);
al1.setMessage("Share Something");
al1.setPositiveButton("Share",
new DialogInterface.OnClickListener() {
public void onClick(
DialogInterface dialog,
int which) {
final EditText input = new EditText(
AgriListView.this);
input.setSingleLine();
AlertDialog.Builder al = new Builder(
AgriListView.this);
al.setTitle("Enter New Value");
al.setView(input);
al.setCancelable(true);
al.setIcon(R.drawable.bt);
al.setPositiveButton(
"OK",
new DialogInterface.OnClickListener() {
public void onClick(
DialogInterface dialog,
int which) {
int len = input
.length();
if (!(len == 0)) {
Toast.makeText(
AgriListView.this,
"Entered text is: "+input.getText()
,
Toast.LENGTH_SHORT)
.show();
} else {
Toast.makeText(
getApplicationContext(),
"Enter Value Properly",
Toast.LENGTH_LONG)
.show();
}
}
});
al.setNegativeButton(
"Cancel",
new DialogInterface.OnClickListener() {
public void onClick(
DialogInterface dialog,
int which) {
dialog.cancel();
}
});
AlertDialog alert = al.create();
alert.show();
}
});
al1.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(
DialogInterface dialog,
int which) {
dialog.cancel();
}
});
AlertDialog alert1 = al1.create();
alert1.show();
}
});
This code create dialog with Edittext value.
You could do something like this.
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Title");
alert.setMessage("Message");
// Set an EditText view to get user input
final EditText input = new EditText(this);
alert.setView(input);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String value = input.getText();
// Do something with value!
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Canceled.
}
});
alert.show();
I want to add dialog box in my app to let the user put his/her desire Ip address.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Please put Ip address")
------> here the user can type on the dialog in String
.setNeutralButton("Add", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
printer = new PrinterObject("134.188.204.155");--->the result text from dialog
....
}
});
AlertDialog alert = builder.create();
alert.show();
Anybody knows how to add it?
Try this...
AlertDialog.Builder builder = new Builder(this);
builder.setTitle("Please put Ip address");
builder.setMessage("");
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.alert, null);
final EditText ipfield = (EditText) view.findViewById(R.id.ipfield);
builder.setView(view);
builder.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int arg1) {
// do what you need
}
});
builder.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int arg1) {
dialog.dismiss();
}
});
AlertDialog dialog = builder.create();
dialog.show();
You can use this snippet:
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Please put Ip address");
// Set an EditText view to get user input
final EditText input = new EditText(this);
alert.setView(input);
alert.setPositiveButton("Add", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String value = input.getText();
// Do something with value!
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Canceled.
}
});
alert.show();