I am attempting to add a title to my AlertDialog Builder. When I add the theme, my title gets moves into the selection area.
Here is the first example:
classificationButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(
new ContextThemeWrapper(mContext, android.R.style.Theme_Holo_Dialog) );
//building my selection options
builder.setItems(classificationList, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
String desiredClassification = classificationList[which];
if ( !getClassification().equals(desiredClassification) ) {
CallsignContract.updateClassification(desiredClassification, mContext);
setClassification(desiredClassification);
classificationButton.setText(desiredClassification);
}
}
});
builder.setTitle(R.string.classification_alert_header)
.create().show();
}
});
This is the result.
On this second attempt, I create a alertdialog from the builder and give that a title. The result is the correct title, but the title appears again in the selection area.
classificationButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(
new ContextThemeWrapper(mContext, android.R.style.Theme_Holo_Dialog) );
//building my selection options
builder.setItems(classificationList, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
String desiredClassification = classificationList[which];
if ( !getClassification().equals(desiredClassification) ) {
CallsignContract.updateClassification(desiredClassification, mContext);
setClassification(desiredClassification);
classificationButton.setText(desiredClassification);
}
}
});
AlertDialog alertDialog = builder.create();
alertDialog.setTitle(R.string.classification_alert_header);
alertDialog.show();
}
});
Thank you!
In order to show only one title, you must call alertDialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE).
AlertDialog.Builder builder = new AlertDialog.Builder(
new ContextThemeWrapper(mContext, android.R.style.Theme_Holo_Dialog)
);
//building my selection options
builder.setItems(classificationList,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
String desiredClassification = classificationList[which];
if (!getClassification().equals(desiredClassification)) {
CallsignContract.updateClassification(desiredClassification, mContext);
setClassification(desiredClassification);
classificationButton.setText(desiredClassification);
}
}
}
);
AlertDialog alertDialog = builder.create();
alertDialog.setTitle(R.string.classification_alert_header);
// Requesting dialog to remove the title
alertDialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
alertDialog.show();
This is my code,
b3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (v == b3) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Delete")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
mydb.deleteContact(id_To_Update);
Toast.makeText(getApplicationContext(), "Deleted Successfully", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getApplicationContext(),MainActivity.class);
startActivity(intent);
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
AlertDialog d = builder.create();
d.setTitle("Are you sure");
d.show();
}
}
});
if (!rs.isClosed()) {
rs.close();
}
name.setText(nam);
email.setText(emai);
When I use this code on a delete button an error appear on first "this". How to solve it? How can I use delete confirmation message?
Please help me. Thanks in advance!
Use context instead of simply "this" reference. Here you can use Activity context, not any other context. For more information visit these links Context , AlertDialog.Builder
For activity:
AlertDialog.Builder builder = new AlertDialog.Builder(ActivityName.this);
For fragment:
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
Thanks
You need to replace
AlertDialog.Builder builder = new AlertDialog.Builder(this);
with
AlertDialog.Builder builder = new AlertDialog.Builder(YourActivityName.this);
I am always getting an error while trying to register a user to firebase in my android app.
"The email specified is invalid"
I noticed this happens on JellyBean devices and I don't know what the problem is, whether it is from the API or something else.
My code snippet is given below:
private void registerUser() {
// Sign Up
mRef.createUser(mAccount.name, mPassword, new Firebase.ResultHandler() {
#Override
public void onSuccess() {
loginUser(true);
}
#Override
public void onError(FirebaseError firebaseError) {
// Sign Up failed
if (firebaseError.getCode() == FirebaseError.NETWORK_ERROR) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setMessage(R.string.connection_error_message)
.setTitle(R.string.error_title)
.setPositiveButton("Try again", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
checkUserExistence();
}
}).setCancelable(false);
AlertDialog dialog = builder.create();
dialog.show();
} else {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setMessage(firebaseError.getMessage())
.setTitle(R.string.error_title)
.setPositiveButton("Try again", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
registerUser();
}
}).setCancelable(false);
AlertDialog dialog = builder.create();
dialog.show();
}
}
});
}
I want to add an icon to the alert dialog box. How do I add an icon to the alert box ?
Please help me.
public void onClick(View arg0) {
if(TextUtils.isEmpty(entermass.getText().toString()) || TextUtils.isEmpty(enterheight.getText().toString()))
{
if(TextUtils.isEmpty(entermass.getText().toString()) || TextUtils.isEmpty(enterheight.getText().toString()))
{
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Developer's Check");
builder.setMessage("Either Of The Text Fields Are Empty");
builder.setPositiveButton("OKAY", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
You can use builder.setIcon() method by specifying drawable or id of the resource:
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this)
.setIcon(R.drawable.ic_launcher) //using id of the drawable resource
In my android application, when i click on text view, i want to display an alert dialog box which contain list of items. How is it possible. kindly guide.
i code it as:
cus_name_txt = (TextView)findViewById(R.id.cus_name_txta);
cus_name_txt.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Onclick_click1(cus_name_txt);
// TODO Auto-generated method stub
}
});
contact_no_txt = (TextView)findViewById(R.id.contact_no_txta);
attend_by_txtbx = (EditText)findViewById(R.id.attend_by_txt);
attend_by_txtbx.setText(My_Task.attend_by_txt);
ticket_no_txt = (TextView)findViewById(R.id.ticket_no_txta);
task_detail_txt = (TextView)findViewById(R.id.task_detail_txt);
How can i get the alert box of list of items by clicking on textView. Please guide. I'll be thankful to u
If you want to show the progressBar Before loading of the List in alert dialog, then use AsyncTask for that.
e.g.:
private class LoadingTask extends AsyncTask<String, Void, String> {
#Override
protected void onPreExecute(){
super.onPreExecute();
progressDialog.show();
}
#Override
protected String doInBackground(String... str) {
String response = "";
// Call Web Service here and return response
response = API.getDealsByCategory(str[0], str[1]);
// e.g.: above is my WebService Function which returns response in string
return response;
}
#Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
System.out.println("result is: "+result);
new Thread(new Runnable() {
#Override
public void run() {
progressDialog.dismiss();
}
}).start();
// SHOW THE ALERT DIALOG HERE.....
}
}
Call AsyncTask as like below :
LoadingTask task = new LoadingTask();
task.execute("YOUR_PARAMETER","YOUR_PARAMETER");
//==============================
Just put below code in the Post Excution of the AsyncTask and you will get what you want.
final CharSequence[] items = {"","50","100","150","200","250","300","350","400","450","500","550","600","650","700","750","800","850","900","1000"};
AlertDialog.Builder builder = new AlertDialog.Builder(getParent());
builder.setTitle("Select Country");
//builder.setI
builder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
//Toast.makeText(getApplicationContext(), con.get(item).getCountrName(), Toast.LENGTH_SHORT).show();
selectDistanceTV.setText(items[item]);
System.out.println("Item is: "+items[item]);
/*CONTRY_ID = con.get(item).getCountryId();
stateET.requestFocus();*/
}
});
AlertDialog alert = builder.create();
alert.show();
Hope it will help you.
If you need more help on how to use AsyncTask then see here:Vogella
Comment me for any query.
Enjoy Coding... :)
Put following code in onClick of textView:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Select Color Mode");
ListView modeList = new ListView(this);
String[] stringArray = new String[] { "Bright Mode", "Normal Mode" };
ArrayAdapter<String> modeAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, stringArray);
modeList.setAdapter(modeAdapter);
builder.setView(modeList);
final Dialog dialog = builder.create();
dialog.show();
Or
Dialog dialog = new Dialog(**Your Context**);
dialog.setContentView(R.layout.**Your Layout File**);
dialog.show();
In this layout file you can make your layout as per your requirements. When you want to use ListView from your Dialog Layout file then you have to write
ListView listView = (ListView)**dialog**.findViewById(R.id.**Your ListView Id**)
You can pass list of items in an String Array and display it in AlertBox..
For Example:
private void SingleChoice() {
String[] selectFruit = new String[] {"Apple","orange","mango"};
Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Single your Choice");
builder.setItems(selectFruit, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,
selectFruit[which] + " Selected", Toast.LENGTH_LONG)
.show();
dialog.dismiss();
}
});
builder.setNegativeButton("cancel",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
AlertDialog alert = builder.create();
alert.show();
}
button.setOnClickListener(new OnClickListener()
{
public void onClick(View arg0)
{
new AlertDialog.Builder(MainActivity.this)
.setSingleChoiceItems(arrClientName,0, new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
name.setText(arrClientName[which]);
}
})
.setPositiveButton("Ok", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int id)
{
}
})
.show();
}
});