I want to remove a cardview from the activity after an alert confirmation.
If I delete cardview directly, the default sliding animation is shown, but with the DialogInterface i don't see the animation and cardview is not disappearing.
Below is the code
AlertDialog.Builder builder1 = new AlertDialog.Builder(this);
builder1.setMessage("Are you sure you want to delete this account?");
builder1.setCancelable(true);
builder1.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
RelativeLayout rlMain = (RelativeLayout) findViewById(R.id.rlMain);
CardView cv = (CardView) findViewById(id);
rlMain.removeView(cv);
}
});
builder1.setNegativeButton("No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert11 = builder1.create();
alert11.show();
Related
I have a created a app with List view when i click back button from home page it will toast a message and ask to exit or not. after i have implement a Navigation drawer and set open and close. but when i click back button from navigation go back and popup the toast which i set to hoe page.
#Override
public void onBackPressed() {
if (drawerLayout.isDrawerOpen(GravityCompat.START)){
drawerLayout.closeDrawer(GravityCompat.START);
}else{
super.onBackPressed();
}
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to Exit?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
finish();
AdminHome.super.onBackPressed();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
how to stop the toast message when i click back from navigation bar and set toast from home page.
hi You can change your code like this
#Override
public void onBackPressed() {
if (drawerLayout.isDrawerOpen(GravityCompat.START)){
drawerLayout.closeDrawer(GravityCompat.START);
}else{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to Exit?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
finish();
AdminHome.super.onBackPressed();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
}
Put the AlertDialog code in else block.
Put your alertdialog inside else of your condition
#Override
public void onBackPressed() {
if (drawerLayout.isDrawerOpen(GravityCompat.START)){
drawerLayout.closeDrawer(GravityCompat.START);
}else{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to Exit?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
super.onBackPressed();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
}
I'm currently developing an android app. In this app should an alert dialog ask for an username and a password. The problem is, that the show-method causes a crash of the app. So I have tried to show a simple alert dialog; but there was the same problem. The wired this is, that the app closes without any exception.
Here is the code of the showLoginWindow:
public class MainScreen extends AppCompatActivity {
// other code
private void showLoginWindow(boolean falseLogin){
LayoutInflater layoutInflater = LayoutInflater.from(this);
View promptView = layoutInflater.inflate(R.layout.login, null);
TextView tv = (TextView) promptView.findViewById(R.id.Message3);
if(falseLogin) tv.setText("Falscher Benutzername oder falsches Passwort. Bitte wiederholen.");
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setView(promptView);
final EditText uName = (EditText) promptView.findViewById(R.id.username);
final EditText password = (EditText) promptView.findViewById(R.id.password);
// setup a dialog window
alertDialogBuilder.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
login(uName.getText().toString(), password.getText().toString());
}
});
// create an alert dialog
AlertDialog alert = alertDialogBuilder.create();
alert.show();
}
}
And here is the code of the simple dialog (also in MainScreen):
private void testIt(){
AlertDialog.Builder builder;
builder = new AlertDialog.Builder(this, android.R.style.Theme_Material_Dialog_Alert);
builder.setTitle("test")
.setMessage("test dialog")
.setPositiveButton("yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
})
.setNegativeButton("no", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
})
.setIcon(android.R.drawable.ic_dialog_alert)
.show();
}
I hope you can help me and sorry for my bad language.
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();
}
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.