creating alertdialog inside a non activity class - java

i have a class that implements view.onclicklistener. how do i create an alert dialog from this class which is not an activity class.
i kept on getting this error.
java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
this is my code
class LoginView implements View.OnClickListener {
public void onClick(final View v) {
new AlertDialog.Builder(v.getContext())
.setTitle("Delete entry")
.setMessage("Are you sure you want to delete this entry?")
.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();

Do it like this
private Handler mHandler = new Handler(Looper.getMainLooper());
#Override
public void run() {
// ...
mHandler.post(new Runnable() {
public void run() {
// Create your AlertDialog Here...
}
});
// ...
}

try this method in your class:
public static void showAlert(Activity activity) {
new AlertDialog.Builder(activity)
.setTitle("Delete entry")
.setMessage("Are you sure you want to delete this entry?")
.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();
}

Related

Android how to use dialogfragment effecitively(using multiple dialog)

I want to show many dialogs in one activity.
I can show one dialog now, but when I add more dialog, it confuses me.
Here is my code.
#NonNull
#Override
public Dialog onCreateDialog(#Nullable Bundle savedInstanceState) {
//then create a alertDialog class
String [] age = getActivity().getResources().getStringArray(R.array.age);
String [] job = getActivity().getResources().getStringArray(R.array.job);
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("당신의 나이는?");
builder.setItems(age, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
((SetUpActivity)getActivity()).textView_age.setText(age[which]);
}
});
builder.setTitle("당신의 직업은?");
builder.setItems(job, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
((SetUpActivity)getActivity()).textView_job.setText(job[which]);
}
});
return builder.create();
}
When the user click the button,(and appear dialog box)
Here is a code
new FragmentDialogBox().show(getSupportFragmentManager(),"fragmentDialog");
Maybe I change the tag - "fragmentDialog" part?

How to prevent back button until confirmation message?

In Android Studio, I've used a WebView. So if a user clicks the back button, I want to show a confirmation message before app close.
This is my current code which I used, but it is not working every time
public void onBackPressed() {
new AlertDialog.Builder(this)
.setMessage("Are you sure you want to exit?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
finish();
}
})
.setNegativeButton("No", null)
.show();
}
The above code is working fine.
If you want to navigate the WebView back to the previous page use below one.
#Override
public void onBackPressed() {
if (mWebView.canGoBack()) {
mWebView.goBack();
}else{
new AlertDialog.Builder(this)
.setMessage("Are you sure you want to exit?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
super.onBackPressed();
}
})
.setNegativeButton("No", null)
.show();
}
}
Can you try with it. Hope it works
#Override
public void onBackPressed() {
if (webView.canGoBack()) {
// If web view have back history, then go to the web view back history
webView.goBack();
} else {
// Ask the user to exit the app or stay in here
exitApp();
}
}
public void exitApp() {
final AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
builder.setTitle("Please confirm");
builder.setMessage("Do you want to exit the app?");
builder.setCancelable(true);
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
finish();
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
});
// Create the alert dialog using alert dialog builder
AlertDialog dialog = builder.create();
// Finally, display the dialog when user press back button
dialog.show();
}
You have to call your confirmation message method inside your Activities onBackPressed Method
#Override
public void onBackPressed() {
if (mWebView.canGoBack()) {
mWebView.goBack();
} else {
onBackPressed() // This is Your Confirmation Dialog method
}
}
#Override
public void onBackPressed() {
if (webView.canGoBack()) {
webView.goBack();
}else {
new AlertDialog.Builder(this)
.setTitle("Are you sure?")
.setMessage("Do you want to exit?")
.setPositiveButton("Close", new DialogInterface.OnClickListener(){
#Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
})
.setNegativeButton("No", null)
.show();
}
This worked for me. Hope it works.

Adding and removing textviews from a dynamic list in Android Studio?

I've been struggling with this for a while now, looking for any tips how to proceed. I'm trying to create a fragment that has an add button, that will create a new textview and when clicking the textview , you would have the option to delete it.
Adding textviews works fine. The problem is in the foreach loop that sets up listeners for each textview and adds the option to delete them. It's forcing me to declare the current parameter final, or the deletion won't work. But if I do declare it final, then I can't add any new textviews to the list, so they wont have listeners.
So if anyone could give me some kind of a hint, that would be hugely appreciated.
Here's the list and the adding part.
//Variables
List<TextView> TextViews = new ArrayList<TextView>();
#Override
//Put button functionality here
public void onClick(View v) {
final LinearLayout myLayout = (LinearLayout) view.findViewById(R.id.linearlayout);
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setTitle("Title");
// Set up the input/
final EditText input = new EditText(getContext());
input.setInputType(InputType.TYPE_CLASS_TEXT);
builder.setView(input);
// Set up the buttons
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
notesIndex = new Integer(notesIndex + 1);
noteText = input.getText().toString();
TextView a = new TextView(view.getContext());
a.setText(noteText);
a.setHeight(150);
a.setGravity(Gravity.CENTER);
myLayout.addView(a);
TextViews.add(a);
noteTexts.add(noteText);
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.show();
And here is the foreach loop that sets up listeners for each TextView. They are both called in OnCreateView.
for (final TextView current : TextViews) {
current.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setTitle("Delete?");
// Set up the buttons
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
myLayout.removeView(current);
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.show();
}
});
}
// Set up the buttons
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
addViewToLayout();
}
});
void addViewToLayout(){
notesIndex = new Integer(notesIndex + 1);
noteText = input.getText().toString();
TextView a = new TextView(view.getContext());
a.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
showAlertToDelete(v)
}
});
a.setText(noteText);
a.setHeight(150);
a.setGravity(Gravity.CENTER);
myLayout.addView(a);
TextViews.add(a);
noteTexts.add(noteText);
}
void showAlertToDelete(View v){
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setTitle("Delete?");
// Set up the buttons
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
myLayout.removeView(v);
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.show();
}

How can I tell when my Android alert dialog is cancelled?

Here is my alert:
new AlertDialog.Builder(Activity.this)
.setMessage("You have unsaved text. Are you sure you want to leave?")
.setCancelable(true)
.setNegativeButton("Leave", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id)
{
finish();
}
})
.setPositiveButton("Stay", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {}
}).show();
Notice how I allow the alert to be cancelable: .setCancelable(true)
How can I run some code once the alert is cancelled by the user pressing the back button?
According to the AlertDialog.Builder documentation on Android's website, you can use the setOnCancelListener (DialogInterface.OnCancelListener onCancelListener) method to handle when the dialog is cancelled.
.setOnCancelListener(new DialogInterface.OnCancelListener() {
#Override
public void onCancel(DialogInterface dialog)
{
// do what you need when the dialog is cancelled
}
})
So, your code would change to this:
new AlertDialog.Builder(Activity.this)
.setMessage("You have unsaved text. Are you sure you want to leave?")
.setCancelable(true)
.setOnCancelListener(new DialogInterface.OnCancelListener() {
#Override
public void onCancel(DialogInterface dialog)
{
// do what you need when the dialog is cancelled
}
})
.setNegativeButton("Leave", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id)
{
finish();
}
})
.setPositiveButton("Stay", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {}
}).show();

how to reload main activity after click "OK" on AlertDialog

I have an AlertDialog and I want the Main Activity to reload after I have click "OK". The problem is after I have click OK my activity returned to the home screen.
JAVA Code :
private void buildAlertMessageNoGps() {
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(
"This application needs GPS satellite or Wireless Networks localization enabled"
+ "\n" + "Do you want to enable it?")
.setCancelable(true)
.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(final DialogInterface dialog, #SuppressWarnings("unused") final int id) {
startActivity(new Intent(
Settings.ACTION_LOCATION_SOURCE_SETTINGS));
finish();
startActivity(getIntent());
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(final DialogInterface dialog, #SuppressWarnings("unused") final int id) {
dialog.cancel();
}
});
final AlertDialog alert = builder.create();
alert.show();
}
Help would be appreciated.
Change this code
new DialogInterface.OnClickListener() {
public void onClick(final DialogInterface dialog, #SuppressWarnings("unused") final int id) {
startActivity(new Intent(
Settings.ACTION_LOCATION_SOURCE_SETTINGS));
finish();
startActivity(getIntent());
}
to
new DialogInterface.OnClickListener() {
public void onClick(final DialogInterface dialog, #SuppressWarnings("unused") final int id) {
startActivityForResult(new Intent(
Settings.ACTION_LOCATION_SOURCE_SETTINGS), 0);
}
Call onCreate method again. But pass null as a parameter.
onCreate(null);

Categories