Button to start activity if statement - java

I want the button to open a dialog only if the content equals to America. How do I do it?
I've tried creating a dialog activity and then using an intent I got the dialog box but it appeared separately and not over the current activity!
if(check.contentEquals("america")){
// Then I want the dialog to open here

try this:
if(check.contentEquals("america")){
imageview.setAlpha(0);
Intent i = new Intent(ACTION NAME);
startActivity(i);
}
if you want to run a simple dialog all you have to do is:
if(check.contentEquals("america")){
imageview.setAlpha(0);
mDialog.show();
}

to display an alert dialog try this:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(true);
builder.setTitle("Title");
builder.setMessage("your message");
builder.setInverseBackgroundForced(true);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
final AlertDialog alert = builder.create();
if(check.contentEquals("america")){
alert.show();
}

Related

android studio exit confirmation dialogue not working

public void exit(){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setIcon(R.mipmap.ic_launcher_round);
builder.setTitle("Likee Likes");
builder.setMessage("Do you really wanna Exit?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.create();
builder.show();
}
**I am using this code to confirm my user either exit or not. when my user click the "yes" button the app doesn't close and get back to the previous activity. Is there any mistake with this code? **
i am trying to close my app by user confirmation.
I assumes you use AlertDialog in the another activity rather than your first activity, so when you use finish, you are close the activity that isn't the first activity.
If you you want to close you app, you can try use startActivityForResult to process some job accordingto the requestCode.
But now startActivityForResult is deprected, you can try to use new way to do this: OnActivityResult method is deprecated, what is the alternative?.
You can reference to this too How to quit android application programmatically

How to use a dialogue box within a popup window?

I have a popup window that gets executed when a button is pressed. When the user clicks "home" within the popup window I want a dialogue message to pop up asking the user if they are sure they want to quit.
Here is how I declare the popup window:
LayoutInflater inflater = (LayoutInflater) endlessflags.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layout = inflater.inflate(R.layout.success_menu, (ViewGroup) findViewById(R.id.myPop5));
pwindo4 = new PopupWindow(layout, ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.MATCH_PARENT);
pwindo4.setAnimationStyle(R.style.AnimationPopup);
pwindo4.showAtLocation(layout, Gravity.TOP | Gravity.CENTER, 0, 0);
And then I have a onTouchListener for the button that are also called in the popup window. I try to call the dialogue function I created but when I do nothing happens and I get no errors or crashes:
AlertDialog.Builder myAlert = new AlertDialog.Builder(mainActivity.this);
myAlert.setMessage("Would you like to quit?").setCancelable(false).setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
saveState = true;
saveState();
//finish();
dialog.cancel();
}
}).setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
saveState = false;
dialog.cancel();
}
});
My question is why is it that when I call the dialogue event function nothing seems to happen and how can I fix this so the dialogue box shows up when I click a button within my popup window?
You need to show the dialog:
AlertDialog dialog = myAlert.create();
dialog.show();

Place icon at right of title in a AlertDialog builder

All the answers and info that i find at internet seems old and deprecated, i could add a simple icon at my title bar at left (user image), but i need to have 2 more images, a message and a phone at the right, like a simple user details view, the problem is that i don't know how to set the position of those images, the only thing i could do was this:
AlertDialog.Builder builder = new AlertDialog.Builder(Friends.this);
final User user = usersList.get(position);
builder.setTitle(user.getUsername());
builder.setMessage("You wanna delete friendgroup: " + user.getUsername());
builder.setIcon(R.drawable.ic_user);
builder.setCancelable(false);
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//myRef.child("Users").child(userId).child("FriendLists").child(friend.getName()).removeValue();
//friendList.remove(friend);
//mAdapter.notifyDataSetChanged();
}
});
builder.show();
with the builder.setIcon i could add a image at the left, but how can i add more icons and at the right of my title ? do i really need a custom builder to do this?
Thanks
You have to make a custom AlertDialog.
Try this:
Design a RelativeLayout that contains your icon, title and right message and phone icon and other things.
Set this layout to your AlertDialog using setView() method.
AlertDialog.Builder builder = new AlertDialog.Builder(Friends.this);
final User user = usersList.get(position);
LayoutInflater inflater = this.getLayoutInflater();
View dialogView = inflater.inflate(R.layout.your_dialog_layout, null);
builder.setView(dialogView);
builder.setCancelable(false);
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//myRef.child("Users").child(userId).child("FriendLists").child(friend.getName()).removeValue();
//friendList.remove(friend);
//mAdapter.notifyDataSetChanged();
}
});
builder.show();
Here is a good tutorial
Hope this will help~

Add a link to a button on an AlertDialog that shows when the app starts

I have an Android app, I need to add to it an AlertDialog that shows when the app starts. Also, I need to add to the AlertDialog a button (like: visit website), when the user click that button, it will open the link and browse it in the browser or anything else.
How I can do that?!
Thanks in advance.
Use an AlertDialog.Builder in your activities onCreate() method. You can use setPositiveButton() and launch your website in an intent, or webview when the user clicks it.
For example
new AlertDialog.Builder(mContext)
.setMessage("Launch Website")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// Hide the dialog
dialog.dismiss();
// Launch the website
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://google.com"));
startActivity(intent);
}
})
.show();
See http://developer.android.com/reference/android/app/AlertDialog.Builder.html
First you have to add an AlertDialog to your onCreate() method in your Activity.
After then You have to add a button to that AlertDialog.
There are three types of button in AlertDialog.
Positive
Negative
Neutral
Use any one of them.
Then when the button clicked you need to go to the website url, in some browser in the android device.
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.your-web-site-url.com"));
startActivity(browserIntent);
Try this:
final Context context = this;
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
// set title
alertDialogBuilder.setTitle("Your Title");
// set dialog message
alertDialogBuilder
.setMessage("Click to visit website!")
.setCancelable(false)
.setPositiveButton("Go to web site",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.your-web-site-url.com"));
startActivity(browserIntent);
}
}));
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();

How to display AlertDialog in a Fragment?

I want to display an alert dialog in my app. I am using fragments. I tried the below code to do this:
AlertDialog ad = new AlertDialog.Builder(context)
.create();
ad.setCancelable(false);
ad.setTitle(title);
ad.setMessage(message);
ad.setButton(context.getString(R.string.ok_text), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
ad.show();
but it was crashing and the error in logcat was:
04-18 15:23:01.770: E/AndroidRuntime(9424): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
From internet I came to know that the crash is due to context issue. I had given context as
context = this.getActivity().getApplicationContext();
I don't know what is the problem with this. Can anybody help me?
Replace context with getActivity().
The ApplicationContext should not be used for tasks such as creating Dialogs. As you are in a fragment you can instead get the Activity-Context simply by calling the Fragments getActivity() method.
More Information about this question (AlertDialog in a fragment, managed inside an event):
If you call AlertDialog within an event like onClick(View v) or onLongClick(View v) you can use
public boolean onClick(View v) {
...
AlertDialog.Builder alertBuilder = new AlertDialog.Builder(v.getContext());
...
}
Try to use DialogFragment, DialogFragment is better when you use Fragments
I have had similar issues whereby I was trying to create an AlertDialog from a Fragment. A NullPointerException arose from it. Initially I did as follows:
AlertDialog alertDialog = new AlertDialog.Builder(getActivity()).create();
The NullPointerException occurred specifically when calling alertDialog.show() later on in the code.
But after searching the documentation for AlertDialog.Builder(), there seemed to be another way to initialize it [AlertDialog.Builder Doc], which is to include a theme/resId as shown below:
AlertDialog alertDialog = new AlertDialog.Builder(getActivity(), R.style.Theme_AppCompat_Dialog_Alert).create();
This resolved the NullPointerException at hand. Hope this helps you as well!
I used it in an adapter inside a listView, therefore I couldn't use getActivity(). In order to make it work I used getActivity() for the context in the instantiation of the adapter in the fragment:
this.adapter = new myAdapter(getActivity(), factory);
Later in the other class (the adapter's class) I was able to use getContext()and it worked.
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
AlertDialog alert= null;
AlertDialog.Builder build= new AlertDialog.Builder(getActivity());
build.setTitle("title");
build.setItems(stringarrayname, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
//Toast.makeText(getActivity(), "hi", Toast.LENGTH_SHORT).show();
}
});
build.create().show();
You can try this or use DialogFragment
private void showAlert(final int position) {
new AlertDialog.Builder(getActivity().getApplicationContext())
.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) {
// deleteSuggestions(position);
}
})
.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();
}
The solution is to replace by getActivity()
AlertDialog.Builder alert = new AlertDialog.Builder(getActivity(),R.style.MaDialog);

Categories