Andengine ask questions with toast? - android

I wonder if there is any native support for andengine or ADK to ask question-toasts? For example if I press the back button, I want some box to popup asking if I really want to quit the application and give me the option to answer yes or no.

Better to use alert dialog use this code, hope work same like that
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
switch(keyCode)
{
case KeyEvent.KEYCODE_BACK:
AlertDialog.Builder ab = new AlertDialog.Builder(AlertDialogExampleActivity.this);
ab.setMessage("Are you sure?").setPositiveButton("Yes", dialogClickListener)
.setNegativeButton("No", dialogClickListener).show();
break;
}
return super.onKeyDown(keyCode, event);
}
DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
switch (which){
case DialogInterface.BUTTON_POSITIVE:
//Yes button clicked
break;
case DialogInterface.BUTTON_NEGATIVE:
//No button clicked
break;
}
}
}

You found your solution that is good but for the above answer you have to use AndEngine functionality. If you are working with AndEngine then you have to develop all the things with AndEngine power.
So for your solution you have to create one child scene which popup when user press device back button like in the following code snippet.
class DialogBox extends Scene{
DialogBox(...){
}
// you have to include all the functionality that your dialog box should contain
}
You have to set above dialog box as child of your main scene like in the following manner on the back event of user.
mScene.setAsChildScene(new DialogBox(...));
I prefer this way if I am developing a game.

Related

How to trigger Actionbar menu items in android app

I have webview in my app, which loads some url's. In this webview, I am seeing Actionbar menu (Cut, Copy, Paste, SelectAll, Settings) on top of the app, while long pressing on the texts in the loaded webpages.
I am trying to make an alert for this menus, while click on them. If user touch's cut I need to show an alert of You clicked Copy, Are you sure to Copy this?.
For this, I am overriding onOptionsItemSelected method, but not sure that this is right way. There is no onActionBarItemSelected method.
My mainActivity extends ActionBarActivity
Here my code, I used for this triggering,
#Override
public boolean onOptionsItemSelected(MenuItem item) {
Log.d("MenuItem clicked - inside onOptionsItemSelected");
if (item.getItemId() == R.attr.actionModeCopyDrawable) {
Log.d("MenuItem clicked----", "Copy");
ShowAlert("You clicked Copy, Are you sure to Copy this?");
}
return true;
}
public void ShowAlert(String str) {
AlertDialog.Builder builder1 = new AlertDialog.Builder(this);
builder1.setMessage(str);
builder1.setCancelable(true);
builder1.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder1.create();
alert.show();
}
But there is no alert and not even a log message of MenuItem clicked - inside onOptionsItemSelected, while click on the Copy menu.
What I am missing here, hope someone can assist here.

Why do I always have to press the alert dialog button twice to close the dialog?

The below alert dialog requires me to click whichever button I click twice in order to close the dialog window and after an hours googling, I can't find the answer. I am sure it is staring me in the face but I just can't see it.
Edit: More searching has led me to believe the dialog is actually opened twice and it is occuring here:
asset_id_text_view.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
showAssetIDDialog();
return true;
}
});
Edit: started out with dialog.dismiss(); and some googling suggested trying dialog.cancel(); Neither of which were successful for me.
public void showAssetIDDialog() {
// TODO Auto-generated method stub
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
CreateTicketActivity.this);
// set title
alertDialogBuilder.setTitle("Enter Asset ID");
alertDialogBuilder.setCancelable(true).setMessage(
"How would you like to proceed?");
alertDialogBuilder.setPositiveButton("Enter text",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//enterTextDialog();
dialog.cancel();
}
});
alertDialogBuilder.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
cancel() - Cancel the dialog. This is essentially the same as calling dismiss(), but it will also call your DialogInterface.OnCancelListener (if registered).
just cancel() will cancel the listener registerred on DialogInterface.
dismiss() - Dismiss this dialog, removing it from the screen. This method can be invoked safely from any thread. Note that you should not override this method to do cleanup when the dialog is dismissed, instead implement that in onStop().
Use dialog.dismiss() for your solution
http://developer.android.com/guide/topics/ui/dialogs.html#DismissingADialog
Please refer the above link for further query.
So it looks like the problem was indeed with the OnTouchListener
The code was doing what I had asked it to do and that was to open an AlertDialog every time there was a motionEvent. That makes at least 2 times for every touch
by including a switch statement, I was able to only trigger the opening of the alertDialog when the screen was pressed and not also when the screen was released as follows:
asset_id_text_view.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
showAssetIDDialog();
break;
default:
break;
}
});
You can call dialog.dismiss() in the OnClickListener of the buttons to close the dialog.
Use dialog.dismiss();
Dismiss this dialog, removing it from the screen. This method can be
invoked safely from any thread.
alertDialogBuilder.setPositiveButton("Enter text",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//enterTextDialog();
dialog.dismiss();
}
});

Alert dialog pops up even after dismissing it

I have come across a typical problem and it seems strange to me. Details are something like this - On the activity in my app, there are edittexts and submit button. After filling the data in the edittexts, user can click submit button. After clicking submit button, based on the values that are entered by the user, either of the two alert dialogs are shown. One is success and the other one is failed.
The thing is when the user enters invalid data and clicks submit button, the failed alert dialog gets opened. I have a button(OK) on the failed alert dialog, after clicking it I wrote dialog.dismiss(); to make it disappear, so that user can recheck the data and can modify. But the problem is while rechecking & modifying the data if he changes the orientation, then again the failed alert dialog is popping up even without clicking submit button. Please suggest.
Extra Details(though probably not necessary for this problem): While changing orientation the activity is recreated. So, I am saving the current data in the onSavedInstanceState() and retrieving it in onCreate() method to set back the values in the edittexts. Everything works fine, but once clicking on submit button, the respective alert dialog appears. Then after changing orientation the dialog is again popping up. I am sure that I wrote showDialog(1); in the onClick() method but then again why control is going back into onClick and showing that alert dialog even without clicking.
protected Dialog onCreateDialog(int id) {
switch(id){
case 0:
return new AlertDialog.Builder(this)
.setMessage("Success!")
.setIcon(R.drawable.success)
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}).show();
case 1:
return new AlertDialog.Builder(this)
.setMessage("Failed")
.setIcon(R.drawable.failure)
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
return;
}
}).show();
}
return null;
}
Here is the method that makes alert dialog show.
public void onClick(View v) {
switch (v.getId()) {
//Here there are other cases too.
case R.id.submit:
getEditTexts();
validator();
break;
}
}
public void validator() {
if(generator.receiveVal(0,0,sudo)) {
showDialog(0);
}
else if(!generator.receiveVal(0,0,sudo)) {
showDialog(1);
}
}
Just try replacing .create() in the place of .show(). In your case like this:
case 1:
return new AlertDialog.Builder(this)
.setMessage("Failed")
.setIcon(R.drawable.failure)
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
return;
}
}).create(); //Here replaced .show with .create()
This is just an idea but it seems the issue is that onOrientation is trying to re-draw the activity.
Try something like the following:
You can add this to the activity declaration in the manifest:
android:configChanges="orientation"
so it looks like
<activity android:label="#string/app_name"
android:configChanges="orientation"
android:name=".your.package">
The matter is that the system destroys the activity when a change in the configuration occurs. See ConfigurationChanges.
So putting that in the configuration file avoids the system to destroy your activity. Instead it invokes the onConfigurationChanged(Configuration) method.
Hope this helps.

A better solution for an AlertDialog with an EditText

I would like an AlertDialog with an EditText field in it to accept input. This in itself is pretty straight-forward. However there are a few "obvious" features that I would like as fallout from this request. I'll take them one-by-one. I am looking for anyone that has a simpler solution to these things. I am guessing the solution is, "Write your own custom dialog you lazy bum."
AlertDialog with an EditText
final EditText input = new EditText(context);
final AlertDialog dlg = new AlertDialog.Builder(this).
setTitle("Title").
setView(input).
setCancelable(false).
setPositiveButton(android.R.string.ok, new OnClickListener()
{
#Override
public void onClick(final DialogInterface dialog, final int which)
{
/* Handle ok clicked */
dialog.dismiss();
}
}).
setNegativeButton(android.R.string.cancel, new OnClickListener()
{
#Override
public void onClick(final DialogInterface dialog, final int which)
{
/* Handle cancel clicked */
dialog.dismiss();
}
}).create();
dlg.show();
Yay, works great. It'd sure be nice if that input field got focused right away (and show the keyboard), right?
AlertDialog with focused EditText
The following code would be after create() and before dlg.show()
/** This requires API Level 8 or greater. */
dlg.setOnShowListener(new OnShowListener()
{
#Override
public void onShow(final DialogInterface dialog)
{
input.requestFocus();
((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE)).showSoftInput(input, 0);
}
});
Nice job... I guess. Now that I have this focused input it'd be nice if it respected the inputs IME option...
AlertDialog with focused EditText with a custom IME Option
input.setImeOptions(EditorInfo.IME_ACTION_DONE);
input.setOnEditorActionListener(new OnEditorActionListener()
{
#Override
public boolean onEditorAction(final TextView v, final int actionId, final KeyEvent event)
{
/** Same code here that goes in the dialog.setPositiveButton OnClickListener */
dlg.dismiss();
return true;
}
});
Now that's really not a great solution (repeated code) but it works...
Do people have a better way of solving this problem, or is it really that rare to ask a user for a small piece of information in a dialog, or am I just a winer and should go write my own dialog?
As per comments on the OP:
You do not have to have such repeated code in the OnEditorActionListener. Instead of repeating the code, you can tell the OS to click the "Ok" button when it is activated.
Something like this:
public boolean onEditorAction(final TextView v, final int actionId, final KeyEvent event) {
dlg.getButton(DialogInterface.BUTTON_POSITIVE).performClick(); // Click OK button
return true;
}
Overall I would say you are taking the right approach (documentation about collecting information through dialogs). As I mentioned in a comment, the OS uses an AlertDialog w/ EditText for adding dictionary words (to the user dictionary), so this is an expected functionality in the OS.
You can always switch to an Activity with Theme.Dialog theme or a DialogFragment which gives you a lot more freedom in tuning your widgets. AlertDialogs are probably better for displaying information. Hope this helps.

Having problems closing dialog box

I am a new to android development and need help. Please explain in detail and not just supply me with an answer that would be ideal.
My issue: I created a dialog box for my app and well it displays great, it dims the app and just opens the box but I am having issues closing it. If someone wanted to exit out of it, they would have to press the back arrow button. Yeah, this is not hard-work, but I would like my app to be nicely done and clean-cut. So I was wondering if there was a way to put an "X" at the top right corner to exit the dialog box?
If someone could add on to my code that would be perfect.Like I said, I am new to this and someone telling me just add this. I would not know where to add that code to.
My Code:
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
}
public boolean onCreateOptionsMenu(Menu menu){
super.onCreateOptionsMenu(menu);
MenuInflater library = getMenuInflater();
library.inflate(R.menu.main_menu, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()){
case R.id.menuAus:
startActivity(new Intent("com.tester.web.AUS"));
return true;
}
return false;
}
You can also use a close button (BUTTON_NEGATIVE) if you like within the Dialog Box.
Also check out this answer: How to display a Yes/No dialog box in Android?
You should be easily able to customize that code as per your requirements. Good Luck!
Edit:
Put this line in OnCreate()
Context mcontext=this;
Now use this variable in following code
final AlertDialog.Builder alert = new AlertDialog.Builder(mcontext);
alert.setTitle(title);
alert.setMessage(description);
alert.setNegativeButton("Ok",new DialogInterface.OnClickListener()
//You can also run this without the overiding the method
{
#Override
public void onClick(DialogInterface dialog, int which)
{
dialog.cancel();
});
alert.show();
Try this code.. It is running successfully..you might need to customize it a bit as per your needs..

Categories

Resources