Currently struggling to handle the user input from my AlertDialog box. Everything works fine, but I don't know what to do with the user input in order to save it and access it within another function.
private void promptUser() {
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
final EditText promptTitle = new EditText(this);
promptTitle.setHint("Title");
layout.addView(promptTitle);
userPrompt = new AlertDialog.Builder(this);
userPrompt.setView(layout);
userPrompt.setTitle("Enter map information").setView(layout);
userPrompt.setPositiveButton("Create", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
Log.i("AlertDialog", "Create button was hit");
Log.i("AlertDialog", "This is the text that was entered:" + promptTitle.getText().toString());
userInputs = promptTitle.getText().toString();
}
});
userPrompt.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
Log.i("AlertDialog", "Cancel button was hit");
}
});
userPrompt.show();
}
and where I call the function:
public void onMapClick( LatLng point ) {
promptUser()
//Log.d(userInputs.get(0), "COOL");
}
The main error I'm encountering is that after calling my method promptUser(), everything runs but the next several lines of code don't wait for the user to click the PositiveButton. e.g, my Log shows "User inputs: null", since tempString isn't yet available. How can I make my function wait until the user has hit submit before running the next line of code?
Put the call to onMapClick inside the positive button callback.
userPrompt.setPositiveButton("Create", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
userInputs = promptTitle.getText().toString();
onMapClick();
}
});
It's a bit hard to tell if this is exactly what you want since you haven't provided enough code, but this is what you asked for.
Related
Ok, so i need to display some of my product's database data on the scanner builder window(as shown in the image) when this product gets scanned. Scanner window results
As you can see in the image, the barcode scanner is displayed, my question is: is it possible to print in this windows some other data except from the product's barcode? if yes how should i move?
this is my windows builder code
public void handleResult(Result result) {
//myResult= barcode text
myResult = result.getText();
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Scan Result");
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
scannerView.resumeCameraPreview(addquantityactivity.this);
updateData();
}
});
builder.setNeutralButton("Visit(if url)", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(myResult));
startActivity(browserIntent);
}
});
builder.setMessage(result.getText());
AlertDialog alert1 = builder.create();
alert1.show();
}
Do you see where it says builder.setMessage(result.getText()); <--?
This is where you'd set the result. At the moment you're just putting in the result text which is the number. If you want something more, there is where you would put it.
EDIT:
I'm terrible at writing answers. The method you are using is defined here. It takes in a Character Sequence and will display it. Or it can take in an Int and show that.
EDIT2:
As an example of concatenating the barcode with some other information I would do this:
String myMessage = result.getText() + " and some other amazing information about this barcode";
builder.setMessage(myMessage);
I want to build a function that creates an AlertDialog and returns the string that the user entered, this the function I have for creating the dialog, how do I return the value?
String m_Text = "";
private String openDialog(String title) {
AlertDialog.Builder builder = new AlertDialog.Builder(view.getContext());
builder.setTitle(title);
final EditText input = new EditText(view.getContext());
input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_NORMAL);
builder.setView(input);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
m_Text = input.getText().toString();
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.show();
// return string
}
The call builder.show() which opens your AlertDialog is not a blocking call, meaning the next instructions will be executed without waiting for the AlertDialog to finish (return). The way you should interact with it is by using some sort of callback. For instance, your OnClickListeners are an implementation of such a pattern.
A simple callback pattern
One clean way to achieve what you want is to create a Functional Interface which is an interface having only one method. You would use it for handling your callbacks.
Example
interface OnOK{
void onTextEntered(String text);
}
And then you would alter you method to be like:
private void openDialog(String title, final OnOK onOK) {
AlertDialog.Builder builder = new AlertDialog.Builder(view.getContext());
builder.setTitle(title);
final EditText input = new EditText(view.getContext());
input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_NORMAL);
builder.setView(input);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//Oi, look at this line!
onOK.onTextEntered(input.getText().toString());
}
});
builder.show();
}
You can use it like this:
openDialog("Title", new OnOK() {
#Override
onTextEntered(String text) {
Log.i("LOG", text);
}
});
This looks to me like you have stored the value of the inputted text in the m_Text field. You can either just return that field or have a variable within the function in which you store the value to be returned.
Where you have:
//Return string
simply replacing with:
return m_Text;
should do the job.
Create another method in the same class that accepts a string value, then call that function providing the value of input.getText().toString() from your setPositiveButton onclick event
I am using the technique brought up in previous StackOverflow questions, which suggest using TextViews and setting properties in them as the parameter for setTitle in an AlertDialog. I need this so I can style the font of the title.
The problem is that Android Studio says that it
cannot resolve method android.widget.TextView
Below is my code:
TextView settingsTitle = new TextView(this);
settingsTitle.setText("Settings");
settingsTitle.setTypeface(Typeface.createFromFile("monospace"));
new AlertDialog.Builder(this)
.setTitle(settingsTitle)
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// continue with delete
}
})
.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// do nothing
}
})
.setIcon(android.R.drawable.ic_dialog_alert)
.show();
Replace your code as
new AlertDialog.Builder(this)
.setTitle(settingsTitle.getText().toString())
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// continue with delete
}
})
as there is no such method called setTitle() which has an argument of type TextView.
Thus use, settingsTitle.getText().toString() which gets the String in the TextView and use that to set the title like
setTitle(settingsTitle.getText().toString())
Read more about it in the docs
I have implemented a dialog box, I also get the text field as a string. Now the problem I have is that I want to compare the text input to a String and exit the app if the text on the dialog box matches either a string under (R.strings.stringname) or either a private String variable.
I have implemented this code but it does not seem to work.
public void onBackPressed(){
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setIcon(R.drawable.ic_about_logo);
alert.setTitle("Phoebus Club");
alert.setMessage("Please Insert Security Key");
// Set an EditText view to get user input
final EditText input = new EditText(this);
alert.setView(input);
alert.setPositiveButton("Enter", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
securityKey = input.getText().toString();
if(securityKey == "oneplc"){
System.exit(0);
}
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Canceled.
}
});
alert.show();
}
Have a look at this question
The == operator doesn't really compare values, rather reference equality. You should use the equals() method of the String object instead:
if(securityKey.equels("oneplc")){
System.exit(0);
}
if static string then instead of
securityKey == "oneplc"
do
securityKey.equals("oneplc");
if from strings.xml then
securityKey.equals(getResources().getString(R.string.stringname));
I have the following code:
protected void showSelectToDialog() {
boolean[] checkedDate = new boolean[toDate.length];
int count = toDate.length;
DialogInterface.OnClickListener setD2 = new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//TODO Auto-generated method stub
onChangeSelectedTo(which);
}
};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Select To Year");
builder.setSingleChoiceItems(toDate, count, setD2);
builder.setCancelable(true);
builder.setPositiveButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
dialog2 = builder.create();
dialog2.show();
}
protected void onChangeSelectedTo(int j) {
bTo.setText(toDate[j]);
sTo = ((AlertDialog)dialog2).getListView().getCheckedItemPosition();
blTo = true;
displayToast(String.valueOf(sTo));
to = j;
dialog2.dismiss();
}
What I am looking to do is, when the dialog loads the first time and the user selects a choice it is stored. So the next time the user opens the dialog, it will remember what was select and scroll to that choice.
How do I accomplish that?
Save the selected choice's position for the first time using Shared Preferences.
Then at the start of showSelectToDialog() check if any value exists in Shared Preferences, if so, then set the value of count from the Shared Preferences else set value of count to toDate.length.
I can't see the rest of your code, but all you have to do is save the user's choice in a variable somewhere else, and then read that choice every time you open the dialogue. It could be a static variable on the class, or it could be an instance variable of the class, or it could be a public field of some other class you have access to, like a parent object. You just need to assign it when you close the dialogue, and read it back and initialize the value to what you read when you open the dialogue.