I am using a slightly different approach in order to keep the dialog open when a button is pressed:
AlertDialog.Builder builder = new AlertDialog.Builder(NewTableActivity.this);
builder.setTitle(R.string.addComponent);
final EditText titleText = new EditText(NewTableActivity.this);
titleText.setHint(R.string.title);
builder.setView(titleText);
builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener(){
#Override
public void onClick(DialogInterface dialogInterface, int i){
dialogInterface.cancel();
}
});
builder.setPositiveButton(R.string.ok, null);
final AlertDialog alertDialog = builder.create();
alertDialog.setOnShowListener(new DialogInterface.OnShowListener(){
#Override
public void onShow(DialogInterface dialogInterface){
Log.i("TEst", "Doung");
Button button = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
}
}
The dialog is opened a little lower (this works too), but the Log is never called
AlertDialog.Builder builder = new AlertDialog.Builder(NewTableActivity.this);
builder.setTitle(R.string.addComponent);
final EditText titleText = new EditText(NewTableActivity.this);
titleText.setHint(R.string.title);
builder.setView(titleText);
builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.cancel();
}
});
builder.setPositiveButton(R.string.ok, null);
final AlertDialog alertDialog = builder.create();
alertDialog.setOnShowListener(new DialogInterface.OnShowListener() {
#Override
public void onShow(DialogInterface dialogInterface) {
Log.e("TEst", "Doung");
Button button = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
}
});
alertDialog.show();
Related
I am very new to Java and I'm trying to keep things simple. Why doesn't this work? I have an XML layout with a EditText field and a Submit Button. I want to press it, an AlertDialog to pop up with the TextView of what I inputted into the EditText. What I tried keeps crashing:
//CustomAlertDialogPopUp
public void submitButton(View v) {
LayoutInflater inflater = getLayoutInflater();
View alertLayout = inflater.inflate(R.layout.alertXML, null);
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Alert");
alert.setView(alertLayout);
AlertDialog dialog = alert.create();
dialog.show();
EditText text1 = (EditText)findViewById(R.id.inputText);
TextView text2 = (TextView)findViewById(R.id.alertText);
String result = text1.getText().toString();
text2.setText(result);
}
Sorry if I sound stupid! Like I say, super new.
If you want to keep it simple, this will work:
For custom AlertDialog view you can refer to this question
public void submitButton(View v){
new AlertDialog.Builder(this)
.setTitle("Message")
.setMessage(text1.getText().toString())
.setPositiveButton("Dismiss", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
})
.show();
}
In my Android application I created a custom DialogFragment with a custom View but I want the Positive and Negative Button with transparent background.
So I saw some SO answers and i did something like this:
private class PersonalInfoDialogFragment extends DialogFragment {
#Override
public void onStart(){
super.onStart();
Button pButton = ((AlertDialog) getDialog()).getButton(DialogInterface.BUTTON_POSITIVE);
Button nButton = ((AlertDialog) getDialog()).getButton(DialogInterface.BUTTON_NEGATIVE);
pButton.setBackgroundColor(getResources().getColor(Color.TRANSPARENT));
nButton.setBackgroundColor(getResources().getColor(Color.TRANSPARENT));
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
LayoutInflater inflater = getActivity().getLayoutInflater();
final View view=inflater.inflate(R.layout.personalinformation_dialog, null);
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(R.string.personalinformation)
.setView(view)
.setPositiveButton(R.string.edit, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//Some code
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
// Create the AlertDialog object and return it
final AlertDialog dialog = builder.create();
//I also tried this: dialog.getButton(DialogInterface.BUTTON_POSITIVE).setBackgroundColor(Color.TRANSPARENT);
return dialog;
}
}
I don't get why I am facing NullPointerException. Any idea?
public Button pButton ,nButton ;
#Override
public void onStart(){
super.onStart();
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
LayoutInflater inflater = getActivity().getLayoutInflater();
final View view=inflater.inflate(R.layout.personalinformation_dialog, null);
pButton = ((AlertDialog) getDialog()).getButton(DialogInterface.BUTTON_POSITIVE);
nButton = ((AlertDialog) getDialog()).getButton(DialogInterface.BUTTON_NEGATIVE);
pButton.setBackgroundColor(getResources().getColor(Color.TRANSPARENT));
nButton.setBackgroundColor(getResources().getColor(Color.TRANSPARENT));
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(R.string.personalinformation)
.setView(view)
.setPositiveButton(R.string.edit, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//Some code
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
// Create the AlertDialog object and return it
final AlertDialog dialog = builder.create();
//I also tried this: dialog.getButton(DialogInterface.BUTTON_POSITIVE).setBackgroundColor(Color.TRANSPARENT);
return dialog;
}
}
When I write this code, it only shows dialog with one button, but not showing check boxes.
I don't know what's the problem.
private void showDialog(){
final ArrayList selectedItems = new ArrayList();
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
dialogBuilder.setTitle("asdasd");
dialogBuilder.setMessage("asdasd");
final String[] options = {"asd", "dsa","asd","aa"};
dialogBuilder.setMultiChoiceItems(options, null, new DialogInterface.OnMultiChoiceClickListener() {
#Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
if(isChecked){
selectedItems.add(which);
}else if(selectedItems.contains(which)){
selectedItems.remove(Integer.valueOf(which));
}
}
});
dialogBuilder.setNegativeButton("CANCEL",null);
AlertDialog dialog = dialogBuilder.create();
dialog.show();
};
Just see this link, and here is some code:
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(R.string.pick_color);
.setItems(R.array.colors_array, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// The 'which' argument contains the index position
// of the selected item
}
});
return builder.create();
}
Hope you can solve your problem.
So I have a button with an on click listener. It's supposed to open a dialog window with options... but it doesn't.
Anyone know what the problem is?
seeTexts.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Pick a color");
builder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
}
});
AlertDialog alert = builder.create();
}
});
EDIT (UPDATED CODE):
seeTexts.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Pick a color");
builder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
}
});
AlertDialog alert = builder.create();
alert.show();
}
});
You just don't call the show method of your AlertDialog object
I have an AlertDialog having the text "For more information, Please Call 1-800-200-1000".
Here is my Code for the Alert Dialog Display while clicking on the ListView Item :
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
final SpannableString s = new SpannableString("1-800-200-1000");
Linkify.addLinks(s, Linkify.ALL);
ListView.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
builder.setMessage("For more information, Please Call "+s)
.setCancelable(true)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
AlertDialog alert = builder.create();
alert.show();
}});
Here i wish to hyperlink the "1-800-200-1000" and while clicking on this, another dialog for call function should be implemented.
Here is my AlertDialog for Call Function:
final Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Do you want to Call?");
builder.setCancelable(false);
builder.setPositiveButton("Call", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent dial = new Intent();
dial.setAction("android.intent.action.DIAL");dial.setData(Uri.parse("tel:"+ Phone));
startActivity(dial);
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Toast.makeText(getApplicationContext(),"Activity will continue",Toast.LENGTH_LONG).show();
dialog.cancel();
}
});
AlertDialog dialog = builder.create();
dialog.show();
Please help me in two problems:
1.How to hyperlink the Phone number in the First Dialog?
2.how to embed the Call AlertDialog while clicking the Hyperlinked Phone Number?
Thanks in advance.
try this one:
Spannable spans = (Spannable) text;
ClickableSpan clickSpan = new ClickableSpan() {
#Override
public void onClick(View widget) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://"+ url));
}
public void updateDrawState(TextPaint ds) {
//override link-centric text appearance
}
};
int index = (text.toString()).indexOf(url);
spans.setSpan(clickSpan, index, url.length() + index,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);