The buttons in my android application are unresponsive - java

I am trying to implement a dialog box that pops up when a button is pressed by the user. This currently works, however the buttons I have included in the pop up are unresponsive. I have tried the following code to try and resolve the problem.
public void showDialog()
{
final Dialog dialog= new Dialog(context);
dialog.setContentView(R.layout.dialog_info);
infoView=(EditText) dialog.findViewById(R.id.infoView);
infoView.setFocusable(false);
infoView.setText("");
dialog.setTitle(aList.get(count).toTitle());
infoView.append(aList.get(count).toDescription());
Button back=(Button)findViewById(R.id.back);
Button reminder=(Button)findViewById(R.id.reminder);
Log.e(TAG,"Testing click 1.5");
back.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
Log.e(TAG,"Testing click 2");
dialog.dismiss();
}
});
dialog.show();
}
showDialog() gets called after a button (back) in the first view is pressed
public void onClick(View v) {
for (count =0;count<aList.size();count++)
{
if (v==buttons.get(count))
{
Log.e(TAG,"Testing click -1.1");
showDialog();
}
}
}

Correct me if I understood wrong,
your button back and reminder are a part of the dialog, so you should be getting a null pointer exception. Try finding your buttons like this
Button back=(Button)dialog.findViewById(R.id.back);
Button reminder=(Button)dialog.findViewById(R.id.reminder);
EDIT: By doing this you'll find the button inside the dialog. If you don't do this android will try to find the button in Activity itself and not the dialog .
For the Override problem please recheck that you've done the right import. (there are two kinds of import for View.onClickListner I don't remember their name right now.) You can delete your import associated with View.Onclick and try to re-import the correct package.
If this is not the case then I might have misinterpreted your question. You can check your Java Compliance level and see if its on 1.6 or not. You can check this by going to your project properties under Java Compliance Level

What version of Java are you using?
Right-click your project
Go to Properties
Go to Java Compiler
Enable project specific settings
Select 1.6 for Java compliance level
This will fix the #Override errors. If you want to use another version, you can just remove all #Override annotations.

Related

Cannot dismiss 2 AlertDialogs at once in Android

I've built an android AlertDialog (named dialog) that fires other Android AlertDialog (named dialog2) if some conditions happen.
I've checked that what if only one of them is displayed on screen it is dismissed without any problem.
Problem comes when both of them are showing, that happens when I press the OK button for the second dialog, it just closes the second dialog, despite the first dialog is even showing on screen.
This is the code related to dialog2 for that operation:
dialog2.setOnShowListener(new DialogInterface.OnShowListener()
{
#Override
public void onShow(final DialogInterface dialog)
{
Button button = ((AlertDialog)
dialog).getButton(AlertDialog.BUTTON_POSITIVE);
button.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
[some more operations]
dialog2.dismiss();
dialog.dismiss();
}
});
}
});
The strangest thing is that if I supress the line dialog2.dismiss(); by leaving only dialog.dismiss(); what get dismissed is the second dialog, not the first one, looks like android somehow confuses one with the other, and I don't think that should be happening because they are created separately like this:
dialog=[code to create that dialog]
dialog2=[code to create that dialog]
Doing that the only way I see that app would close dialog2 when asked to close dialog would be doing dialog=dialog2, which I am not. I think They should be different objects loaded in memory each one with their characteristics.
I don't see any reason of why this is happening, seems like a clueless error from my point of view. Hope you can give ideas about what is happening.
A couple things to take note of here:
First, it isn't necessary to create an "onShowListener", unless you actually need to perform a task when the dialog is shown, this code should help you correctly create an AlertDialog:
new AlertDialog.Builder(getContext())
.setTitle(R.id.dialog_title)
.setMessage(R.id.dialog_message)
.setPositiveButton(R.id.positive_text, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
//do onClick stuff here
}
})
.show();
This example does all your setup at once. If you need a reference to this dialog or don't want to show it right away, just use AlertDialog.Builder dialog1 = new ..., and then use dialog1.show() to create the dialog.
Second, the reason that only the second dialog is closing when you suppress dialog2.dismiss() is because there is a local variable named 'dialog' inside of your onShow() method (look at the method parameters) that is taking precedence over your broader scoped 'dialog' variable.
Third, to answer your actual question, can you dismiss the first dialog just before you show the second? I don't see any real reason to have 2 dialogs open at the same time.

First addCommand() item in a Form fails when used with native theme

I have encountered a problem when using the the addCommand() method of the Form class along with the Native theme - other themes work fine. See the following example:
Form hi = new Form("Hi World");
hi.addComponent(new Label("Hi World"));
// with native theme - can't click on the first command in the list
hi.addCommand(new Command("Dummy1") {
public void actionPerformed(ActionEvent ev) {
Dialog.show("Dummy1 Clicked!", "You clicked the Dummy1", "OK", null);
}
});
hi.addCommand(new Command("Dummy2") {
public void actionPerformed(ActionEvent ev) {
Dialog.show("Dummy2 Clicked!", "You clicked the Dummy2", "OK", null);
}
});
hi.show();
When I create an application using the code above, a click on the second command ("Dummy2") produces the expected Dialog, but a click on the first command ("Dummy1") does nothing.
This only happens when using the Native theme. If I switch to Flat Blue, then clicking on either command produces the expected Dialog.
This behavior happens both on the Simulator and on a real Android device (don't know about iOS).
Fyi, my toolchain is NetBeans IDE v8.2, Java 1.8.0_25, with the CodenameOne plugin v3.6.0.
Has anyone else seen this? Am I missing something? If so, is there a workaround?
If the element is very narrow and very close to the top the click might be misinterpreted as a click out of bounds or on the status bar area. You need to set the styling of the SideCommand to have a sensible default as this element is very application specific. Otherwise touches might be lost.
I tried styling the SideCommand but it didn't seem to help. What worked for me was to define a style for TitleArea and simply uncheck Derived for the Padding settings (I left them all set to 0px).
I have no idea why this works - I would have thought that the derived values would have been zero in any case.

Android Back Button deleting activity for good

I am making a game, and when a level is complete I have a pop-up activity which tells you your score.
When I press the back button I want this instance of the pop-up activity to be deleted for good.
Currently I get the behaviour;
Finish level, pop-up appears
I press back button to get rid of it
Start a new level, finish it and new pop-up appears
If I press the back button twice, the new pop-up disappears and the old one re-appears.
I want to delete the old pop-up as soon as it is hidden.
I am using the following code in my pop-up activity;
#Override
public void onBackPressed() {
finish();
super.onBackPressed();
}
Any help would be much appreciated.
Thanks!
You can use this code. It clears off the PopupActivity form the Top Activity Stack so that it will not appear again.
#Override
public void onBackPressed() {
Intent removeIntent = new Intent(GameActivity.this, PopUpActivity.class);
removeIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(removeIntent);
}

Android: Basic Dialog Implementation

I have searched and found a lot of different answers for this question, but nothing quite settles it for me. Total android n00b, and I ask for your patience in advance.
I'm having trouble dealing with a FileChooser problem with Android KitKat. As per [here][1], and according to [Steve N][2] i get the impression that this file chooser problem is caused by my android version (4.4.2)
Given that filechooser isn't working, I want to implement a basic dialog. I'm going to check the android version number for the device, and then display a message, citing the current lack of support, if the version number comes back with a 4.4 in front.
At present, i'm just using toast
public boolean checkVersionSupport(){
if (Build.VERSION.RELEASE.equals("4.4.2") {
Toast toast = Toast.makeText(context, androidOS, duration);
toast.show();
}
}
Instead of toast, I'd like a simple, one button dialog box to open, with an OK button, which I will use to redirect the user out of the native app and off to chrome, where the whole file chooser thing seems to be working.
Couple of things that I have found difficult after reading through the android developer materials.
Do I need a layout XML file for the dialog box?
Where do I put the class file for the MyAlertDialogFragment class I am creating? Can it be anywhere in the java folder or does it have to be in a sub folder java/com.myproject... And what impact does that have for importing the class into my java activity file?
Can someone please explain where FragmentAlertDialog comes from in the Android Developer Materials.
public static class MyAlertDialogFragment extends DialogFragment {
public static MyAlertDialogFragment newInstance(int title) {
MyAlertDialogFragment frag = new MyAlertDialogFragment();
Bundle args = new Bundle();
args.putInt("title", title);
frag.setArguments(args);
return frag;
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
int title = getArguments().getInt("title");
return new AlertDialog.Builder(getActivity())
.setIcon(R.drawable.alert_dialog_icon)
.setTitle(title)
.setPositiveButton(R.string.alert_dialog_ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
((**FragmentAlertDialog**)getActivity()).doPositiveClick();
}
}
)
.setNegativeButton(R.string.alert_dialog_cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
((**FragmentAlertDialog**)getActivity()).doNegativeClick();
}
}
)
.create();
}
}
What's best practice for icons? I see a debate going on about this, but for the simple purpose of having an icon at the top of a dialog, for one single basic usage, what would be the quickest way of getting it done? I'm just copying files into the res folder and referencing them... 48dp? This might be a whole different question.
Do I need a layout XML file for the dialog box?
That honestly depends on which type of Dialog you use.
The AlertDialog does not require a specified layout, all you need is to use AlertDialog.Builder() and set the positive/negative buttons, and how it handles that.
The DialogFragment onCreateDialog() method is the preferred way of using an AlertDialog. It also doesn't require an XML layout, but as such, its layout is still restricted to essentially a Yes/No option: look here for complete example
The DialogFragment onCreateView() method allows you to create a dialog from a specified layout XML. So if you wish to customize the view of the dialog beyond "title, description, yes/no", then yes, you need to specify the XML file for the dialog fragment, look here for an example, although the advice says you should look into ButterKnife and Otto libraries to make it even better.
Where do I put the class file for the MyAlertDialogFragment class I am creating? Can it be anywhere in the java folder or does it have to be in a sub folder java/com.myproject... And what impact does that have for importing the class into my java activity file?
Anywhere inside the project. Although I prefer to put it in something like <projectroot>/presentation/fragments/dialog, package-wise.
Can someone please explain where FragmentAlertDialog comes from in the Android Developer Materials.
FragmentAlertDialog is an assumed Activity from which the MyAlertDialogFragment dialog fragment is shown, and is assumed to have the methods doPositiveClick() and doNegativeClick(). The title int that is provided is most likely a string resource defined in /res/values/strings.xml which is used for localization. For example, R.string.fancy_title_name.
So it's something like this
public class FragmentAlertDialog extends AppCompatActivity {
#Override
public void onCreate(Bundle saveInstanceState) {
super.onCreate(saveInstanceState);
setContentView(R.layout.activity_fragment_alert_dialog);
//stuff
}
#Override
public void onPostResume() {
MyAlertDialogFragment madf = MyAlertDialogFragment.newInstance(R.string.something);
madf.show(getSupportFragmentManager(), "alert-dialog-fragment");
}
}
Otherwise, MyAlertDialogFragment just extends from DialogFragment and overrides onCreateDialog to create an AlertDialog inside this DialogFragment.
In case you'd ask, the newInstance() method is so that you would NOT use a parametrized constructor. Fragments should not have parametrized constructors, they ought to receive their data in the setArguments(Bundle) method.
What is the best practice for icons?
Refer to the material design guidelines.

AlertDialog OnClose Event Android

I have been looking around and I couldn't find anything, so I don't know if its possible, but is there a way to do something on close for alertdialogs?
In my situation, I have the background UI paused while the dialog is up as its a pause screen. If you click the buttons in the dialog it will run the onClick stuff, so I have it unpausing there, but if they click off of the dialog it closes the dialog but doesn't run onClick. Is there anyway to get something to run when they click off to the side? Thanks!
You can set an onDismissListener() to listen whenever the dialog goes away. Docs.
You can use onDismissListener() but it was introduced in higher APIs so will not be compatible with older ones. You can use onCancel() instead.
AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
alertDialog.setTitle("My Titile");
alertDialog.setOnCancelListener(new OnCancelListener() {
#Override
public void onCancel(DialogInterface dialog) {
//your logic
}
});
The only way to do this is to not use a AlertDialog.Builder and instead create a PopupWindow sub class.
A good example may be found here on line 37 :
https://github.com/lorensiuswlt/NewQuickAction/blob/master/src/net/londatiga/android/PopupWindows.java
You can use setCanceledOnTouchOutside(false) to force the user to click on the cancel button.
You could also (if you're using a DialogFragment) override the onDetach() method.
And finally you could set a dismiss listener.

Categories