AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
TextView titleView = new TextView(this);
EditText editTextView = new EditText(this);
titleView.setText("PASSWORD");
editTextView.setHint("password");
editTextView.setFilters(new InputFilter[]{new InputFilter.LengthFilter(15)});
titleView.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_START);
dialogBuilder.setCustomTitle(titleView);
dialogBuilder.setView(editTextView);
dialogBuilder.setPositiveButton("confirm", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface DialogInterface, int i) {
String editTextInput = editTextView.getText().toString();
}
});
dialogBuilder.setNegativeButton("dismiss", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
});
AlertDialog alertDialog = dialogBuilder.create();
alertDialog.show();
This is my alert dialog . When ,dialog shows, keyboard gets up and editText has focus , i tried to force hidding keyboard different ways but without success so far . I looking forward to show this diakog while keeping keyboard down .
Related
AlertDialog.Builder builder = new AlertDialog.Builder(adapter.getContext());
builder.setTitle("Sterge Task-ul");
builder.setMessage("Esti sigur ca vrei sa stergi acest Task?");
AlertDialog dialog = builder.setNegativeButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
adapter.deleteItem(position);
}
}).create();
I don't know what to do more, can someone help?
//NotificationPermission Dialog
private void notificationPermissionDialog(){
AlertDialog dialog = new AlertDialog.Builder(getActivity())
.setCancelable(false)
.setMessage("Do you want to Enable Notifications?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
savePermissionInPreferences("Yes");
dialogInterface.dismiss();
}
}).setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
savePermissionInPreferences("No");
dialogInterface.dismiss();
}
}).show();
}
//Save Notification Permission in Preferences
private void savePermissionInPreferences(String isNotificationAllowed){
SharedPreferences sharedPreferences;
sharedPreferences = getContext().getSharedPreferences("PermissionPref", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("isNotificationAllowed", isNotificationAllowed);
editor.apply();
}
I have created a dialogue which appears when fragment is opened and ask for permission if I click on NO its value is saved in shared preference for preventing it to appear again but when I replace my fragment in same activity dialogue appears again and again. Please anyone can guide me through it.
Thanks in advance.
So you can do something like below
private void notificationPermissionDialog(){
SharedPreferences sharedPreferences = getSharedPreferences("PermissionPref", Context.MODE_PRIVATE);//Add these
String permission = sharedPreferences.getString("isNotificationAllowed", "");
//Add this line
if(!permission == "No"){
AlertDialog dialog = new AlertDialog.Builder(getActivity())
.setCancelable(false)
.setMessage("Do you want to Enable Notifications?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
savePermissionInPreferences("Yes");
dialogInterface.dismiss();
}
}).setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
savePermissionInPreferences("No");
dialogInterface.dismiss();
}
}).show();
}
}
//Save Notification Permission in Preferences
private void savePermissionInPreferences(String isNotificationAllowed){
SharedPreferences sharedPreferences;
sharedPreferences = getContext().getSharedPreferences("PermissionPref",
Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("isNotificationAllowed", isNotificationAllowed);
editor.apply();
}
If the user selected no to the Alert Dialog it wont appear again.
In my adapter class I have created a alert dialog and added a update button to it which allows me to update the items on my list view, it updates my listview but only when I leave the page and return back to it, I have used notifydatasetchanged but it doesn't seem to work, I want it to update on the list view immediately after the dialog closing.
update button
viewHolder.btnDisplayUpdate.setText("Update");
viewHolder.btnDisplayUpdate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
updateCrime(crimes);
}
});
Update method
private void updateCrime(CrimesDelete crimes){
AlertDialog.Builder popupWindow = new AlertDialog.Builder(context);
LayoutInflater inflater = LayoutInflater.from(context);
View root = inflater.inflate(R.layout.dialog_update, null);
popupWindow.setView(root);
AlertDialog alertDialog = popupWindow.create();
alertDialog.show();
editLocation = (EditText)root.findViewById(R.id.editText_location);
editType = (EditText)root.findViewById(R.id.editText_type);
editDescription = (EditText)root.findViewById(R.id.editText_description);
editDate = (EditText)root.findViewById(R.id.textViewDate);
editTime = (EditText)root.findViewById(R.id.textViewTime);
Spinner spinner = (Spinner) root.findViewById(R.id.editText_rating);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
getContext(), R.array.rating, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
editRating = parent.getItemAtPosition(pos).toString();
if (pos==0){
editRating = null;
}
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
editLocation.setText(crimes.getLOCATION());
editType.setText(crimes.getTYPE());
editDescription.setText(crimes.getDESCRIPTION());
editDate.setText(crimes.getDATE());
editTime.setText(crimes.getTIME());
root.findViewById(R.id.btnAdd).setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
String location = editLocation.getText().toString().trim();
String type = editType.getText().toString().trim();
String description = editDescription.getText().toString().trim();
String date = editDate.getText().toString().trim();
String time = editTime.getText().toString().trim();
String rating = spinner.getSelectedItem().toString().trim();
DB_CRIME.updateDate(crimes.getID(), location, type, description, date, time, rating);
loadUpdatedCrimes();
alertDialog.dismiss();
}
});
}
private void loadUpdatedCrimes() {
notifyDataSetChanged();
Toast.makeText(getContext(), "Crime has been Updated" , Toast.LENGTH_SHORT).show();
}
so I have an app with three different buttons(parent buttons) on pressing any of them, the custom alert dialog with 9 different buttons is displayed but the functions of these 9 buttons within the alert dialog are different depending on which of the three parent buttons called it. On pressing any of the 9 buttons, I want the app to perform a particular function and then close the alertdialog. Now the problem is that I can easily call the alert dialog by calling the method showcustomdialog(); that I created but I can't dismiss it using alertdialog.dismiss(); inside the OnClickListener of the parent button since the method has a void result type. I've tried using if-else statements but it doesn't work. How can I achieve what is required?
The method:
private void showCustomDialog() {
ViewGroup viewGroup = findViewById(android.R.id.content);
View dialogView = LayoutInflater.from(this).inflate(R.layout.dialog_main2, viewGroup, false);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(dialogView);
final AlertDialog alertDialog = builder.create();
alertDialog.show();
}
I am calling the meathod and using it as follows:
parentbutton1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
showCustomDialog();
alertbutton1.getId();
alertbutton1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
textView1.setText("500");
function();
//I want to dismiss the alertdialog here.
}
});
alertbutton2.getId();
alertbutton2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
textView1.setText("1000");
function();
//I want to dismiss the alertdialog here.
}
});
and so on.
I figured out a solution for you using AlertDialog.
parentbutton1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
createDialog();
}
});
Then the method MUST be a private or public AlertDialog:
private AlertDialog createDialog() {
LayoutInflater inflater = (getActivity()).getLayoutInflater(); // for fragment or getLayoutInflater(); for activity
View v = inflater.inflate(R.layout.dialog_add_new_list, null);
Button okButton = v.findViewById(R.id.confirm_button);
Button cancelButton = v.findViewById(R.id.cancel_button);
final AlertDialog dialog = new AlertDialog.Builder(getActivity()) // for fragment or AlertDialog.Builder ( this ) for activity
.setView(v)
.show();
okButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});
cancelButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View p1) {
dialog.dismiss();
}
});
return dialog;
}
P.S. Replace the ids I used to create this sample with yours.
view.findViewById(R.id.doc_prof_det_add_speciality_btn).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(v.getContext());
LayoutInflater factory = LayoutInflater.from(v.getContext());
final View view = factory.inflate(R.layout.doc_prof_det_add_speciality, null);
alertDialog.setView(view);
alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alertDialog.setPositiveButton("Save", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
alertDialog.show();
}
});
Try this:
alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alertDialog.setPositiveButton("Save", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
alertDialog.show();
//Grab the window of the dialog, and change the width
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
Window window = alertDialog.getWindow();
lp.copyFrom(window.getAttributes());
//This makes the dialog take up the full width
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
window.setAttributes(lp);
}
});