Output string variable value from EditText into a message box - java

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();
}

Related

Can I set a Button already in my xml to be the positive or negative Button in a Dialog?

I'd rather not use Alert Dialog, but I will if I can set the positive Button to be the button I already have. If I can't do that, is there a way to set positive and negative Buttons in a custom dialog?
You can use the following custom alert dialog.
public class CustomAlertDialog {
public void showDialog(Context activity, String msg, String buttonText, final CustomDialogListener customDialogListener){ //one button with callback
final Dialog dialog = new Dialog(activity);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCancelable(false);
dialog.setContentView(R.layout.custom_alert);
TextView text = (TextView) dialog.findViewById(R.id.text_alertdialog);
text.setText(msg);
Button dialogButton = (Button) dialog.findViewById(R.id.btn_alert_dialog);
dialogButton.setText(buttonText);
dialogButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
customDialogListener.onPositiveButtonClick();
}
});
dialog.show();
}
public void showDialog(Context activity, String msg, String positiveButtonText, String negativeButtonText, final CustomDialogListener customDialogListener){//two button with callback
final Dialog dialog = new Dialog(activity);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCancelable(false);
dialog.setContentView(R.layout.custom_alert_two_button);
TextView text = (TextView) dialog.findViewById(R.id.text_alert_two_dialog);
text.setText(msg);
Button positiveDialogButton = (Button) dialog.findViewById(R.id.btn_alert_two_dialog_YES);
Button negativeDialogButton = (Button) dialog.findViewById(R.id.btn_alert_two_dialog_NO);
positiveDialogButton.setText(positiveButtonText);
negativeDialogButton.setText(negativeButtonText);
positiveDialogButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
customDialogListener.onPositiveButtonClick();
}
});
negativeDialogButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
customDialogListener.onNegativeButtonClick();
}
});
dialog.show();
}
public void showDialog(Context activity, String msg, String buttonText){ //simple alert without callback
final Dialog dialog = new Dialog(activity);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCancelable(false);
dialog.setContentView(R.layout.custom_alert);
TextView text = (TextView) dialog.findViewById(R.id.text_alertdialog);
text.setText(msg);
Button dialogButton = (Button) dialog.findViewById(R.id.btn_alert_dialog);
dialogButton.setText(buttonText);
dialogButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
}
The custom_alert.xml contains one button and one textView to display the message.
The custom_alert_two_button.xml contains two buttons and one textView to display the message.
Last one contains only one textview to display message.

How to get out of an event handler from a alert dialog?

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();
}
}
});

Android integer conversion from EditText failed

I'm trying to set a value in a Handler from a Dialog edittext but i get an error. The code is this:
private Button btnstart;
private Button btnstop;
TimePicker myTimePicker;
final static int RQS_1 = 1;
TimePickerDialog timePickerDialog;
Context context;
private int n = 0;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnstart = (Button)findViewById(R.id.btnstart);
btnstart.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
LayoutInflater l = LayoutInflater.from(MainActivity.this);
View dview = l.inflate(R.layout.dialog, null);
AlertDialog.Builder ad = new AlertDialog.Builder(MainActivity.this);
ad.setView(dview);
final EditText edit = (EditText) dview.findViewById(R.id.edit);
String e = edit.getText().toString();
n = Integer.parseInt(e);
ad.setCancelable(false)
.setTitle("Imposta i Secondi")
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id){
Handler handler = new Handler();
handler.postDelayed(new Runnable(){
#Override
public void run(){
Toast.makeText(getApplicationContext(), "Start",Toast.LENGTH_LONG).show();
}
}, n);
}
})
.setNegativeButton("Cancel",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id){
dialog.cancel();
}
});
AlertDialog alertD = ad.create();
alertD.show();
}
});
the n value, usually could be 3000,4000,5000 or any other value in ms as integer value. So i converted the edit value of edittext in integer but i get an error in the logcat:
Invalid int "" at line n = Integer.parseInt(e); what's wrong here?
You need to move these two lines of code from where they are now to inside the onClick handler of the DialogInterface.OnClickListener.
String e = edit.getText().toString();
n = Integer.parseInt(e);
revised code
private Button btnstart;
private Button btnstop;
TimePicker myTimePicker;
final static int RQS_1 = 1;
TimePickerDialog timePickerDialog;
Context context;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnstart = (Button)findViewById(R.id.btnstart);
btnstart.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
LayoutInflater l = LayoutInflater.from(MainActivity.this);
View dview = l.inflate(R.layout.dialog, null);
AlertDialog.Builder ad = new AlertDialog.Builder(MainActivity.this);
ad.setView(dview);
final EditText edit = (EditText) dview.findViewById(R.id.edit);
ad.setCancelable(false)
.setTitle("Imposta i Secondi")
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id){
String e = edit.getText().toString();
int n = Integer.parseInt(e);
Handler handler = new Handler();
handler.postDelayed(new Runnable(){
#Override
public void run(){
Toast.makeText(getApplicationContext(), "Start",Toast.LENGTH_LONG).show();
}
}, n);
}
})
.setNegativeButton("Cancel",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id){
dialog.cancel();
}
});
AlertDialog alertD = ad.create();
alertD.show();
}
});
At what point does your EditText get populated? This is in a create event so it appears that you are trying to extract an integer from an empty string?
int value;
try{
value = Integer.parseInt(stringValue);
}(Execption e){
value = 0; // If edittext contains letters, space, etc
}
First , you should use try catch for formatting numbers.
String e = edit.getText().toString().trim();
if(e!=null && !e.isEmpty())
{
int n ;
try {
n = Integer.valueOf(e);
} catch (NumberFormatException e) {
e.printStackTrace();
n = 0;
}
}
Hope it helps.

Android - Can't get value from EditText inside Custom Dialog

I have been unsuccessful in getting the input from my EditText object inside my custom dialog.
public class SetCityDialog extends DialogFragment {
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LayoutInflater factory = LayoutInflater.from(MainActivity.this);
final View view = factory.inflate(R.layout.city_dialog, null);
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// Get the layout inflater
LayoutInflater inflater = getActivity().getLayoutInflater();
builder.setView(inflater.inflate(R.layout.city_dialog, null))
// Add action buttons
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog,
int id) {
//This is the input I can't get text from
EditText inputTemp = (EditText) view.findViewById(R.id.search_input_text);
//query is of the String type
query = inputTemp.getText().toString();
newQuery();
getJSON newData = new getJSON();
newData.execute("Test");
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
SetCityDialog.this.getDialog().cancel();
}
});
return builder.create();
}
}
I don't get any exceptions, but the variable query is set to an empty string.
Any help would be fantastic.
I was trying to do the same thing and i get the same error. I don't no why. I already use AlertDialog.Builder in the past and get no trouble. But in your case change this code:
public void onClick(DialogInterface dialog,
int id) {
//This is the input I can't get text from
EditText inputTemp = (EditText) view.findViewById(R.id.search_input_text);
//query is of the String type
query = inputTemp.getText().toString();
newQuery();
getJSON newData = new getJSON();
newData.execute("Test");
}
By this one:
public void onClick(DialogInterface dialog,
int id) {
Dialog f = (Dialog) dialog;
//This is the input I can't get text from
EditText inputTemp = (EditText) f.findViewById(R.id.search_input_text);
query = inputTemp.getText().toString();
...
}
This solution works for me and it seems to be the same for you.
Found on stackoverflow
Use this instead :
View myLayout = nflater.inflate(R.layout.city_dialog, null);
EditText myEditText = (EditText) myLayout.findViewById(R.id.myEditText);
String valueOfEditText = myEditText.getText().toString();
No need to do that much coding. just change
builder.setView(inflater.inflate(R.layout.city_dialog, null))
to
builder.setView(view)
and access text of EditText using **view.findView.....
EditText inputTemp = (EditText) view.findViewById(R.id.search_input_text);
String xyz = inputTemp.getText().toString();
This is worked for me:
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
try {
Dialog inDialog = (Dialog) dialog;
EditText emailAddress = (EditText) inDialog.findViewById(R.id.emailAddress);
email = emailAddress.getText().toString();
if(email.length() == 0) {
objPublicDelegate.showToast("Please fill Email Address.");
}else{
objLoadingDialog.show("Please wait...");
// Call a network thread Async task
mNetworkMaster.runForgetAsync(email);
}
} catch (Exception e) {
e.printStackTrace();
}
}
});

How to show user entered EditText value in a android dialogue box

my modified sample of my work basically below i have 2 editboxes i want the values of the both to be shown in an alertbox, rest of the code works fine, the alertbox does show up but without the entered values.
public class Main extends Activity {
EditText username = null;
EditText password = null;
Button login;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
EditText usern = (EditText) findViewById(R.id.username);
EditText pass = (EditText) findViewById(R.id.password);
Button login = (Button) findViewById(R.id.login);
String usernm = usern.getText().toString();
String passnm = pass.getText().toString();
final Builder alert = new AlertDialog.Builder(this)
.setTitle("SHOW FIELDS")
.setMessage("USERNAME:" + usernm + " PASSWORD:" + passnm)
.setNegativeButton("CLOSE", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// do nothing
closeContextMenu();
}
});
login.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
alert.show();
}
});
you will need to get EditText's value on button so show in AlertDialog as :
login.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
usernm = usern.getText().toString();
passnm = pass.getText().toString();
alert.show();
}
and declare both usernm and passnm at class level
EDIT :
or best way create a method to show alert and call it on button click as:
public void showalert(){
usernm = usern.getText().toString();
passnm = pass.getText().toString();
alert = new AlertDialog.Builder(Main.this)
.setTitle("SHOW FIELDS")
.setMessage("USERNAME:" + usernm + " PASSWORD:" + passnm)
.setNegativeButton("CLOSE", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// do nothing
closeContextMenu();
}
});
alert.show();
}
and call this method on button click as:
login.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
showalert();
}:
});
and declare all variables at class level by which you can access it in whole class as :
public class Main extends Activity {
EditText usern ;
EditText pass ;
Button login;
String usernm ,passnm; //<<< Declare here
Builder alert;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
usern = (EditText) findViewById(R.id.username);
pass = (EditText) findViewById(R.id.password);
login = (Button) findViewById(R.id.login);
login.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
showalert();
}
});
}

Categories