Web Link using DialogFragment [duplicate] - java

This question already has answers here:
How can I open a URL in Android's web browser from my application?
(41 answers)
Closed 8 years ago.
I have this dialog fragment, but i'm trying to get it so that when a user clicks on the "My Other Apps" Button they get redirected to a web address. Is there any way of doing this.
Any help is appreciated
Thanks
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// TODO Auto-generated method stub
AlertDialog.Builder theDialog = new AlertDialog.Builder(getActivity());
theDialog.setTitle("About");
String d1 = "Thanks For Downloading! ";
String d2 = "Made by ME";
theDialog.setMessage(d1 +"\n"+ d2 );
theDialog.setPositiveButton("My Other Apps", new DialogInterface.OnClickListener(){
#Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getActivity(), "Clicked My Other Apps",
Toast.LENGTH_SHORT).show();
}
});
theDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener(){
#Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getActivity(), "Clicked Cancel",
Toast.LENGTH_SHORT).show();
}
});
return theDialog.create();
}
}

#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// TODO Auto-generated method stub
AlertDialog.Builder theDialog = new AlertDialog.Builder(getActivity());
theDialog.setTitle("About");
String d1 = "Thanks For Downloading! ";
String d2 = "Made by ME";
theDialog.setMessage(d1 +"\n"+ d2 );
theDialog.setPositiveButton("My Other Apps", new DialogInterface.OnClickListener(){
#Override
public void onClick(DialogInterface dialog, int which) {
String url = "http://www.example.com";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}
});
theDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener(){
#Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getActivity(), "Clicked Cancel",
Toast.LENGTH_SHORT).show();
}
});
return theDialog.create();
}
}

Do this on your button click:
#Override
public void onClick(DialogInterface dialog, int which) {
String url = "http://www.example.com";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}

Related

How to give Choice of Numbers to Call using Intent.ACTION_CALL?

I am trying to give the option of two numbers on a click of a button to make a call on the number directly but when I am selecting one of the numbers it is going to the dialer screen but "Ljava.lang.CharSequence" is written there and a dialog box with a message service is not supported pops up.
Here is my code:-
phone.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final CharSequence numbers[] = new CharSequence[] {"02212345678","+14356789809"};
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setTitle("Select number to call");
builder.setItems(numbers, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Intent callIntent = new Intent(Intent.ACTION_CALL);
String call = "tel:" +numbers;
callIntent.setData(Uri.parse(call));
startActivity(callIntent);
}
});
builder.show();
}
});
Change
String call = "tel:" +numbers;
To
String call = "tel:" +numbers[which];
phone.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String number = "02212345678";
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setTitle("Select number to call");
builder.setItems(number , new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Intent callIntent = new Intent(Intent.ACTION_CALL);
String call = "tel:" +number ;
callIntent.setData(Uri.parse(call));
startActivity(callIntent);
}
});
builder.show();
}
});
This is the complete code for giving choice of numbers and calling the selected number on a Button Click. I hope this will help other members too.
phone.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final CharSequence numbers[] = new CharSequence[] {"+1xxxxxxxxx","+1xxxxxxxxxx"};
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setTitle("Select number to call");
builder.setItems(numbers, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Intent callIntent = new Intent(Intent.ACTION_CALL);
String call = "tel:" +numbers[which];
callIntent.setData(Uri.parse(call));
startActivity(callIntent);
// the user clicked on colors[which]
}
});
builder.show();
}
});

intent in alart dialog [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
intent in alart dialog how pass in next intent
alertDialogBuilder
.setCancelable(false)
.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
String title = editTextTitle.getText().toString().trim();
String r=android.get(i).getPassword();
if(title.equals(r)){
view.getContext().startActivity(new Intent(activity,DetailView.class));
Toast.makeText(view.getContext()," user name " , Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(view.getContext()," user name not valid " , Toast.LENGTH_LONG).show();
}
// sendMessage(user.getId(), title, message);
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
});
}
you need to use activity or context, for example, please refer below code
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(activity)
.setCancelable(false)
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
activity.startActivity(new Intent(activity, DetailView.class));
}
}).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
you can get activity or context from the contractor.
Change this part of the code:
view.getContext().startActivity(new Intent(activity,DetailView.class));
To this:
Intent intent = new Intent(FirstActivity.this,Screen2Activity.class);
startActivity(intent);
Full code:
alertDialogBuilder
.setCancelable(false)
.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
String title = editTextTitle.getText().toString().trim();
String r=android.get(i).getPassword();
if(title.equals(r)){
Intent intent = new Intent(FirstActivityName.this,Screen2ActivityName.class);
startActivity(intent);
Toast.makeText(view.getContext()," user name " , Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(view.getContext()," user name not valid " , Toast.LENGTH_LONG).show();
}
// sendMessage(user.getId(), title, message);
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
});
}

I want to dismiss the dialog box as soon as it is connected to internet

Here I want to show two dialog boxes...one for if there is net connection available and other if there is no connection..but i want that when one dialog box is shown, the other dialogue box should be dismissed .......dismiss() is not working in this case....and somehow if I use AlertDialog instead of AlertDialog.Builder to use dismiss(), then i am not able give setPositive, setNegative and setNeutral buttons....any help will be appreciated.......
BroadcastReceiver br;
#Override
protected void onCreate(Bundle savedInstanceState) {
...........//
getStarted();
}
private void getStarted() {
if (br == null) {
br = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
...............//
if (state == NetworkInfo.State.CONNECTED) {
AlertDialog.Builder builder1 = new AlertDialog.Builder(context);
builder1.setCancelable(false);
builder1.setTitle("Connected");
builder1.setMessage("Online");
builder1.setNeutralButton("Exit", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//
}
});
builder1.show();
}
else {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setCancelable(false);
builder.setTitle("No Internet ");
builder.setMessage("Offline");
builder.setNeutralButton("Exit", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//
}
});
builder.show();
}
}
};
final IntentFilter if = new IntentFilter();
if.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
getActivity().registerReceiver(br, if);
}
}
}
Dismiss Your dialog if NetworkInfo.State.CONNECTED is connected,Please change builder1.show(); into builder1.dismiss();
if (state == NetworkInfo.State.CONNECTED) {
AlertDialog.Builder builder1 = new AlertDialog.Builder(context);
builder1.setCancelable(false);
builder1.setTitle("Connected");
builder1.setMessage("Online");
builder1.setNeutralButton("Exit", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//
}
});
builder1.dismiss();
}
Use broadcast receiver to react when the connection is changed with intent filter android.net.ConnectivityManager.CONNECTIVITY_ACTION. So, you can do your stuffs when the receiver receive the intent (or there connection is changed). See here.

android alert box with multiple options, how to use those options?

I am a beginner, so if anyone would help me out. I created a list in the dialogue box , now how do i use those options? Like click one and it does something , click another and it does something else.
CharSequence features[] = new CharSequence[] {"Save", "Send", "Something", "Something"};
AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);
alertDialog.setTitle("Options");
alertDialog.setItems(features, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this,"Eta chu ma aile",
Toast.LENGTH_LONG).show();
}
});
alertDialog.show();
return true;
}
If you know exact position of every item, just compare it with which param.
public void onClick(DialogInterface dialog, int which) {
if (which == 0) {
// handle "Save" option
} else if (which == 1) {
// handle "Send" option
} ...
}
You can use following code:
Somewhere in another function:
String title = "My Alert Box";
String msg = "Choose Option";
alertfunc(title,msg);
The main alert function:
private void alertfunc(String title, String msg) {
if (title.equals(TASK_VIEW_PROFILE)) {
new AlertDialog.Builder(MainActivity.this)
.setTitle(title)
.setMessage(msg)
.setPositiveButton("Save",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog,
int which) {
//Do something
}
})
.setNegativeButton("Send",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog,
int which)
{
//Do something
}
}).create().show();
.setNegativeButton("Something",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog,
int which)
{
//Do something
}
}).create().show();
//...and so on
}
}

Dialog only first option works

I'm trying to make a SingleChoiceItems dialog activated from a listview onItemLongCLick which has 3 options via radiobuttons 1.View Profile 2.Send Message 3.Delete Friends and from the way I have it set up,only the first option toast works on all 3 options.
How would I make it that options 2 and 3 does their actions instead of doing option 1's actions?
Here is the code to my dialog,
#Override
public Dialog onCreateDialog(int id) {
String[] items = { "View Profile", "Send Message", "Remove Friend" };
final DBAdapter db = new DBAdapter(this);
db.open();
switch (id) {
case 0:
return new AlertDialog.Builder(this)
.setIcon(R.drawable.icon)
.setTitle("Select a option")
.setSingleChoiceItems(items, id,
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog,
int which) {
int choice = 0;
// TODO Auto-generated method stub
mChoice = choice;
}
})
.setPositiveButton("OK", new OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int choice) {
// TODO Auto-generated method stub
if (mChoice == 0) {
Toast.makeText(getBaseContext(), "Test 1",
Toast.LENGTH_SHORT).show();
} else if (mChoice == 1) {
Toast.makeText(getBaseContext(), "Test2",
Toast.LENGTH_SHORT).show();
} else if (mChoice == 2) {
TextView friends = (TextView) findViewById(R.id.textview_friends);
String deletedfriend = friends.getText()
.toString();
db.DeleteFriends(deletedfriend);
Toast.makeText(getBaseContext(),
"Friend Removed", Toast.LENGTH_SHORT)
.show();
}
}
}
)
.setNegativeButton("Cancel", new OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int choice) {
}
})
.create();
}
return null;
}
Remove choice = 0;
.setSingleChoiceItems(items, id,
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog,
int which) {
mChoice = which;
}
})

Categories