How can I make a list on AlertDialog which include some radiobuttons in that , and when I clicked on items of the AlertDialog , do something like toast .
Try this:
//Setup the alert builder
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Choose an animal");// add a radio button list
String[] animals = {"horse", "cow", "camel", "sheep", "goat"};
int checkedItem = 1; // cow
builder.setSingleChoiceItems(animals, checkedItem, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//Here you can implement Toast Message
}
});
//Add OK and Cancel buttons
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//Handle when user clicks OK
}
});
builder.setNegativeButton("Cancel", null); //Create and show the alert dialog
AlertDialog dialog = builder.create();
dialog.show();
Let me know if that helped :)
Related
I want to add one more button that if someone presses the back button two times the alert dialog box should disappear or at least get a close dialog box X button on the top right side of the alert box. I have already used setpossitivebutton and setnegativebutton.
public void onBackPressed()
{
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle(R.string.app_name);
builder.setIcon(R.mipmap.ic_launcher);
builder.setMessage("Wanna Exit?")
.setCancelable(false)
.setPositiveButton("Exit", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
finish();
}
})
.setNegativeButton("Share on Whatsapp", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent sendIntent = new Intent(); sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT,"simple text");
sendIntent.setType("text/plain");
sendIntent.setPackage("com.whatsapp");
startActivity(sendIntent);
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
Try this code :
AlertDialog alertDialog;
public void onBackPressed() {
if(alertDialog == null || !alertDialog.isShowing()){
alertDialog = = new AlertDialog.Builder(context)
.setTitle("Delete entry")
.setMessage("Are you sure you want to delete this
entry?")
.setPositiveButton(android.R.string.yes, new
DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// continue with delete
}
})
.setNegativeButton(android.R.string.no, new
DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// do nothing
}
})
.setIcon(android.R.drawable.ic_dialog_alert)
.show();
}else{
alertDialog.cancel();
}
}
Hope it will help you :)
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();
}
After installing the app, an alertDialog will pop-out. I wanted it to have a edit text field asking for the name of the user. After answering, it will never show again. Then afterwards, every time I open the app, another alertDialog will pop-out like a greeting to the user.
public class MainActivity extends Activity {
final Context context = this;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AlertDialog.Builder alert = new AlertDialog.Builder(context);
alert.setTitle("Welcome");
alert.setMessage("Enter Your Name Here");
final EditText input = new EditText(context);
alert.setView(input);
alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
});
AlertDialog alertDialog = alert.create();
alertDialog.show();
AlertDialog.Builder a_builder = new AlertDialog.Builder(MainActivity.this);
a_builder.setMessage("Mabuhay!")
.setCancelable(true)
.setPositiveButton("ok", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog alert = a_builder.create();
alert.show();
}
}
After the user has entered his name on the first open of your app, you can write the given username into the SharedPreferences of your Application.
When a user now opens the app at any other time, you can simply check if you have an entry in the sharedpreferences of your application, and greet the user with the appropriate user name.
//instance variable in the Mainactivity.class
private boolean showMessage = true;
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
String username = sharedPref.getString("username", null);
if(username == null){
//first time user - ask for username
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("username", enteredName);
editor.commit();
showMessage = false;
} else if(showMessage) {
showMessage = false;
//greet
AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this);
alert.setMessage("Hello " + username + "!")
.setCancelable(true)
.setPositiveButton("ok", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
alert.create().show();
}
You can use two methods for do this.
The first is by using the alert dialog like your example, you can find here how use dialogs.
Dialogs by Android Developer.
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage(R.string.dialog_message)
.setTitle(R.string.dialog_title);
AlertDialog dialog = builder.create();
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// Add the buttons
builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User clicked OK button
}
});
builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
// Set other dialog properties
...
// Create the AlertDialog
AlertDialog dialog = builder.create();
Or you can create an anctivity dialog by setting in the manifest the dialog theme, and you can find it here.
for further explanations let me know!
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);
I created a Button,While i Click that Button a new Dialog Box is Displayed.In that dialog Box i have set icon and text.text is Displayed but icon(image) is not displayed in dialog Box.
Here my Coding
Button btnteam_westernbulldogs=new Button(this);
btnteam_westernbulldogs.setId(team_westernbulldogsid);
btnteam_westernbulldogs.setBackgroundResource(R.drawable.team_westernbulldogs);
public void onClick(View v){
createbtnteam_westernbulldogs();
}
});
public void createbtnteam_westernbulldogs()
{
AlertDialog alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setIcon(R.drawable.team_westernbulldogs);
alertDialog.setMessage("What kind of Banner do you Want to Create?");
alertDialog.setButton("Text", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
createText();
}
alertDialog.setIcon(R.drawable.icon);
alertDialog.show();
} });
Try this..
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to exit?")
.setIcon(R.drawable.icon)
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int id)
{
MyActivity.this.finish();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int id)
{
dialog.cancel();
}
});
AlertDialog alert = builder.create();
public void createbtnteam_westernbulldogs()
{
AlertDialog alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setIcon(R.drawable.team_westernbulldogs);
alertDialog.setTitle("What kind of Banner do you Want to Create?");
alertDialog.setButton("Text", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
createText();
}
alertDialog.setIcon(R.drawable.icon);
alertDialog.show();
} });
You must call setTitle with non-empty string!
Icon is displayed only if there's also title.