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
Related
I have an AlertDialog and I want to show it even if the user is visiting another activity.
This is AlertDialog:
AlertDialog.Builder builder = new AlertDialog.Builder(context.getApplicationContext());
builder.setMessage("Message").setCancelable(
false).setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
}).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
AlertDialog alert = builder.create();
alert.show();
You can create a method in utils class and pass context, so that you can use dialog in required class, but precaution need to take with life cycles to avoid memory leaks.
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.
I have the following code inside an Adapter class I use for a listview in another class.
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setTitle("Introduceti parola:");
final EditText input = new EditText(getContext());
input.setInputType(InputType.TYPE_CLASS_TEXT);
builder.setView(input);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which)
{
hash.put("name", Rooms.name);
hash.put("parola", input.getText().toString());
site = siteul + "/join";
new ATask((ViewHolder) v.getTag()).execute(site);
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.show();
The problem is that when I trigger the action I get this error at the builder.show() line
android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
I thought the getContext() was at fault, but it doesn't generate any error in this code in the same Adapter class:
Toast.makeText(getContext(), "Cool message!", Toast.LENGTH_LONG).show();
What could be the problem then?
Replace getContext() with ActivityName.this.
The code is not working. Please help me. It print the replace all string, but further code is not running.
when I debug this, there is no error in the code. It will show the code of alert box.
if(count>0)
{
System.out.println("replace all string name ");
// final Intent intent_ul=new Intent(this, UploadExcel.class);
AlertDialog.Builder alertDialogBuilder_ue = new AlertDialog.Builder(this);
alertDialogBuilder_ue.setTitle("Alert!!");
alertDialogBuilder_ue
.setMessage("Are you sure you want to Replace all the data related to this style ? ")
.setCancelable(false)
.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
mySQLiteAdapter.openToWrite();
mySQLiteAdapter.delete_style_measurement(style_no);
Log.d("","yes click");
count=0;
mySQLiteAdapter.close();
}
})
.setNegativeButton("No",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
Log.d("","No click");
count++;
dialog.cancel();
// startActivity(intent_ul);
//finish();
}
});
}
Add these lines before the end of if condition
AlertDialog alertDialog = alertDialogBuilder_ue.create();
alertDialog.show();
You need to add
alertDialogBuilder_ue.show();
in your code
Check with this code. This code working for me
Context context = CurrentActivity.this;
AlertDialog.Builder ad = new AlertDialog.Builder(context);
ad.setTitle("Application");
ad.setMessage("Do you want to proceed?");
ad.setPositiveButton("Yes", new OnClickListener()
{
public void onClick(DialogInterface dialog, int arg1)
{
}
});
ad.setNegativeButton("Cancel", new OnClickListener()
{
public void onClick(DialogInterface dialog, int arg1)
{
}
});
ad.setCancelable(false);
ad.show();
In your code adding alertDialogBuilder_ue.show(); should make the dialog appear.
By some people it has been suggested that you have to use alertDialogBuilder_ue.create(); to get a handle to the AlertDialog that you can then use the .show() method on.
Both are possibilities but you don't have to use the .create() option if you don't need a handle to the AlertDialog
I am validating an AlertDialog, and I would like to raise a Toast on top of the AlertDialog display.
I have this code, but the Toast is displayed on the activity
new AlertDialog.Builder(this).setTitle(R.string.contact_groups_add)
.setView(addView).setPositiveButton(R.string.ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
if (wrapper.getTitle().length()>0)
{
processAdd(wrapper);
} else {
Toast.makeText(getApplicationContext(), "Name is required", Toast.LENGTH_SHORT).show();
}
}
}).setNegativeButton(R.string.cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
// ignore, just dismiss
}
}).show();
Instead of using AdvertDialog.Builder, you can create a custom dialog which will behave like a dialog, but is in fact a normal activity. Your toasts should be drawn normally on top of this.
Had this problem myself as well, when I wanted to show a validation message within a dialog.
The answer that seanhodges gave is probably the cleaner and better way. But a seperate activity wasnt practical for me, so i came up with this solution.
Anyway, you can use the AlerDialog.Builder, and show a toast.
If you override the OnClickListener of the button the you want to trigger the toast, you can show a toast on top of a dialog.
An example;
public void showToastOnDialog(final Context context) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Dialog title");
builder.setMessage("Dialog message");
builder.setPositiveButton(android.R.string.ok, new OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// Do nothing, you will be overriding this anyway
}
});
builder.setNegativeButton(android.R.string.cancel,
new OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// You can implement code here, because you wont be
// overriding this
}
});
final AlertDialog dialog = builder.create();
// Make sure you show the dialog first before overriding the
// OnClickListener
dialog.show();
// Notice that I`m not using DialogInterface.OnClicklistener but the
// View.OnClickListener
dialog.getButton(Dialog.BUTTON_POSITIVE).setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast toast = Toast.makeText(context,
"I`m a toast on top of a dialog.",
Toast.LENGTH_LONG);
toast.show();
// Because you are overriding the OnClicklistener, the
// dialog will not auto dismiss
// after clicking
dialog.dismiss();
}
});
}
Try this:
AlertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_PHONE);