As the title says, I want to make a button in my alertDialog of my app go to a certain URI, and I was wondering how i would do this?
heres and excerpt of the code:
// Add a neutral button to the alert box AND assign a listener for said button...
alertbox.setNeutralButton("Ok", new DialogInterface.OnClickListener(){
// click listener for box
public void onClick(DialogInterface arg0, int arg1){
// Button was clicked!!
Toast.makeText(getApplicationContext(), "Dialog closed successfully!", Toast.LENGTH_LONG).show();
}
});
// Add a Forums button to take user to forums...
alertbox.setPositiveButton("Forums", new DialogInterface.OnClickListener(){
//listener for button
public void onClick(DialogInterface arg0, int arg1){
// Button Pressed!
Toast.makeText(getApplicationContext(), "Oops...this button is broke!", Toast.LENGTH_LONG).show();
}
});
// show it!!!
alertbox.show();
instead of making display the toast info saying the button is broke, I actually want it to launch the browser and take the user to a URI.
There has to be a way...
Ideas?
Thanks!
Updated with more code..
Start intent in onClick() handler:
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.addCategory(Intent.CATEGORY_BROWSABLE);
intent.setData(Uri.parse("http://website.com"));
startActivity(intent);
Related
I am trying to create a very simple dialog box. I found the code references from the internet obviously.
My code is this:
AlertDialog.Builder alertDialog = new AlertDialog.Builder(
ConsignmentConViewActivity.this);
alertDialog.setTitle("Alert Dialog");
alertDialog.setMessage("Welcome to AndroidHive.info");
alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Write your code here to execute after dialog closed
Toast.makeText(getApplicationContext(), "You clicked on OK", Toast.LENGTH_SHORT).show();
}
});
AlertDialog alertDialogMain = alertDialog.create();
alertDialogMain.show();
The youtube videos and google link that talks about dialog boxes and are similar to the code above. It even works for the people showing the demo on youtube.
But when I run it, the dialog box appears for half a second like I can see it. and then closes automatically. It appears and then its gone.
I have also tried to reboot my system. Nothing works.
Yes because you created it as it shows.
Why are you declaring a new variable as the below code if you already declared alertDialog above?
AlertDialog alertDialogMain = alertDialog.create();
alertDialogMain.show();
This problem is showing because you are declaring a new variable when you have already declared it. Make your code as below I maintained.
AlertDialog.Builder alertDialog = new AlertDialog.Builder(
ConsignmentConViewActivity.this);
alertDialog.setTitle("Alert Dialog");
alertDialog.setMessage("Welcome to AndroidHive.info");
alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Write your code here to execute after dialog closed
Toast.makeText(getApplicationContext(), "You clicked on OK", Toast.LENGTH_SHORT).show();
}
});
alertDialog.create();
alertDialog.show();
OR USE DIRECT WITHOUT DECLARING VARIABLE
new AlertDialog.Builder(this)
.setTitle("Alert Dialog")
.setMessage("Welcome to AndroidHive.info")
//.setIcon(R.drawable.ic_round_warning) // set your dialog icon if you want to show. JJust uncomment.
.setPositiveButton("OK", (dialogInterface, i) -> {
Toast.makeText(getApplicationContext(), "You clicked on OK", Toast.LENGTH_SHORT).show();
//dialogInterface.dismiss(); // for dismiss dialogbox
}).create().show();
Let me know if the problem not solved yet.
After grinding on it for almost the whole day, I realized that I have an Intent function right after the dialog box.
The intent function was switching to the next activity.
By the time the dialog would appear, the next code (which was the intent) would run. The activity would change and thats why I was getting that problem.
It works all fine when i removed the intent.
My code (in the question) is all correct and has no problem in it.
I'm running ussd code from my code. After it returns the results the user will press the ok button to close the the dialog. I want to detect when the user does this.
Let's assume that this is your dialog code:
AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.AppCompatAlertDialogStyle);
builder.setTitle("dialog's title");
builder.setMessage("dialogs's text");
builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//do stuff when user presses ok button
}
});
builder.setNeutralButton("CANCEL", null); //same here just add the listener
AlertDialog dialog = builder.create();
dialog.show();
//you can use neutralButton as well
I'm a beginner for Java as well as for Android Studio so, here my problem is: I had created a alert dialog window for an activity with positive button being "OK" and negative button being "No thanks". As shown in the code below.
if(Times==0) {
AlertDialog.Builder builder1 = new AlertDialog.Builder(this);
builder1.setIcon(android.R.drawable.ic_dialog_alert);
builder1.setTitle("Warning");
builder1.setMessage("Rooting of a phone may void your Warranty in most of the cases,so it is adviced to proceed at your own risk");
builder1.setCancelable(true);
builder1.setPositiveButton(
"OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Times += 1;
dialog.cancel();
}
});
builder1.setNegativeButton(
"No Thanks",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
}
It was going fine but now the catch is I want it to be displayed only once if the user clicks the "OK" button and don't want to show it again if the user clicked "OK". I had created a variable times in my class and initialised it to zero as shown below.
public class rootingRooting extends AppCompatActivity {
int Times=0;
and put the complete AlertDialog in the if loop and incremented it's value when the user clicked "OK" so that the loop may execute only once if the user clicked "OK", but it is of no use whenever I open the activity the alert box is being displayed inspite of clicking "OK". So, now the things i want to do happen is:
The alert box should not be displayed if the user once clicked "OK".
If the user clicked the "no Thanks" button, I want to take him to the home activity. So, how should I use the intent with the "no thanks" button?
Thank you.
You need to use SharedPreferenes to save data persistently, local variables will not help.
something like this:
EDIT As per your request, I have added a sample activity class to show the whole process. See the comments in between for more info
EDIT 2 See the code after //Second Edit comment
public class MainActivity extends AppCompatActivity {
SharedPreferences prefs;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//When the activity starts, we look into the shared prefs and get an int of name "ok_clicked" from it.
//0 will be the default value of the int if there is no int stored in sharedPreferences.
prefs = getSharedPreferences("myPrefs", MODE_PRIVATE);
int times = prefs.getInt("ok_clicked", 0);
//if the times value is 0, we will open the dialog, otherwise nothing happens
if (times==0){
openDialog();
}
}
//Read This comment First: We will create a Method, which create an alert Dialog.
private void openDialog(){
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle("Test").setMessage("Lorem ipsum dolor");
dialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//When OK button is clicked, an int with value of 1 will be saved in sharedPreferences.
prefs = getSharedPreferences("myPrefs", MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putInt("ok_clicked", 1);
editor.apply();
}
});
dialog.setNegativeButton("No Thanks", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//Second Edit: To open another acitivty on No Thanks Button
Intent intent = new Intent(MyActivity.this, HomeActivity.class);
startActivity(intent);
}
});
dialog.show();
}
}
I have developed an Android Service which will run in the background. The Service want to accept Yes or No confirmation from the user at an event(say when receive an SMS).
Its working fine; the Yes or No question will be shown to user. But i want the input from (press YES or NO) user without exit from the alert(exit by press on the Mobile Back button or Home button etc).
Please help me how it can be possible.
Below the code I am using;
AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
//ctx is the Context
builder.setTitle("Emergency!");
String txt="Do you want to accept?";
builder.setMessage(txt);
builder.setPositiveButton("YES", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
//Do something
}
});
builder.setNegativeButton("NO", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
///dialog.dismiss();
}
});
AlertDialog alert = builder.create();
alert.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
alert.show();
just add this in you AlertDialog
builder.setCancelable(false);
hope you are useing android.support.v7.app.AlertDialog;
You can disable back press while the dialog is shown (or possibly, send a "No" response when back button is pressed) with dialog.setOnKeyListener
dialog.setOnKeyListener(new Dialog.OnKeyListener() {
#Override
public boolean onKey(DialogInterface arg0, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
finish();
dialog.dismiss();
}
return true;
}
});
Unfortunately you can't override Home button press on Android. You can, however, react to an activity being sent to the background via Home button by implementing onUserLeaveHint in the activity hosting your dialog.
Edit: You may also want to disable dismissing the dialog by touching outside (which is the default behavior) by doing
dialog.setCanceledOnTouchOutside(false);
I am new to Android, i am writing a program where when a user clicks a button a Alert Dialog to appear. This alert dialog has 2 buttons, Yes and No. Upon clicking Yes/No, i need to sysout the response.
The code i have so far; Can someone help me add the Alert Dialog;
public class HelloWorldProjectActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.myFirstScreen);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v.getId()==(R.id.button1)){
System.out.println("first button clicked");
// I need a Alert Dialog to appear here, and that will have 2 buttons YES and NO, the users response should be printed to the console.
}
}
You cannot System.out.print().
There are several methods to display the result. One is to use Toast. It will briefly show a text message and then disappear.
new AlertDialog.Builder(this)
.setMessage("Are you sure?")
.setPositiveButton("Yes", new OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(TestAndroidActivity.this, "YES CLICKED",
Toast.LENGTH_LONG).show();
}
}).setNegativeButton("No", new OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(TestAndroidActivity.this, "NO CLICKED",
Toast.LENGTH_LONG).show();
}
}).show();
Modify your code as follows:
The activity class doesn't have to implement OnClickListener.
Thus, remove onClick() method
Go to the layout file, add an attribute android:onClick="click" in the button declaration.
Add public void click(View view) with the previous code.
First of all, there really isn't any system.out to print to in android. What you should try instead is printing to the log. For information on how to print to the log, check this out. To then see the activity of the log (including messages you printed to it), checkout the logcat.
Second, for information on creating an alert dialog, please view this documentation.