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();
}
});
Related
I need some help with my on click in my adapter class.
This is the code that I want to run when an item is clicked. connectOrDisconnectUser()
private void connectOrDisconnectUser(final int position) {
final AlertDialog.Builder builder = new AlertDialog.Builder(
RelationshipRemoved.this);
builder.setMessage("Do you want to un tag " + usersInfo.get(position).get(
RelationshipRemoved.likedOne))
.setCancelable(false)
.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
//do something
public void onClick(DialogInterface dialog, int randomvar) {
Log.d("OkHttpRemove", "getting removing!");
String username = (globalInt);
String UT = usersInfo.get(position).get(
RelationshipRemoved.likedOne);
String type = "remove";
BackgroundWorker backgroundWorker = new BackgroundWorker(getApplicationContext());
backgroundWorker.execute(type, username, UT);
startActivity(new Intent(RelationshipRemoved.this, MainActivity.class));
Toast.makeText(context, "UnTagged!",
Toast.LENGTH_SHORT).show();
}
})
.setNegativeButton("No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
dialog.cancel();
}
});
final AlertDialog alert = builder.create();
alert.show();
}
The file that holds the adapter is called AdapterClass and the class that uses the adapter class is Called RelationshipRemoved. When I try to write this code. I get an error on RelationshipRemoved.this on this line RelationshipRemoved.this);, startActivity and RelationshipRemoved.this on this line startActivity(new Intent(RelationshipRemoved.this, MainActivity.class));
What's wrong?
I have a button that open a dialog for change a number. The button is in activity, and generate a new class names dialogs, for storage different dialogs.
Dialog consigna: Dialogs.class
public String consigna(){
AlertDialog.Builder alert = new AlertDialog.Builder(ctxt);
alert.setTitle("Nueva temperatura");
alert.setMessage("Agrega una nueva temperatura");
final EditText input = new EditText(ctxt);
input.setInputType(InputType.TYPE_CLASS_NUMBER);
input.setRawInputType(InputType.TYPE_CLASS_NUMBER);
alert.setView(input);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
data = input.getText().toString();
}
});
alert.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
//Put actions for CANCEL button here, or leave in blank
}
});
alert.show();
return data;
}
on click button: MainActivity
//I need change these two values:
final public double temperatura = 200.3;
String newData;
...
public void onClick(View v) {
switch (v.getId()){
case R.id.tempConsigna:
dialog = new Dialogs(MainActivity.this);
String data = dialog.consigna();
newData = data;
break;
...//other cases...
The issue is in newData = data; I doesn't have a data, because the dialog not are closed. The dialog work in other thread,no?
How to change newData var with the dialog result? It is posible into a dialogs class?
You need a callback method implemented via an interface:
public String consigna(final OnConfirm confirm){
final String[] data = new String[1];
AlertDialog.Builder alert = new AlertDialog.Builder(activity);
alert.setTitle("Nueva temperatura");
alert.setMessage("Agrega una nueva temperatura");
final EditText input = new EditText(activity);
input.setInputType(InputType.TYPE_CLASS_NUMBER);
input.setRawInputType(InputType.TYPE_CLASS_NUMBER);
alert.setView(input);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
data[0] = input.getText().toString();
confirm.onConfirm(data[0]);
}
});
alert.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
//Put actions for CANCEL button here, or leave in blank
}
});
alert.show();
return data[0];
}
public interface OnConfirm {
void onConfirm(String s);
}
And in your Main Activity
Dialogs dialog = new Dialogs(MainActivity.this);
dialog.consigna(new Dialogs.OnConfirm() {
#Override
public void onConfirm(String s) {
Log.d("data", s);
}
});
It will not return result as you want but after click on dialog ok button then access data variable. no need to return.
My activity has an accept button witch sends intent to parent through onClickOkayButton method. I have overriden the onBackPressed() method so that it shows an AlertDialog asking the user if he really wishes to leave or if he wants to save the preferences, witch does the exact same action as the accept button.
Is it possible to merge both onClick methods into one so I don't need to copy/paste in case of editing one of those, even if both methods use different parameters?
public void onClickOkayButton(View view) {
// THIS IS THE ACCEPT BUTTON
EditText editText = (EditText)findViewById(R.id.surveyadd_name_edittext);
String title = editText.getText().toString();
if (!(title.matches("")) || !(title.isEmpty()) || !(title.equals("")) ) {
Intent intent = new Intent();
intent.putExtra("title", title);
setResult(RESULT_OK, intent);
finish();
} else {
Toast.makeText(this, R.string.surveyadd_warn_notitle, Toast.LENGTH_SHORT).show();
}
}
#Override
public void onBackPressed() {
if (adapter.getCount() != 0) {
showAlertDialog();
} else {
super.onBackPressed();
}
}
public void showAlertDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage(R.string.surveyadd_warn_back_message);
builder.setNegativeButton(R.string.surveyadd_warn_back_save, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// THIS WAS COPIED FROM THE ACCEPT BUTTON
EditText editText = (EditText)findViewById(R.id.surveyadd_name_edittext);
String title = editText.getText().toString();
if (!(title.matches("")) || !(title.isEmpty()) || !(title.equals("")) ) {
Intent intent = new Intent();
intent.putExtra("title", title);
setResult(RESULT_OK, intent);
finish();
} else {
Toast.makeText(context, R.string.surveyadd_warn_notitle, Toast.LENGTH_SHORT).show();
}
}
});
// UP TO HERE
builder.setPositiveButton(R.string.surveyadd_warn_back_erase, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
finish();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
You won't be able to use the same callback methods because they are from different interfaces.
What you should do is move all the common code into a third method and just call it from both click handlers like below:
private void setResultAndFinish() {
EditText editText = (EditText)findViewById(R.id.surveyadd_name_edittext);
String title = editText.getText().toString();
if (!TextUtils.isEmpty(title)) ) {
Intent intent = new Intent();
intent.putExtra("title", title);
setResult(RESULT_OK, intent);
finish();
} else {
Toast.makeText(context, R.string.surveyadd_warn_notitle, Toast.LENGTH_SHORT).show();
}
}
And then the handlers:
public void onClickOkayButton(View view) {
setResultAndFinish();
}
#Override
public void onClick(DialogInterface dialog, int which) {
setResultAndFinish();
}
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);
}
public void onClick(View v) {
if(a){
Intent i = new Intent();
if(type.equals("x")){
showErrorAlert("string");
i = new Intent(Activity1.this, Activity2.class);
i.putExtra("label", var);
i.putExtra("label1", var2);
startActivity(i);
}
else if(type.equals("y")){
i = new Intent(Activity1.this, Activity3.class);
i.putExtra("label2", var3);
}
//startActivity(i);
}
else startActivity(new Intent(Activity1.this, Activity4.class));
}
});
private void showErrorAlert(String errorMsg){
AlertDialog errorDialog = new AlertDialog.Builder(this).create();
errorDialog.setTitle("title");
errorDialog.setMessage(errorMsg);
errorDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "Okay", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
errorDialog.show();
}
So what happens is the error alert shows, but it will immediately close and the next activity will appear. I want the activity to not start until after "Okay" is selected.
Try this
public void onClick(View v) {
if(a){
Intent i = new Intent();
if(type.equals("x")){
showErrorAlert("string");
}
else if(type.equals("y")){
i = new Intent(Activity1.this, Activity3.class);
i.putExtra("label2", var3);
}
//startActivity(i);
}
else startActivity(new Intent(Activity1.this, Activity4.class));
}
});
private void showErrorAlert(String errorMsg){
AlertDialog errorDialog = new AlertDialog.Builder(this).create();
errorDialog.setTitle("title");
errorDialog.setMessage(errorMsg);
errorDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "Okay", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Intent i = new Intent(Activity1.this, Activity2.class);
i.putExtra("label", var);
i.putExtra("label1", var2);
startActivity(i);
}
});
errorDialog.show();
}
Move your startActivity(i); to inside if (or) else (or) both blocks depending on where you need.
Place startActivity(i); inside the onClick method in your dialog like this:
private void showErrorAlert(String errorMsg){
AlertDialog errorDialog = new AlertDialog.Builder(this).create();
errorDialog.setTitle("title");
errorDialog.setMessage(errorMsg);
errorDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "Okay", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
startActivity(i);
}
});
errorDialog.show();
}