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..
Related
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.
I am following the tutorial given here http://www.android-feedback.com/library for sending feedback. But I am unable to show dialog in onOptionsItemSelected.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
//menu.add("Email");
// TODO Auto-generated method stub
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case 0:
// do whatever
FeedbackSettings feedbackSettings = new FeedbackSettings();
//SUBMIT-CANCEL BUTTONS
feedbackSettings.setCancelButtonText("No");
feedbackSettings.setSendButtonText("Send");
//DIALOG TEXT
feedbackSettings.setText("Hey, would you like to give us some feedback so that we can improve your experience?");
feedbackSettings.setYourComments("Type your question here...");
feedbackSettings.setTitle("Feedback Dialog Title");
//TOAST MESSAGE
feedbackSettings.setToast("Thank you so much!");
feedbackSettings.setToastDuration(Toast.LENGTH_SHORT); // Default
feedbackSettings.setToastDuration(Toast.LENGTH_LONG);
//RADIO BUTTONS
feedbackSettings.setRadioButtons(false); // Disables radio buttons
feedbackSettings.setBugLabel("Bug");
feedbackSettings.setIdeaLabel("Idea");
feedbackSettings.setQuestionLabel("Question");
//RADIO BUTTONS ORIENTATION AND GRAVITY
feedbackSettings.setOrientation(LinearLayout.HORIZONTAL); // Default
feedbackSettings.setOrientation(LinearLayout.VERTICAL);
feedbackSettings.setGravity(Gravity.RIGHT); // Default
feedbackSettings.setGravity(Gravity.LEFT);
feedbackSettings.setGravity(Gravity.CENTER);
//SET DIALOG MODAL
feedbackSettings.setModal(true); //Default is false
//DEVELOPER REPLIES
feedbackSettings.setReplyTitle("Message from the Developer");
feedbackSettings.setReplyCloseButtonText("Close");
feedbackSettings.setReplyRateButtonText("RATE!");
//DEVELOPER CUSTOM MESSAGE (NOT SEEN BY THE END USER)
feedbackSettings.setDeveloperMessage("This is a custom message that will only be seen by the developer!");
feedBack = new FeedbackDialog(this, "AF-548BD4EFE07D-89", feedbackSettings);
feedBack.show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
I am unable to show the dialog on click of item !
Please help me to identify my mistake !
Thanks in advance !
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.
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.
An Android device configuration change (for example "slide the hard keyboard back in") will always call PhoneWindow.onConfigurationChanged(), which in turn, will call reopenMenu(). This will cause the menu of the currently running activity to be reopened, in case it is showing.
I have a lock on my menu implemented in my onPrepareOptionsMenu() override. The user must enter a code each time they want to see the menu. I don't want the user to be asked to enter the code again, while the menu is still up just because of a configuration change. Thus, I would like to know, is there any way I can check if the menu of current foreground activity is already showing? Knowing this, I could bypass asking for the access code if the menu is already up.
My custom workaround implementation is to use my own flag menuShowing, which I set in onPrepareOptionsMenu and reset in onOptionsItemSelected and in onKeyDown if the back button is clicked.
EDIT: It appears a screen orientation configuration change does not trigger this behavior. A hard keyboard slide however, does.
Until someone comes up with a nicer 'one call' answer, here is the custom workaround implementation that I mention in the question, with help from Sam's tips, in case someone needs the same functionality:
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
if (showingMenu) {
// The menu button was clicked or the hard keyboard was
// slid open/closed while the menu was already showing
return true;
}
// Otherwise, either the menu was clicked or openOptionsMenu() was called
if (codeEntered) {
// Code was entered and then openOptionsMenu() was called
showingMenu = true;
// Menu will now be shown
return true;
} else {
// The menu button was clicked, ask for code
askForCode();
// Don't show menu yet
return false;
}
}
#Override
public void onOptionsMenuClosed(Menu menu) {
showingMenu = false;
codeEntered = false;
}
private void askForCode() {
codeEntered = getUserInput();
if (codeEntered)
openOptionsMenu();
}
getUserInput() actually occurs with the help of an AlertDialog and an EditText with an attached TextWatcher but the implementation details exceed the scope of this question, unless someone is interested.
In my case it´s
#Override
public void onPanelClosed(int featureId, Menu menu) {
showingMenu = false;
super.onPanelClosed(featureId, menu);
}