How to use phone number in alert dialog android? - android

I want to use default phone number and pass that in alert dialog.How it's possible. I have tried,but it force close.Please solve my issue. Thanks in advance. My coding is as follows:
case R.id.menu_settings:
String phoneNo ="123456789";
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setMessage("Do you want to call us?"+phoneNo);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
String uri = "123456789";
public void onClick(DialogInterface dialog, int whichButton) {
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse(uri));
startActivity(intent);
}
});
alert.setNegativeButton("Ok", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
finish();
}
});
alert.show();
return true;
}
return false;
}

The Uri should be in the format tel:########
String uri = "tel:123456789";

This is happening because at least 1 of 2 reasons.
You do not have the permission <uses-permission android:name="android.permission.CALL_PHONE"/> in your manifest
Your Uri is in an incorrect format. Change it from
String uri = "123456789" to
String uri = "tel:123456789"

Related

EditText dialog input always null (Android)

The following code runs in the onClick method for a button and brings up a dialog box. I can enter text and press ok but my String filename, as shown in the Log.d, is always null. I do not understand why.
How do I get the text inputted in the dialog box to save?
My code runs in an activity(no fragment) and String filename and EditText input are both class member variables.
// Get filename from user
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle("Enter filename");
// EditText to get user input
this.input = new EditText(this);
dialog.setView(input);
dialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
filename = input.getText().toString();
}
});
dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// cancelled
}
});
dialog.show();
Log.d(LOG_TAG, "Filename is : " + filename);
I based the code on android prompt user's input using a dialog but my issue is different.
you try to Log the string before you actually input data into it.
try to put the log inside the positive onClick and I think you will see that it does save.
like so:
// Get filename from user
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle("Enter filename");
// EditText to get user input
this.input = new EditText(this);
dialog.setView(input);
dialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
filename = input.getText().toString();
Log.d(LOG_TAG, "Filename is : " + filename);
}
});
dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// cancelled
}
});
dialog.show();

Insert LongPress from listview

I tried to introduce long press.
this time using a alertdialog to the 2 options there, play and delete.
so I would like to offer longpress for delete and touch for play. How can I do?
code:
#Override
public void onVideoSelected(final String uri, String mimeType) {
AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.AppCompatAlertDialogStyle);
builder.setTitle("Select");
// builder.setMessage("Lorem ipsum dolor ....");
builder.setItems(new CharSequence[]
{getString(R.string.play_video), getString(R.string.remove_video)},
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case 0:
Intent intent = new Intent(Main2Activity.this, MainActivity.class);
intent.putExtra("url", uri);
startActivity(intent);
break;
case 1:
File file = new File(uri);
file.delete();
Main2Activity.this.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(new File(uri))));
break;
}
}
});
builder.create().show();
}

alertdialog redirection to url

I used the android alertdialog in order to redirect to a url, the redirection should go according to user choice, here is the code:
final CharSequence[]stringArray = {"1" ,"2" , "3"};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Press selected name");
builder.setItems(stringArray, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
String st = "https://www.youtube.com/watch?v=x9QyKNQ0uVc";
String st2 = "https://www.youtube.com/";
if (stringArray.toString().equals("1")) {
Intent intent = new Intent(Intent.ACTION_VIEW,
Uri.parse(st));
startActivity(intent);
}
else if (stringArray.toString().contains("2")) {
Intent intent = new Intent(Intent.ACTION_VIEW,
Uri.parse(st2));
startActivity(intent);
}
}
});
AlertDialog alert = builder.create();
alert.show();
but when I click 1 or 2 there is no redirection to the url
what is wrong with the code?
You are checking your arrays items which is not valid way to implement. You need to check for selected item's id which you can get from onclick method only.
As the array count starts from "0" so can check your selected item with "0 to 2" where your 0 will be considered as "1" selected and so on.
Check out below code:
final CharSequence[] stringArray = { "1", "2", "3" };
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Press selected name");
builder.setItems(stringArray, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
String st = "https://www.youtube.com/watch?v=x9QyKNQ0uVc";
String st2 = "https://www.youtube.com/";
if (item == 0)
// if (stringArray.toString().equals("1"))
{
Intent intent = new Intent(Intent.ACTION_VIEW, Uri
.parse(st));
startActivity(intent);
} else if (item == 1)
// else if (stringArray.toString().contains("2"))
{
Intent intent = new Intent(Intent.ACTION_VIEW, Uri
.parse(st2));
startActivity(intent);
}
}
});
AlertDialog alert = builder.create();
alert.show();
Please use following code:-
final CharSequence[] stringArray = { "1", "2", "3" };
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Press selected name");
builder.setItems(stringArray, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int position) {
String st = "https://www.youtube.com/watch?v=x9QyKNQ0uVc";
String st2 = "https://www.youtube.com/";
if (position == 0) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(st));
startActivity(intent);
}
else if (position == 1) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(st2));
startActivity(intent);
}
else if (position == 2) {
// write some code
}
}
});
AlertDialog alert = builder.create();
alert.show();

making phone call from DialogBox in Android

I have one DialogBox with 2 buttons. If we click the +ve button, it attempts to open the Dialing Pad; else if we click the -ve button it closes the dialog. When I click the +ve button it shows the null exception. If the same code executes without the DialogBox, it is fine.
Here is my code:
callDialog.setPositiveButton("Call Now", new android.DialogInterface.
OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Intent dial = new Intent();
dial.setAction("android.intent.action.DIAL");
try {
dial.setData(Uri.parse("tel:9951037343"));
startActivity(dial);
} catch (Exception e) {
Log.e("Calling", "" + e.getMessage());
}
}
}
I've given permissions in the manifest file as <uses-permission android:name="android.permission.CALL_PHONE"/>
Try this..
Intent call = new Intent(Intent.ACTION_DIAL);
call.setData(Uri.parse("tel:phonenumber");
startActivity(call);
Remove the #Overrride line.
Like this:
callDialog.setPositiveButton("Call Now", new android.DialogInterface.
OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent dial = new Intent();
dial.setAction("android.intent.action.DIAL");
try {
dial.setData(Uri.parse("tel:9951037343"));
startActivity(dial);
} catch (Exception e) {
Log.e("Calling", "" + e.getMessage());
}
}
}
----UPDATE-----
Since you havent posted any logcat, its hard to know where it crashes. Try this block:
AlertDialog.Builder callDialog = new AlertDialog.Builder(this);
callDialog.setTitle("My title");
callDialog.setMessage("My message");
callDialog.setPositiveButton("Call", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent dial = new Intent();
dial.setAction("android.intent.action.DIAL");
try {
dial.setData(Uri.parse("tel:9951037343"));
startActivity(dial);
} catch (Exception e) {
Log.e("Calling", "" + e.getMessage());
}
}
});
callDialog.setNegativeButton("Cancel", null);
callDialog.show();
Try the following---
Intent dial = new Intent(Intent.ACTION_DIAL);
//dial.setAction("android.intent.action.DIAL");
try {
dial.setData(Uri.parse("tel:9951037343"));
startActivity(dial);
} catch (Exception e) {
Log.e("Calling", "" + e.getMessage());
}
startActivity(dial);
Use
Intent callIntent = new Intent(Intent.ACTION_CALL);
instead of this
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setAction("android.intent.action.DIAL"); // but this line is an optional one
callIntent.setData(Uri.parse("tel:044 6000 6000"));
startActivity(callIntent);
use this code......
String number = "tel:" + phonenumber;
Intent intent = new Intent(Intent.ACTION_CALL, Uri
.parse(number));
startActivity(intent);
give permission in manifest: <uses-permission android:name="android.permission.CALL_PHONE" />
updated.....
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.menu_icon_pager).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
new AlertDialog.Builder(MainActivity.this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle("Exit")
.setMessage(
"Texting from dialog")
.setNegativeButton(android.R.string.cancel, null)
.setPositiveButton(android.R.string.ok,
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog,
int which) {
// Exit the activity
String number = "tel:"+phonenumber ;
Intent intent = new Intent(Intent.ACTION_CALL, Uri
.parse(number));
startActivity(intent);
}
})
.show();
}
});
}
Actually I called the AlertDialog from another Activity,so I need the context of called
Activity's Context reference.So I passed the Context as a parameter to Called Activity
Here is Called Activity's code
if(position == 1)new CalledByActivity().dailingDialogBox(param1,
param2,context);//Here I'm passing the context as param
Here CalledBy activity's implemented function
public void dailingDialogBox(final String param1,String param2,final Context context){
Here I implemented in +ve button click
Intent dail = new Intent(Intent.ACTION_DIAL);
dail.setData(Uri.parse("tel:"+station_Number));
context.startActivity(dail);//Here the "context" is reference of called Activity's context

How to show sequential dialog popups?

I'm having some trouble with displaying multiple popups. Right now I have an AlertDialog that pops up with an EditView for the user to put in the name of the file they want to make, which I would then pass into a File object and then a Writer and a new Dialog is supposed to pop up asking the user if they want to launch the music player.
However, as things are now, after I press 'Ok' on the first AlertDialog, absolutely nothing happens. I'm not sure what I'm doing wrong. Any help? Here is my code.
//naming the playlist
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Exporting Playlist");
alert.setMessage("Enter the name of the playlist!");
// Set an EditText view to get user input
final EditText input = new EditText(this);
alert.setView(input);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
name = input.getText().toString() + ".m3u";
popup = true;
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Canceled.
}
});
alert.show();
//after the playlist is named, put songs into file
if (popup){
popup = false;
final File list = new File(mp3folderPath + name);
FileWriter writer;
BufferedWriter write;
ArrayList<String> playlist = new ArrayList<String>();
Log.d("poo", "mAdapter count: "+mAdapter.getCount());
for (int i=0; i < mAdapter.getCount(); i++) {
playlist.add(mAdapter.getItem(i));
}
Log.d("poo", playlist.toString());
//write the songs to the m3u playlist
writer = new FileWriter(list);
write = new BufferedWriter(writer);
for (int i = 0; i<playlist.size(); i++){
String[] name = playlist.get(i).split(" : ");
Log.d("poo", name[0]);
write.append(name[0]+"\n");
}
write.close();
//popup window
CharSequence choices[] = new CharSequence[] {"Launch Music Player", "Quit"};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Exported playlist!");
builder.setItems(choices, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
if (which == 0) {
Intent intent = new Intent(MediaStore.INTENT_ACTION_MUSIC_PLAYER);
startActivity(intent);
finish();
}
else {
finish();
}
}
});
builder.show();
}
}
To show sequential popup, the conditions and code for consecutive popup(s) would have to be reachable from one to the other.
AlertDialog1 has to contain the code which would show AlertDialog2...
Try something like this:
//naming the playlist
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Exporting Playlist");
alert.setMessage("Enter the name of the playlist!");
// Set an EditText view to get user input
final EditText input = new EditText(this);
alert.setView(input);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
//check if the name is not null
name = input.getText().toString() + ".m3u";
//Now instead of popup = true;
//call func to name the playlist and next dialog
callNextDialog();
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Canceled.
}
});
alert.show();
//after the playlist is named, put songs into file
// if (popup){
// popup = false;
// }
public void callNextDialog(){
final File list = new File(mp3folderPath + name);
FileWriter writer;
BufferedWriter write;
ArrayList<String> playlist = new ArrayList<String>();
Log.d("poo", "mAdapter count: "+mAdapter.getCount());
for (int i=0; i < mAdapter.getCount(); i++) {
playlist.add(mAdapter.getItem(i));
}
Log.d("poo", playlist.toString());
//write the songs to the m3u playlist
writer = new FileWriter(list);
write = new BufferedWriter(writer);
for (int i = 0; i<playlist.size(); i++){
String[] name = playlist.get(i).split(" : ");
Log.d("poo", name[0]);
write.append(name[0]+"\n");
write.close();
//popup window
CharSequence choices[] = new CharSequence[] {"Launch Music Player", "Quit"};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Exported playlist!");
builder.setItems(choices, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
if (which == 0) {
Intent intent = new Intent(MediaStore.INTENT_ACTION_MUSIC_PLAYER);
startActivity(intent);
finish();
}
else {
finish();
}
}
});
builder.show();
}
}
AlertDialog.show() doesn't wait for the dialog to go away. It returns immediately. That means ALL of the logic of what to do after the user makes a choice has to go in the onClick function of the dialog's positive button.
Basically, everything in your if(popup) code needs to be in the onClick handler

Categories

Resources