How to not check a checkbox when touch - java

I have a checkbox that is switching state on touch. I am wanting to find out how to ignore the state switch on touch. I have an onClickListener attached to the checkbox and inside of that I am changing the state with "Yes" or "No" buttons.
if(sog.isChecked()){
sog.setChecked(false);
} else {
sog.setChecked(true);
}
Edit: The checkbox is switching when touched and my yes button is also changing state.
Edit 2: I want my dialog to change the state rather than the checkbox doing it on it's own.
Edit 3:
sog.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
showDialog(3);
}
});
builder.setTitle("Checkbox")
.setMessage(
"Are you sure you want to switch checkbox?")
.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
if(sog.isChecked()){
sog.setChecked(false);
} else {
sog.setChecked(true);
setTotalTime();
}
}
})
.setNegativeButton("No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
return alert;

I am just wanting to add a conformation on changing it from off/on.
Implement an OnCheckedChangeListener, registered on the CheckBox via setOnCheckedChangeListener(). In onCheckedChanged(), if the state of the CheckBox is one where you want additional confirmation, leave the CheckBox alone and pop your dialog. If the user taps a button in the dialog indicating that they do not want to make the change, then manually revert the checked state of the CheckBox to its prior value.
That being said, I agree with Waza_Be's comment. I am not a fan of "pop the confirmation dialog immediately" sorts of scenarios, though occasionally they are necessary. If there is some other sort of confirmation step as part of this UI (e.g., "save" action bar item), and you want to display a confirmation dialog at that point, that would be better, IMHO.

Related

Android Alert Message Need Mandatory Input

I have developed an Android Service which will run in the background. The Service want to accept Yes or No confirmation from the user at an event(say when receive an SMS).
Its working fine; the Yes or No question will be shown to user. But i want the input from (press YES or NO) user without exit from the alert(exit by press on the Mobile Back button or Home button etc).
Please help me how it can be possible.
Below the code I am using;
AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
//ctx is the Context
builder.setTitle("Emergency!");
String txt="Do you want to accept?";
builder.setMessage(txt);
builder.setPositiveButton("YES", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
//Do something
}
});
builder.setNegativeButton("NO", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
///dialog.dismiss();
}
});
AlertDialog alert = builder.create();
alert.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
alert.show();
just add this in you AlertDialog
builder.setCancelable(false);
hope you are useing android.support.v7.app.AlertDialog;
You can disable back press while the dialog is shown (or possibly, send a "No" response when back button is pressed) with dialog.setOnKeyListener
dialog.setOnKeyListener(new Dialog.OnKeyListener() {
#Override
public boolean onKey(DialogInterface arg0, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
finish();
dialog.dismiss();
}
return true;
}
});
Unfortunately you can't override Home button press on Android. You can, however, react to an activity being sent to the background via Home button by implementing onUserLeaveHint in the activity hosting your dialog.
Edit: You may also want to disable dismissing the dialog by touching outside (which is the default behavior) by doing
dialog.setCanceledOnTouchOutside(false);

Android make a progressDialog ProgressBar Invisible

This is my ProgressDialog
progressBar = new ProgressDialog(Wallpapers.this);
progressBar.setCancelable(false);
progressBar.setMessage("Downloading " + downloadedFile + ".png");
progressBar.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressBar.setProgress(0);
progressBar.setMax(100);
progressBar.setButton(DialogInterface.BUTTON_NEGATIVE, "No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
progressBar.setButton(DialogInterface.BUTTON_POSITIVE, "Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
progressBar.show();
Given that i can force the Buttons to become VISIBLE INVISIBLE GONE with the following:
progressBar.getButton(ProgressDialog.BUTTON_POSITIVE).setVisibility(View.INVISIBLE);
I had hoped to be able to do the same with the ProgressBar, this would allow more room for text to describe which button to press after the task is completed.
But the following is my best guess and its creating a NullPointer
progressBar.getButton(ProgressDialog.STYLE_HORIZONTAL).setVisibility(View.INVISIBLE);
Please note I am not looking to close the Dialog, only force the ProgressBar to be Invisible.
If it cannot be done then so be it, but any help would be great
ProgressDialog.STYLE_HORIZONTAL and ProgressDialog.STYLE_SPINNER are not Buttons, nor are they identifiers for Buttons in the AlertDialog class.
If you really want to stick with a ProgressDialog, you could hack your way around it by getting a View within the dialog (e.g. a Button or the View that has focus via getCurrentFocus()), then get the root View of the Dialog and traverse it's children until you find a ProgressBar. I wouldn't recommend this.
A better alternative would be to create your own layout that includes a ProgressDialog, and set that as an AlertDialog's View with setView(). This way you can define your own ID for the bar and retrieve it via the Dialog's findViewById() method.

Beginner Button click event

I am new to Android, i am writing a program where when a user clicks a button a Alert Dialog to appear. This alert dialog has 2 buttons, Yes and No. Upon clicking Yes/No, i need to sysout the response.
The code i have so far; Can someone help me add the Alert Dialog;
public class HelloWorldProjectActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.myFirstScreen);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v.getId()==(R.id.button1)){
System.out.println("first button clicked");
// I need a Alert Dialog to appear here, and that will have 2 buttons YES and NO, the users response should be printed to the console.
}
}
You cannot System.out.print().
There are several methods to display the result. One is to use Toast. It will briefly show a text message and then disappear.
new AlertDialog.Builder(this)
.setMessage("Are you sure?")
.setPositiveButton("Yes", new OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(TestAndroidActivity.this, "YES CLICKED",
Toast.LENGTH_LONG).show();
}
}).setNegativeButton("No", new OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(TestAndroidActivity.this, "NO CLICKED",
Toast.LENGTH_LONG).show();
}
}).show();
Modify your code as follows:
The activity class doesn't have to implement OnClickListener.
Thus, remove onClick() method
Go to the layout file, add an attribute android:onClick="click" in the button declaration.
Add public void click(View view) with the previous code.
First of all, there really isn't any system.out to print to in android. What you should try instead is printing to the log. For information on how to print to the log, check this out. To then see the activity of the log (including messages you printed to it), checkout the logcat.
Second, for information on creating an alert dialog, please view this documentation.

How to update AlertDialog content using showDialog(id)

i want to have in my application an alertdialog, that has its message updated everytime it is showed.
This is because the dialog box value depends on some values on the application.
Now i tried to use the showDialog method:
#Override
public boolean onTouch(View arg0, MotionEvent arg1) {
showDialog(RESULT_DIALOG);
return false;
}
But once the dialog is created, it doesn't change the message (i know that if the dialog is created, it use the started version).
My onCreateDialog method code is:
public Dialog onCreateDialog(int dialogId) {
AlertDialog dialog;
switch(dialogId) {
case RESULT_DIALOG:
// do the work to define the pause Dialog
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(localTv.getText())
.setCancelable(false)
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
dialog = builder.create();
break;
default:
dialog = null;
}
return dialog;
}
There is a way to update the content of the AlertDialog.
Actually i create a new dialog box every time the onTouch event is called. But i'm not sure that it is the cleanest way to solve that problem.
Any idea?
Thanks :)
You have to use onPrepareDialog method:
#Override
protected void onPrepareDialog ( int id, Dialog dialog ) {
switch ( id ) {
case RESULT_DIALOG:
AlertDialog alertDialog = ( AlertDialog ) dialog;
alertDialog.setMessage( localTv.getText() );
break;
}
super.onPrepareDialog( id, dialog );
}
From http://developer.android.com/guide/topics/ui/dialogs.html :
Before the dialog is displayed, Android also calls the optional
callback method onPrepareDialog(int, Dialog). Define this method if
you want to change any properties of the dialog each time it is
opened. This method is called every time a dialog is opened, whereas
onCreateDialog(int) is only called the very first time a dialog is
opened. If you don't define onPrepareDialog(), then the dialog will
remain the same as it was the previous time it was opened. This method
is also passed the dialog's ID, along with the Dialog object you
created in onCreateDialog().
You can always change the dialog using onPrepareDialog or you can remove the dialog (so it will always pass through onCreateDialog) setting the onDismiss (dialog.setOnDismiss) to remove the dialog id (removeDialog(id)).

how to detect a search button while an alert is shown in an android

Hi stackoverflow friends
I recently faced an issue that how can i disable global search button in android while an alert is shown in the screen.I don't want to disappear the alert box by using search button. I need to user must click the alertbox button and disappears in that way.So I want to disable the search button while alert box is shown. But I can disable the back button using setCancable(false).How can I solve this ?
THanks in advance.
So, Your intention is to provide non-cancelable alert.
Suggesting to set OnDismissListener and just show alert again. It's not very good from visual perspective (alert get closed and opened again).
Below is some obvious example how to achieve such non-cancelable alert (code is inside Acctivity class):
/** reference to our alert */
private AlertDialog alert = null;
/** to indicate if alert dismissed by key */
private boolean alertKeyPressed = false;
#Override
protected void onResume() {
// Say, we need to show alert when activity resumed
if(true/*provide condition to show alert*/) {
showAlert();
}
}
/**
* Show non dismissable alert
*/
private void showAlert() {
if(null == this.alert) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(false);
builder.setTitle(R.string.str_alert_title);
builder.setMessage(R.string.str_alert_text);
builder.setIcon(android.R.drawable.ic_dialog_alert);
builder.setNeutralButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
YourActivity.this.alertKeyPressed = true;
dialog.dismiss();
}
});
this.alert = builder.create();
this.alert.setOwnerActivity(this);
this.alert.show();
this.alert.setOnDismissListener(new DialogInterface.OnDismissListener() {
#Override
public void onDismiss(DialogInterface dialog) {
// dialog is not allowed to be dismissed, so show it again
YourActivity.this.alert = null;
if(!YourActivity.this.alertKeyPressed) {
showAlert();
}
}
});
}
}
However, I don't think it's the right way to left such alert for the user, sometimes it might be needed for cases like evaluation restriction etc.
Override onSearchRequested in your Activity and have it return false while the dialog is being shown. This should block the request, as per the docs:
You can override this function to force global search, e.g. in
response to a dedicated search key, or to block search entirely (by
simply returning false).
Returns true if search launched, and false if activity blocks it. The
default implementation always returns true.
.setOnKeyListener(new DialogInterface.OnKeyListener()
{
#Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event)
{
//There you catch the key, do whatever you want to do.
//Return true if you handled the key event, so nothing will trigger.
//Return false if you want your activity to handle.
return true;
}
})
Just add the code above to alert dialog builder. Hope this snippet would help. Good luck.

Categories

Resources