I have some code that build a dialog box and makes a listener for it. The dialog box displays fine, but the code inside the listener doesn't seem to run, and I don't know why.
private void showBackgrounDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(MeacruxActivity.this);
builder.setTitle(R.string.background_dialog_title).setCancelable(true)
.setItems(R.array.background_options,
new DialogInterface.OnClickListener(){
#Override
public void onClick(DialogInterface dialog, int selection) {
Log.d(logID, "the selection is: " + selection);
if(backgrounds.length==selection){
notImplementedYet.show();
return;
}
setBckground(backgrounds[selection]);
}
});
currentDialog = builder.create();
currentDialog.show();
}
private void setBackground(String bgName) {
Log.d(logID, bgName);
}
The dialog shows up properly with all the options and everything, but when I click on one nothing come sup in the log.... Why is that?
Edit: I did some more testing and I can confirm that the code inside of the onClick function is being run, its just that the log isnt showing up...
I'm assuming you are looking in eclipse or studio
In DDMS view, make sure the device is selected.
In Logcat view, make sure there's not a filter applied.
On terminal, type adb logcat... does it show up there?
Related
New to Android... I understand Dialogs are asynchronous. But I really can't get my head around the flow for confirming an action. Can someone please explain the flow?
I want to save a file on the sdcard. The Activity prompts the used for the filename. Then it checks to see if the file exists. If it exists, it needs to prompt the user to confirm if they want to overwrite it. Then it proceeds to erase and write the file.
I know you can't hold execution waiting for the response. How then would this common flow work in Android?
Thanks
I am not 100% it is what you are looking for, but here is a link to the Android documentation explaining how we should display Confirmation and Acknowledgement popups using the "Android standard way":
http://developer.android.com/design/patterns/confirming-acknowledging.html
I do not know the exact flow, I suppose it would depend on how the application was written. I would check for the file if it existed call the dialog windows then if the Ok/Yes/Confirm is pressed overwrite the file.
Dialogs | Android Developers - Has an excellent code example
public class FireMissilesDialogFragment extends DialogFragment {
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the Builder class for convenient dialog construction
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage(R.string.dialog_fire_missiles)
.setPositiveButton(R.string.fire, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// FIRE ZE MISSILES! AKA Overwrite your file.
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog AKA do nothing
}
});
// Create the AlertDialog object and return it
return builder.create();
}
}
I know it's slightly silly example but basically check for the file (if exist) > Call Dialog (if yes)> Overwrite.
I am doing alert dialog demo. It work fine but I found a line over "setButton" in MainActivity.java file. Hear is my code:
#SuppressWarnings("deprecation")
public void onClick(View arg0) {
// Creating alert Dialog with one Button
AlertDialog alertDialog = new AlertDialog.Builder(
MainActivity.this).create();
// Setting Dialog Title
alertDialog.setTitle("Alert Dialog");
// Setting Dialog Message
alertDialog.setMessage("Welcome to AndroidHive.info");
// Setting Icon to Dialog
// I got line hear........ over setButtob .........
alertDialog.setButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
// Write your code here to execute after dialog
// closed
Toast.makeText(getApplicationContext(),
"You clicked on OK", Toast.LENGTH_SHORT)
.show();
}
});
// Showing Alert Message
alertDialog.show();![enter image description here][1]
}
});`
You cab see the image here and get more idea of what I mean to say.
You mean you got a warning saying the method is deprecated?
That's because setButton is not used anymore, it's deprecated, instead you should be doing something like this:
alertDialog.setPositiveButton("OK",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// blablabla
}
});
Also notice you can get the String "OK" like thing as well : android.R.string.ok , which is the recommended way ;)
I went through the url image, which made your question clear. This is showing because that method( setButton) is depreciated. You are using
#SuppressWarnings("deprecation")
at the top. This prevents is from giving the warning message and you cannot read the warning message( which probably is that: This method has been depreciated). If you remove the top line, then it will tell you why it shows cross line over setButton. See here. It says that this method was depreciated in API level 3.
For seeing how use use it in proper way, you can go through this official documentation page of using Alert Dialog. You can also see the tutorial here.
I am trying to have a disclaimer pop up when the app is first run, and after each update. I did a bunch of googling and reviewed some questions here as well, and this is what my code looks like:
SharedPreferences pref = getSharedPreferences("Preferences", MODE_PRIVATE);
SharedPreferences.Editor edit = pref.edit();
String lver = pref.getString("Version", "");
String ver = this.getString(R.string.version);
if(ver != lver)
{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Disclaimer")
.setMessage(this.getString(R.string.disclaimer))
.setCancelable(false)
.setPositiveButton("Accept", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
accepted = true;
dialog.cancel();
}
})
.setNegativeButton("Decline", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
MainMenu.this.finish();
}
});
AlertDialog disc = builder.create();
disc.show();
if(accepted == true)
{
edit.putString("Version", this.getString(R.string.version));
edit.commit();
}
}
This code actually worked at first, but when I changed my apps starting activity, and moved this code from the original starting activity to the new one, it no longer works. The disclaimer appears on every run.
I want the popup only to show on first run and after updates. What do I need to do to achieve this? I am very frustrated and confused by the fact that it was working, then wasnt.
Comparing Strings with .equals() is the correct way (see: How do I compare strings in Java? for a good explanation) , although because I'm not sure how the android internals work and you said it worked before, that isn't your problem. Your problem is that your check
if (accepted == true) {/** code */}
isn't run on the on click listener. Because it isn't, that thread (I'm assuming it spawns a new thread to show the dialog) keeps running.
I'm also assuming before you moved this code, you had declared a
boolean accepted = true; //or initialized it to true somewhere
But when you moved it you didn't reinitialize it. Now, because the default value of a primitive is false, in your new code it gets to the check before you press a dialog button, and never commit the new version.
My advice would be put what's in the
accepted == true
block simply into your listener for the positive button click.
Can anyone figure out why this causes a force close??
void failbox(){
// Create the alert box
AlertDialog.Builder alertbox = new AlertDialog.Builder(this);
// Set the message to display
alertbox.setMessage(R.string.fail);
alertbox.setPositiveButton("Get Busybox", new DialogInterface.OnClickListener() {
// do something when the button is clicked
public void onClick(DialogInterface arg0, int arg1) {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse("market://details?id=stericson.busybox"));
}
});
// set a negative/no button and create a listener
alertbox.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
// do something when the button is clicked
public void onClick(DialogInterface arg0, int arg1) {
}
});
// show the alert box
alertbox.show();
}
Thanks in advance!
If you're getting the forced close in the emulator, that's just how it is, as best I can tell. You cannot access the market through the market app from an emulator.
Does your app still crash when run on a real Android device?
(Of course, some folks have figured out sneaky ways to get to the market from an emulator. See How to install Android Market App on the emulator?)
I cannot understand this code in page number 68-69 in Hello Android book. Some methods used in the code are new to me. Can anybody elaborate and explain the code.
private static final String TAG = "Sudoku" ;
private void openNewGameDialog() {
new AlertDialog.Builder(this)
.setTitle(R.string.new_game_title)
.setItems(R.array.difficulty, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialoginterface,int i) {
startGame(i);
}
})
.show();
}
private void startGame(int i) {
Log.d(TAG, "clicked on " + i);
// Start game here...
}
All it does is when you call openNewGameDialog() it will create an alertdialog with an assigned title and list of options from a resource file ("R.array.difficulty" is an integer value ultimately pointing to a string-array declared in the file /res/values/arrays.xml). An AlertDialog is a simple to create way of getting input from the user. It can also be used for output, but many prefer Toast for that task. The
.show() at the end of it brings the dialog to the foreground.
When the items are added in that call they are assigned an onClick listener which when an item is clicked it sends the index of that item to startGame. In that function it only sends a message including the index to the logcat debug system.