I have one Quote application. When I open the application's MainActivity, if there is new data updated on the server side, one AlertDialog is shown for going to SettingsActivity. It takes some time for appear so if the app is already on SettingsActivity, it is still showing the AlertDialog to go to SettingsActivity.
I want to prevent the AlertDialog from showing in SettingsActivity but continue showing in other activities. My code for AlertDialog is below. How can I do that?
public class UpdatesDialogActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AlertDialog.Builder builder = new AlertDialog.Builder(UpdatesDialogActivity.this);
builder.setTitle("Download New Status");
builder.setMessage("There Are New Status Arrived. Push Download Button From Settings.");
builder.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent intent = new Intent(UpdatesDialogActivity.this, SettingsActivity.class);
finish();
startActivity(intent);
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
finish();
}
});
builder.setOnKeyListener(new OnKeyListener() {
#Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP)
finish();
return false;
}
});
builder.show();
}
// ==============================================================================
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
}
}
You can go for an 'instanceof' check and see if the current activity is settings or else.
if(activity instanceof SettingsActivity){
//don;t show dialog
}else{
//show dialog
}
Hope this will help.
Related
I have a snackbar that I build with a duration set to Snackbar.LENGTH_INDEFINITE
The snackbar is displayed properly when I call mySnackbar.show();
But as soon as I hit the action button, the snackbar is dismissed.
The dismiss method seems called by the system.
Does anyone know a workaround ?
Here is my code for building my snackbar:
Snackbar mySnackbar = Snackbar.make(mParent, R.string.the_question, Snackbar.LENGTH_INDEFINITE)
.setAction(R.string.yes, new View.OnClickListener() {
#Override
public void onClick(View v) {
//My code...
}
})
.addCallback(new Snackbar.Callback() {
#Override
public void onDismissed(Snackbar snackbar, int event) {
}
#Override
public void onShown(Snackbar snackbar) {
}
});
Below code is showing the Alert dialog "after" the snackbar is displayed.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Snackbar snackbar = Snackbar.make(findViewById(android.R.id.content),
"This is Snackbar", Snackbar.LENGTH_INDEFINITE).
setAction(R.string.yes, new View.OnClickListener() {
#Override
public void onClick(View v) {
}
}).addCallback(new Snackbar.Callback() {
#Override
public void onDismissed(Snackbar transientBottomBar, int event) {
super.onDismissed(transientBottomBar, event);
}
#Override
public void onShown(Snackbar sb) {
super.onShown(sb);
}
});
snackbar.show();
showAlertDialog(this, "Alert!!", "Alert Dialog", "Yes", "No");
}
The showAlertDialog is simple static method to show the dialog
public static void showAlertDialog(Context context, String title, String message, String posBtnMsg, String negBtnMsg) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle(title);
builder.setMessage(message);
builder.setPositiveButton(posBtnMsg, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.setNegativeButton(negBtnMsg, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
The screen shot of the output for above code is below,
The answer to this question lies in the way Snackbar.setAction(CharSequence text, final View.OnClickListener listener) is implemented
If you pass this method a non empty text or non null listener, the TextView displaying the action's text is set an OnClickListener which calls BaseTransientBottomBar.dispatchDismiss(BaseCallback.DISMISS_EVENT_ACTION) when the action is performed. This causes the Snackbar to be dismissed.
To prevent that, one needs to retrieve the TextView of the Snackbar's action view, and override its OnClickListener with a listener that does not call dispatchDismiss()
Here is the Snackbar.setAction() code for reference
public Snackbar setAction(CharSequence text, final View.OnClickListener listener) {
final SnackbarContentLayout contentLayout = (SnackbarContentLayout) mView.getChildAt(0);
final TextView tv = contentLayout.getActionView();
if (TextUtils.isEmpty(text) || listener == null) {
tv.setVisibility(View.GONE);
tv.setOnClickListener(null);
} else {
tv.setVisibility(View.VISIBLE);
tv.setText(text);
tv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
listener.onClick(view);
// Now dismiss the Snackbar
dispatchDismiss(BaseCallback.DISMISS_EVENT_ACTION);
}
});
}
return this;
}
I created an AlertDialog, which will either refresh the activity (when btnConfirm1 is pressed), or does nothing and simply closes down (when btnDisconfirm1 is pressed). Everything is working apart from btnDisconfirm1. How can I close the dialog?
So apparently AlertDialog does not have a dismiss or cancel method, but is there another way without using negative buttons? The thing is, I created a layout file for this dialog and I don't know how to put a negative button in my xlm-file.
Or should I use a completely different approach apart from AlertDialog? Thanks!
btnClear = (Button) findViewById(R.id.btnClear);
btnClear.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder mBuilder2 = new AlertDialog.Builder(ScoreScreen.this);
View mView2 = getLayoutInflater().inflate(R.layout.dialog_confirm_delete, null);
Button btnConfirm1=(Button) mView2.findViewById(R.id.btnConfirm1);
Button btnDisconfirm1=(Button) mView2.findViewById(R.id.btnDisconfirm1);
btnConfirm1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
player1.setPlayerScore(0);
player2.setPlayerScore(0);
player3.setPlayerScore(0);
player4.setPlayerScore(0);
Intent intent = getIntent();
finish();
startActivity(intent);
}
});
btnDisconfirm1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//WHAT DO I PUT HERE???
}
});
mBuilder2.setView(mView2);
AlertDialog dialog = mBuilder2.create();
dialog.show();
}
});
First you should create the AlertDialog with
AlertDialog mDialog = mBuilder2.create();
And secondly you can dismiss the dialog inside the OnClickListener with
mDialog.dismiss();
You can put this method in your Util class. and use callbacks
public interface OnDialogDismiss {
void onPositiveClick();
void onNegativeClick();
}
Modify it as your requirement.
public void showDialogForMultipleCallback(Context context, String title, String message, boolean cancellable, String neutralBbtn, String negativeBtn, String positiveBtn, final OnDialogDismiss onDialogDismiss) {
final AlertDialog.Builder builder1 = new AlertDialog.Builder(context);
builder1.setTitle(title);
builder1.setMessage(message);
builder1.setCancelable(cancellable);
if (neutralBbtn != null) {
builder1.setNeutralButton(neutralBbtn, new OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
if (alert11 != null)
alert11.dismiss();
}
});
}
if (negativeBtn != null) {
builder1.setNegativeButton(negativeBtn,
new OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
if (alert11 != null)
alert11.dismiss();
}
});
}
if (positiveBtn != null) {
builder1.setPositiveButton(positiveBtn, new OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
onDialogDismiss.onPositiveClick();
}
});
}
alert11 = builder1.create();
alert11.show();
}
Hi my problem is when I select an item my AlertDialog dismiss
alertDialog = new AlertDialog.Builder(getActivity());
alertDialog
.setSingleChoiceItems(ageArr, 1, btnSelectItem)
.setPositiveButton(R.string.dialog_ok, btnPositiveAgeDialog)
.setNegativeButton(R.string.dialog_cancel, null)
.show();
what my dialog click positive looks is.
private DialogInterface.OnClickListener btnSelectItem = new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
selectedIndexAge = which;
}
};
I tried setting the listener to null and it does not close but still
I needed it because I wanted to know which item is selected
Just put it
itemView.setOnClickListener(null);
or
You can use the implementation of hasOnClickListeners() for knowing the status of listener taken from android.view.View class for
public boolean hasOnClickListeners() {
ListenerInfo li = mListenerInfo;
return (li == null && li.mOnClickListener == null);
}
Use the following link for further modifications
Set listener instance in fragment on application restore
try this
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
Boolean wantToCloseDialog = false;
//Do stuff, possibly set wantToCloseDialog to true then...
if(wantToCloseDialog)
dialog.dismiss();
//else dialog stays open. Make sure you have an obvious way to close the dialog especially if you set cancellable to false.
}
});
Something else must be closing your AlertDialog. Below is a program that I believe duplicates the minimum requirements you have posted, and selecting one of the items does not close the dialog.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String[] values = new String[]{ "one", "two", "three", "four" };
DialogInterface.OnClickListener choiceListener = new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "selected index: " + which, Toast.LENGTH_SHORT).show();
}
};
DialogInterface.OnClickListener positiveListener = new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "positive button", Toast.LENGTH_SHORT).show();
}
};
new AlertDialog.Builder(this)
.setSingleChoiceItems(values, 1, choiceListener)
.setPositiveButton("ok", positiveListener)
.setNegativeButton("cancel", null)
.show();
}
}
in my app i have to create deactivation code this is my code
#Override
protected void onCreate(Bundle savedInstanceState) {
context = getApplicationContext();
dbHelper = new DatabaseHelper(context);
userMO = dbHelper.getRingeeUserData(1);
super.onCreate(savedInstanceState);
setContentView(R.layout.manage_account);
TextView deleteAccount = (TextView) findViewById(R.id.delete_account);
deleteAccount.setOnClickListener(new View.OnClickListener() {
// while clicking Delete My Account this method is called
#Override
public void onClick(View arg0) {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(ManageAccount.this);
alertDialog.setTitle("Confirm Deactivate");
alertDialog.setMessage("Are you really want to deactivate your account?");
alertDialog.setNegativeButton("YES", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//while clicking YES button isDelete is stored as 1 in database
userMO.setIsDelete(1);
del();
new AsyncTask<Void, Void, String>() {
protected String doInBackground(Void... arg0) {
return userDelegate.updateUser(userMO, context);
}
}.execute(null, null, null);
dbHelper.updateRingeeUser(1, userMO.getRingeeUserId(), userMO);
Toast.makeText(getApplicationContext(), "successfully deactivated", Toast.LENGTH_SHORT).show();
}
});
alertDialog.setPositiveButton("NO", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
// Showing Alert Message
alertDialog.show();
}
});
}
after clicking Manage Account--> Delete My Account--> YES/NO Here if user clicks YES button IsDelete will be stored as 1(user disabled in database) in database here inaddition i have to close that app and have to bring normal home page of the mobile
any one can help me??
I don't think you should close the App after deactivating the acoount, but return the user to your Login screen:
Intent reLoginIntent = new Intent(context, Login.class);
reLoginIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | IntentCompat.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(reLoginIntent);
finish();
If you just want to close the Activity, just use:
finish();
I want to show a alert dialog box to the user when the back button is pressed..For example :Are you sure want to exit ?.I am not having a problem in creating a alert dialog box..I am having a problem in where to call this dialog box method().
This is my HomeScreen.java
public class HomeScreen extends TabActivity {
ConferenceOpenHelper helper_ob;
Cursor cursorHome;
ProfileEditScreen profileEditScreen;
ConferenceAdapter adapter;
EcMeeting meeting;
final Context context = this;
public static int HOMETEMP = 1;// Constant for Deleting the last created id
// in edit profile screen (Cancel)
public static String HOME = "home";// constant for updating status column
public static String CUSTOM_DIALER = "custom";
public static String TEMPLATE = "template";// constant for updating status // column
public static TabHost tabHost;
public static final int QUICK_START = 1;// Constant for menu options
public static final int TEMPLATE_SCREEN = 2;// Constant for menu options
public static final int USER_GUIDE = 3;// Constant for menu options
public static final int QUICK_CALL = 4;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab);
Resources ressources = getResources();
tabHost = getTabHost();
// Android tab
Intent photo = new Intent().setClass(this,EcMeeting.class);
TabSpec tabSpecphoto = tabHost
.newTabSpec("Calendar")
.setIndicator("Calendar", ressources.getDrawable(R.drawable.calendartab))
.setContent(photo);
// Apple tab
Intent intentApple = new Intent().setClass(this,CallListScreen.class);
TabSpec tabSpecApple = tabHost
.newTabSpec("Calls")
.setIndicator("Calls", ressources.getDrawable(R.drawable.ic_action_device_access_call))
.setContent(intentApple);
// Windows tab
Intent intentWindows = new Intent().setClass(this,UserSettingActivity.class);
TabSpec tabSpecWindows = tabHost
.newTabSpec("Settings")
.setIndicator("Settings", ressources.getDrawable(R.drawable.ic_action_setting))
.setContent(intentWindows);
// add all tabs
tabHost.addTab(tabSpecphoto);
tabHost.addTab(tabSpecApple);
tabHost.addTab(tabSpecWindows);
//set Windows tab as default (zero based)
/*tabHost.setCurrentTab(0);*/
for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
{
tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#d3d3d3"));
}
tabHost.getTabWidget().setCurrentTab(0);
/*tabHost.getTabWidget().getChildAt(0).setBackgroundColor(Color.parseColor("#C35817"));*/
}
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add(0, QUICK_START, 0, "Quick Start").setIcon(R.drawable.ic_menu_add_icon);
menu.add(0, USER_GUIDE, 0, "User Guide").setIcon(R.drawable.ic_menu_guide_icon);
return true;
}
#Override
public void onBackPressed() {
new AlertDialog.Builder(this)
.setMessage("Are you sure you want to exit?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
HomeScreen.this.finish();
}
})
.setNegativeButton("No", null)
.show();
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
switch (item.getItemId()) {
case QUICK_START:
startActivity(new Intent(this, ConfDialerScreen.class));
break;
case USER_GUIDE:
startActivity(new Intent(this, UserGuideScreen.class));
break;
}
return true;
}
public void onTabChanged(String tabId) {
// TODO Auto-generated method stub
for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
{
tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#8A4117"));
}
tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(Color.parseColor("#C35817"));
}
}
This is the method() to create alert dialog
#Override
public void onBackPressed() {
new AlertDialog.Builder(this)
.setMessage("Are you sure you want to exit?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
HomeScreen.this.finish();
}
})
.setNegativeButton("No", null)
.show();
}
Now ,I really did not know where to call this method.Could someone give any Idea?
when user press the back button I want to show the alert Dialog.
Try below code:
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
dialogOnBackPress();
return true;
}
return super.onKeyDown(keyCode, event);
}
protected void dialogOnBackPress() {
new AlertDialog.Builder(this)
.setMessage("Are you sure you want to exit?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
HomeScreen.this.finish();
}
})
.setNegativeButton("No", null)
.show();
}
Use this:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Alert Message:");
builder.setMessage("Do you want to save this image to your file??");
builder.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
// do your stuff
}
});
builder.setNegativeButton("No",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.dismiss();
finish();
}
});
AlertDialog alert = builder.create();
alert.show();
return true;
}
return super.onKeyDown(keyCode, event);
}
you can this code modify acc. to req.
dialog.setOnKeyListener(new Dialog.OnKeyListener() {
#Override
public boolean onKey(DialogInterface arg0, int keyCode,
KeyEvent event) {
// TODO Auto-generated method stub
if (keyCode == KeyEvent.KEYCODE_BACK) {
finish();
dialog.dismiss();
}
return true;
}
});
do not call super.onBackPressed(); in onBackPressed
Else you can simply add onBackPressed() in activity_main.xml(tab.xml) as
android:onClick="onBackPressed"
mention it in your button properties