ContactsDataList is an Array List that contains the data of all Contacts.
I have written following code that pass data to List Adapter
adapter= new ContactsAdapter(getApplicationContext(), contactsDataList);
listView = (ListView) findViewById(R.id.lv_contacts);
listView.setAdapter(adapter);
On List View, I want to Launch Call_Action event, whenever user clicks on any item in List.
I have written following code in Adapter, but its giving me this error message
java.lang.IllegalStateException: You need to use a Theme.AppCompat
theme (or descendant) with this activity.
contactsHeading.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View v) {
AlertDialog alertDialog = new AlertDialog.Builder(context).create();
alertDialog.setTitle("Alert");
alertDialog.setMessage("Are you sure you want to call "+contactsHeading + "?" );
alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + contactsText));
if (ActivityCompat.checkSelfPermission(v.getContext(), Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
return;
}
v.getContext().startActivity(intent);
}
});
alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE, "Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alertDialog.show();
Kindly guide me how to resolve this error message.
This is caused by the Context you passed into the AlertDialog.Builder.
In this line:
AlertDialog alertDialog = new AlertDialog.Builder(context).create();
Replace
context
with
[YourActivityName].this
Related
I am using a dialog in my app that pops up and interacts with the user. I haven't worked with dialogs before, so i know next to nothing about styling them. This is the code:
public void openDialog() {
#SuppressLint("InflateParams") View view = (LayoutInflater.from(AudioRecorder.this)).inflate(R.layout.audio_name_input, null);
AlertDialog.Builder alertBuilder = new AlertDialog.Builder(AudioRecorder.this);
alertBuilder.setView(view);
final EditText userInput = view.findViewById(R.id.userInput);
alertBuilder.setCancelable(true);
alertBuilder.setPositiveButton("Save", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
inputName = String.valueOf(userInput.getText());
if (!inputName.isEmpty()) {
Toast.makeText(AudioRecorder.this, "Next audio clip will be named... " + inputName, Toast.LENGTH_SHORT).show();
filePathMaking();
} else {
inputName = "recorded_audio";
Toast.makeText(AudioRecorder.this, "Input field empty, next audio clip will be named... " + inputName, Toast.LENGTH_SHORT).show();
}
}
});
alertBuilder.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
Dialog dialog = alertBuilder.create();
dialog.show();
}
Can we style the "Save" button to display red text?
You can get the Button and then change it's text color. Something along the following lines should work,
public void openDialog() {
#SuppressLint("InflateParams") View view = (LayoutInflater.from(AudioRecorder.this)).inflate(R.layout.audio_name_input, null);
AlertDialog.Builder alertBuilder = new AlertDialog.Builder(AudioRecorder.this);
alertBuilder.setView(view);
final EditText userInput = view.findViewById(R.id.userInput);
alertBuilder.setCancelable(true);
alertBuilder.setPositiveButton("Save", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
inputName = String.valueOf(userInput.getText());
if (!inputName.isEmpty()) {
Toast.makeText(AudioRecorder.this, "Next audio clip will be named... " + inputName, Toast.LENGTH_SHORT).show();
filePathMaking();
} else {
inputName = "recorded_audio";
Toast.makeText(AudioRecorder.this, "Input field empty, next audio clip will be named... " + inputName, Toast.LENGTH_SHORT).show();
}
}
});
alertBuilder.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
Dialog dialog = alertBuilder.create();
dialog.show();
Button positiveButton = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
positiveButton.setTextColor(Color.parseColor("#FF0B8B42"));
}
You can use AlertDialog as Chrisvin Jem suggested in his answer but I would like to offer another solution:
You can just create a custom dialog class in order to give your dialog a custom layout, control everything in a separate class - I find it cleaner and more organized.
For example, create dialogClass:
public class ProgressDialog extends Dialog {
public ProgressDialog(#NonNull Context context) {
super(context);
setContentView(R.layout.progress_dialog); //this is your layout for the dialog
}
}
And all you need to do is to create dialog instant and call it like this:
ProgressDialog progressDialog = new ProgressDialog(getContext());
progressDialog.show(); // this line shows your dialog
Why I recommend using this and not AlertDialog.Builder :
You can build your layout in a faster way with custom dialog.
No need to write a lot of code just to add views when you can have a custom layout.
It's easier (or so I believe) for you to see myCoolDialog.show(); rather than 50 lines of code or more in a single method.
Do you need to change anything regarding your dialog look and code? good, go to your separate class and change it instead of adding 20 more code lines to your activity.
Chrisvin Jem gave the extact answer to your question however if you want more control over your design you can the this code
final Dialog dialog = new Dialog(context);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCancelable(false);
dialog.setContentView(R.layout.yourview);
RelativeLayout submit = dialog.findViewById(R.id.submit);
final EditText edittext = dialog.findViewById(R.id.edittext);
submit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
Toast.makeText(context, getString(R.string.thanks), Toast.LENGTH_SHORT).show();
}
});
dialog.show();
I have currently working with some application and exploring things and I encountered this error. This just happened out of nowhere and earlier it seems working fine.
I read some questions related to my problem but I can't relate my codes to them so I am really confused. I'm still beginner so any help would be appreciated.
Logcat
4386-4386/com.example.gab.quadrantms E/WindowManager: android.view.WindowLeaked: Activity com.example.gab.quadrantms.Home has leaked window DecorView#abb868f[] that was originally added here
at android.view.ViewRootImpl.<init>(ViewRootImpl.java:576)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:363)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:128)
at android.app.Dialog.show(Dialog.java:454)
at com.example.gab.quadrantms.SurveysFragment$1$1$1.onDataChange(SurveysFragment.java:120)
Fragment
if (getActivity() != null) {
if (myPosition.equals("Project Manager")) {
bottomSheetDialog = new BottomSheetDialog(getActivity());
final View bottomSheetDialogView = getLayoutInflater().inflate(R.layout.bottom_dialog_layot, null);
bottomSheetDialog.setContentView(bottomSheetDialogView);
View viewView = bottomSheetDialogView.findViewById(R.id.view);
View assignView = bottomSheetDialogView.findViewById(R.id.assign);
View deleteView = bottomSheetDialogView.findViewById(R.id.delete);
bottomSheetDialog.show(); //THIS LINE WHERE THE ERROR IS POINTING
if (bottomSheetDialog.isShowing()) {
assignView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String id = viewHolder.setSurveyID(model.getSurveyID());
String loc = viewHolder.setSurveyLocation(model.getSurveyLocation());
String type = viewHolder.setSurveyType(model.getSurveyType());
String progress = viewHolder.setProgress(model.getProgress());
if (progress.equals("Finished")) {
Toast.makeText(getContext(), "This project is Already Finished.", Toast.LENGTH_SHORT).show();
} else {
Intent surveyAssign = new Intent(getActivity(), AssignActivity.class);
surveyAssign.putExtra("id", id);
surveyAssign.putExtra("loc", loc);
surveyAssign.putExtra("type", type);
surveyAssign.putExtra("progress", progress);
startActivity(surveyAssign);
getActivity().finish();
}
}
});
deleteView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder message = new AlertDialog.Builder(getContext());
message.setMessage("Are you sure you want to Delete this Project?").setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(final DialogInterface dialog, int which) {
String id = viewHolder.setSurveyID(model.getSurveyID());
mRemoveDuplicate = FirebaseDatabase.getInstance().getReference().child("Surveys");
mRemoveDuplicate.child(id).removeValue().addOnSuccessListener(new OnSuccessListener<Void>() {
#Override
public void onSuccess(Void aVoid) {
Toast.makeText(getContext(), "Project Deleted.", Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
});
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
AlertDialog alert = message.create();
alert.setTitle("Message");
alert.show();
}
});
Please add your Activity Name in All Dialog where your fragment is run.
Eg.
bottomSheetDialog = new BottomSheetDialog(YourActivity.this);
AlertDialog.Builder message = new AlertDialog.Builder(YourActivity.this);
I hope this can help You!
Thank You.
When does the app crashes? Is it while showing the dialog or while closing the dialog.
If its while closing the dialog, check whether you are dismissing the dialog properly.
Mostly WindowLeaked error occurs when you finish the parent activity before dismissing the dialog properly.
Note : If the app crashes during OnClick on assignView, finishing the activity here getActivity().finish(); in OnClickListener might be the problem.
Please, i would like to show back details after the user must have input something, back on alert dialog box in Android studio. I used this code below:
editText = (EditText) findViewById(R.id.my_edit_txt);
editText.getText().toString();
But it doesn't show on the confirmation dialog box I created.
It looks like you didn't set the text of your AlertDialog, but this is just an assumption because there is not enough code in your question. Calling editText.getText().toString() does not do anything but return a String. It does not assign it to anything. An example with an AlertDialog would be the following:
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage(editText.getText().toString());
builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User clicked OK button
}
});
builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
// Set other dialog properties
...
// Create the AlertDialog
AlertDialog dialog = builder.create();
I've took this example from Android Developers and modified it so that it includes the text of your EditText. This code should work because you not only call the toString() method but also assign it's return value to the AlertDialog's message property.
This is my entire code for the alert dialog box:
public void alertdialog(View view){
mybtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
AlertDialog.Builder cfmalt = new AlertDialog.Builder(Dashboard.this);
//cfmalt.setMessage("Do you want to quit?").setCancelable(false);
//editText.getText().toString();
cfmalt.setMessage(editText.getText().toString()+"\n"+ vol_edit2.getText().toString());
cfmalt.setMessage(dt.getMonth())
//cfmalt.setMessage("Name:").setMessage(vol_edit2.getText().toString());
cfmalt.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
finish();
}
});
cfmalt.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.cancel();
}
});
I'm trying to do alert message by disable ok and cancel button if the checkbox is unchecked.
reconfirm.java:
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(reconfirm.this);
LayoutInflater layoutInflater
= (LayoutInflater)getBaseContext()
.getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.popup, null);
alertDialogBuilder.setView(popupView);
CheckBox check= (CheckBox)findViewById(R.id.checkBox1);
if (check.isChecked() ) {
AlertDialog dialog = null;
((AlertDialog)dialog).getButton(DialogInterface.BUTTON_POSITIVE).setVisibility(View.INVISIBLE);
}
alertDialogBuilder.setPositiveButton("OK",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent intObj = new Intent(getApplicationContext(),
agree.class);
startActivity(intObj);
}
});
alertDialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent intObj = new Intent(getApplicationContext(),
IntentExampleActivity.class);
startActivity(intObj);
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
This is answered already here--How to disable / enable dialog negative positive buttons?
after dialog.show use below code
if(your_condition_true)
dialog.getButton(AlertDialog.BUTTON1).setEnabled(false); //BUTTON1 is positive button
Try Using setEnabled() and setClickable method for buttons.
here is the doc
Try following code snippets. Hope you get some idea from this :)
if (check.isChecked()) {
alertDialogBuilder.getButton(Dialog.BUTTON_POSITIVE).setEnabled(false);
alertDialogBuilder.getButton(Dialog.BUTTON_NEGATIVE).setEnabled(false);
}
where dialog is object of AlertDialog.
I need to show AlertDialog#1 with custom layout R.layout.add_new_entry with button mybtn and this button should open another AlertDialog#2 with single choice list and after choosing one of them this dialog (AlertDialog#2) should send index of shoosen into first dialog (AlertDialog#1) and set some values into EditText field in AlerDialog#1. Is it possible to make?
My dialog#1 with custom layout:
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
builder.setView(inflater.inflate(R.layout.add_new_entry, null))
.setTitle(dialogTitle)
.setPositiveButton("Add", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
// My functions...
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// My functions...
}
});
builder.create().show();
I'v spent all night on it and i can't find any good examples or articles how to open dialog inside dialog.
Thanks