So there is an alert dialog that checks if a value exist in the preferences and I can't get out of the main onClickListener from the negative button.
Button b = (Button) findViewById(R.id.button4);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
EditText et = (EditText) findViewById(R.id.editText2);
EditText et2 = (EditText) findViewById(R.id.editText3);
String code = et.getText().toString();
String name = et2.getText().toString();
if (prefs.contains(code)) {
AlertDialog.Builder builder = new AlertDialog.Builder(Definitions.this);
builder.setTitle(R.string.error);
builder.setCancelable(false);
builder.setMessage(R.string.value_exist);
builder.setPositiveButton(R.string.positive, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.setNegativeButton(R.string.negative, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//how to get out of the main onClickListener from here?
}
});
AlertDialog alert = builder.create();
alert.show();
}
editor.putString(code, name);
editor.apply();
}
});
The onClick method here :
builder.setNegativeButton(R.string.negative, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//how to get out of the main onClickListener from here?
}
});
is not executed during your main listener execution, but only when you click on the negative button.
The last 2 lines :
editor.putString(code, name);
editor.apply();
are already executed when your alert become visible.
What you can probably do to fix your problem is this :
Button b = (Button) findViewById(R.id.button4);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
...
if (prefs.contains(code)) {
...
AlertDialog alert = builder.create();
alert.show();
}else{
editor.putString(code, name);
editor.apply();
}
}
});
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();
}
});
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 am trying to input a number into a EditText, then output it in a message/AlertDialog after click a button. So far I have coded up what I think should work to output it, but for some reason, it doesn't. At the moment, the only output I recieve from the message box, is the text that I specified: "saved". There is no value from the variable being displayed.
Hopefully someone will be able to see what I am doing wrong and find a solution.
Thanks
Code below:
Button saveBtn1 = (Button) findViewById(R.id.btnSave1);
saveBtn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
EditText inputTxt1 = (EditText) findViewById(R.id.yourPhoneNum);
String phoneNum1 = inputTxt1.getText().toString();
savenum1(phoneNum1);
}
});
public void savenum1(String phoneNum1) {
AlertDialog.Builder dlgAlert = new AlertDialog.Builder(this);
dlgAlert.setMessage("Saved" + phoneNum1);
//dlgAlert.setPositiveButton("OK", null);
dlgAlert.setCancelable(true);
dlgAlert.create().show();
dlgAlert.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//dismiss the dialog
}
});
dlgAlert.create().show();
}
Create a private field EditText inputTxt1;
Before Button saveBtn1 = (Button) findViewById(R.id.btnSave1);
get the EditText id: inputTxt1 = (EditText) findViewById(R.id.yourPhoneNum);
At saveBtn1.setOnClickListener
get the phoneNum1: String phoneNum1 = inputTxt1.getText().toString();
Call savenum1 with phoneNum1: savenum1(phoneNum1);
Remove from savenum1:
EditText inputTxt1 = (EditText) findViewById(R.id.yourPhoneNum);
String phoneNum1 = (String) inputTxt1.getText().toString();
Code after correction:
public class SettingsScreen extends Activity {
private EditText inputTxt1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.screen_settings);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
EditText inputTxt1 = (EditText) findViewById(R.id.yourPhoneNum);
Button saveBtn1 = (Button) findViewById(R.id.btnSave1);
saveBtn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String phoneNum1 = inputTxt1.getText().toString();
savenum1(phoneNum1);
}
});
}
public void savenum1(String phoneNum1) {
AlertDialog.Builder dlgAlert = new AlertDialog.Builder(this);
dlgAlert.setMessage("Saved" + phoneNum1);
//dlgAlert.setPositiveButton("OK", null);
dlgAlert.setCancelable(true);
dlgAlert.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//dismiss the dialog
}
});
dlgAlert.create().show();
}
I need some help to run an AlertDialog from my application. It won't compile while there are some errors. Especially at the line alertDialog.setItems(items, new DialogInterface.OnClickListener() with the setItems().
Please take a look.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_nastavenie_casu);
// Show the Up button in the action bar.
getActionBar().setDisplayHomeAsUpEnabled(true);
final CharSequence[] items = {"30 minút", "45 minút", "60 minút", "75 minút", "90 minút"};
Button tlacidlo = (Button) findViewById(R.id.spusti);
EditText pripomienka = (EditText) findViewById(R.id.upozornit_za);
pripomienka.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
// Intent myIntent = new Intent(view.getContext(), agones.class);
// startActivityForResult(myIntent, 0);
AlertDialog alertDialog = new AlertDialog.Builder(NastavenieCasu.this).create(); //Read Update
alertDialog.setTitle("hi");
alertDialog.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// do something
}
alertDialog.show();
});
}
}
}
setItems() is part of the AlertDialog.Builder class, not AlertDialog. Try:
AlertDialog.Builder builder = new AlertDialog.Builder(NastavenieCasu.this);
builder.setTitle("hi");
builder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//
// do something
}
AlertDialog alertDialog = builder.create();
alertDialog.show();