I am trying to make a simple little test on an Android emulator called YouWave for Android 4.1.1 (I believe it is).
I am using IntelliJ to generate an APK file to use with the emulator. I simply went into Artifacts and made one Artifact to create an APK file. Didn't do anything else.
The idea of the app is simple. When some code have run, show the results of the code in a little box that I can read and then close when I have read it. But when I fire the application in the emulator it says "Loading 100 %" and then nothing happens. (It worked with two other applications that came with the software so I know it works otherwise.)
Here is the code:
package com.example.DalvikTest;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.content.Context;
public class MyActivity extends Activity {
final Context context = this;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
long time = RunTest();
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Results");
builder.setCancelable(true);
String s = "Time: " + time + " ns";
builder
.setMessage(s)
.setCancelable(false)
.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
MyActivity.this.finish();
}
})
.setNegativeButton("No",new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
public long RunTest() {
long timeStart = System.nanoTime();
long sum = 0;
int[] arr = new int[1000000];
for(int i : arr) {
sum += i;
}
long timeEnd = System.nanoTime();
long result = timeEnd - timeStart;
return result;
}
}
Why is the box with the results now showing up?
I used YouWave some time ago and had the problem of some apps not working too.
Your problem might be in the fact that you are using debugable and development signed apk file that YouWave player doesn't support (It didn't work in older versions at least) and you need to export your file signed with production key. It's quite simple actually because you can generate your own key, considering your rep on website I am guessing you are already familiar with this proces put for reference if someone needs to know everything is explained here http://developer.android.com/tools/publishing/app-signing.html
sorry, i cant comment because i have not enough reputation,
what i want to say is:
i tested youre code and it worked so it should be fine.
and to clean up youre code type this in oncreate:
showDialog();
and under oncreate type youre code like this:
public void showDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Results");
builder.setCancelable(true);
String s = "Time: " + time + " ns";
builder
.setMessage(s)
.setCancelable(false)
.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
MyActivity.this.finish();
}
})
.setNegativeButton("No",new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
Related
I am trying to show a popup, and if the user clicks dont show again, I want to never show it again. However, the dont show again button is not working. I am using shared preferences:
if (dialogPrefs.getBoolean("Show", true) == true) {
new AlertDialog.Builder(this)
.setTitle("Blah")
.setMessage("Blah blah blah ")
.setNegativeButton("Not now", null)
.setNeutralButton("Don't show again", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialogEditor = dialogPrefs.edit();
dialogEditor.putBoolean("Show", false);
dialogEditor.commit();
}
})
.setPositiveButton("Enable", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
enable();
}
}).show();
My preferences and editor are declared in the beginning as such:
SharedPreferences dialogPrefs;
SharedPreferences.Editor dialogEditor;
The shared prefs are initialized in onCreate().
Please let me know what the problem may be.
Thanks,
Ruchir
Your problem is the declaration of the SharedPreferences; it is all declared but...not initialized! Where should the os write your key-value data?
I suggest you to read this Get a Handle to a SharedPreferences
Try this code, I tested it and work:
SharedPreferences dialogPrefs = this.getPreferences(Context.MODE_PRIVATE);
final SharedPreferences.Editor dialogEditor = dialogPrefs.edit();
if (dialogPrefs.getBoolean("Show", true)) {
new AlertDialog.Builder(this)
.setTitle("Blah")
.setMessage("Blah blah blah ")
.setNegativeButton("Not now", null)
.setNeutralButton("Don't show again", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialogEditor.putBoolean("Show", false);
dialogEditor.commit();
}
})
.setPositiveButton("Enable", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Log.i("TAG", "onClick: enable");
}
}).show();
}
}
It should be like this:
if (!dialogPrefs.getBoolean("Show", false)) {//don't show again will work
instead of:
if (dialogPrefs.getBoolean("Show", true) == true) {//this will always show dialog
SharedPreferences.Editor.commit() returns a boolean, indicating the status of write to the actual SharedPreferences object. See if commit() returned true. Also, make sure, you're not editing the same SharedPreference using two Editors. The last editor to commit, will have its changes reflected.
Update Your code works fine, when I run it. I don't see anything wrong in your code. Please make sure you're writing to and reading from the same SharedPreferences.
I've got a really strange problem where on some KitKat devices, my simple yes/no AlertDialog will appear behind the current fragment and not in the foreground. The reason I say the dialog appears behind the current fragment is because the dialog appears in the foreground only after I rotate the device. The app has a MainActivity that switches between different fragments that take up most of the screen.
MainActivity.java
#Override
public void onBackPressed() {
Builder builder = new AlertDialog.Builder(this);
builder.setMessage(getString(R.string.exit_confirm_summary))
.setTitle(getString(R.string.exit_confirm_title))
.setCancelable(true)
.setPositiveButton(getString(R.string.ok),
new OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//...
}
})
.setNegativeButton(getString(R.string.cancel),
new OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//...
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
After doing some research I found that it is best to use DialogFragment when using Fragments in your app, so I changed my code to this:
MainActivity.java
#Override
public void onBackPressed() {
AlertDialogFragment adf = new AlertDialogFragment();
adf.setRetainInstance(true);
adf.show(getFragmentManager(), "dialog");
}
AlertDialogFragment.java
public class AlertDialogFragment extends DialogFragment {
public AlertDialogFragment() {}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return new AlertDialog.Builder(getActivity())
.setCancelable(false)
.setTitle("Alert DialogFragment")
.setMessage("AlertDialogFragment Test")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// ...
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// ...
}
}).create();
}
}
However, the effect is still the same. The dialog should appear when I press the back button, but is only visible after I press the back button and then rotate the device. It also becomes visible after I go home and come back into the app. I've noticed it only happens on a few devices but I'd like to get rid of this problem for good.
Note: this behavior happens for all dialogs in the app, not just this one.
Anyone have any ideas what is going on?
I'm trying to make a extern class for AlertDialog. I want to have an universal class to use it quickly.I know the code isn't difficult at all, but there are anyhow many rows to write (or copy) and if I would find a mistake I maybe had to change many code...
I've everything but one thing I don't get.
So it works but returning the correct onClick doesn't work.
I've also tried to make an while loop before returning, but then the app is hanging....
Has somebody any idea?
public class RalaAlertDialog{
private static AlertDialog.Builder alertDialog;
private static long onClick=RalaInterfaceDefault.FehlerSpezialZahl;
//neutralButton
public static long AlertDialogNeutral(Context class_this, String mssg, String ntrlBttnTxt, boolean dismissable, String title){
onClick=RalaInterfaceDefault.FehlerSpezialZahl; //default error number
alertDialog=new AlertDialog.Builder(class_this);
if(mssg.equals("")){
mssg="DEFAULT-TEXT";
}
if(title.equals("")){
title="DEFAULT-TITLE";
}
if(ntrlBttnTxt.equalsIgnoreCase("")){
System.out.println("No values set - default in use.");
ntrlBttnTxt="OK";
}
alertDialog.setMessage(mssg)
.setCancelable(dismissable);
alertDialog.setTitle(title);
alertDialog.setPositiveButton(ntrlBttnTxt,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id){
onClick=0;
dialog.dismiss();
}
}
);
AlertDialog a=alertDialog.create();
a.show();
//wait until button is click before continuing
return onClick;
}
public static AlertDialog getAlertDialog(Context ctx, String title, String message, String posButton, boolean dismissable, final DialogInterface.OnClickListener ocl) {
AlertDialog.Builder builder =new AlertDialog.Builder(ctx);
builder.setTitle(title)
.setMessage(message)
.setCancelable(dismissable)
.setPositiveButton(posButton,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id){
dialog.dismiss();
if(ocl!=null) ocl.onClick(dialog, id);
}
});
AlertDialog dialog = builder.create();
return dialog;
}
Use it like this :
AlertDialog dialog = getAlertDialog(this,"Hello","World","OK",false,new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Log.i("DIALOG","OK Clicked");
}
});
dialog.show();
Of course you need only one OnClickListener, but I like it better that way.
I'm new to Java/ Android development (I started learning last night) so it is entirely possible I'm doing something horrendously stupid. However, after more than an hour Googling I've come up with nothing. I'm using Eclipse as my editor.
I'm reading the docs here for AlertDialog, which gives an example:
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();
}
}
I originally re-wrote it so I can start committing some of the methods to memory, but got an error "FragmentAlertDialog cannot be resolved to a type". I hit Ctrl+Shift+O to make sure I had the proper imports, but still it didn't go away.
So I copied/ pasted the example code and did the following, in this order:
Hit Ctrl+Shift+O to get the imports right (using android.app.DialogFragment, not android.support.v4.app.DialogFragment)
Declared my package at the top
Replaced R.string.alert_dialog_ok and R.string.alert_dialog_cancel with android.R.string.ok and android.R.string.cancel respectively
Removed setIcon(), as I don't have an icon to put in yet
I'm still getting errors:
FragmentAlertDialog cannot be resolved to a type (x4)
Illegal modifier for the class MyAlertDialogFragment; only public, abstract & final are permitted
Am I doing something wrong, or is there something wrong with the example code?
1.FragmentAlertDialog
Make sure the Activity you want to cast to is named FragmentAlertDialog. Make sure to also save everything - sometimes Eclipse won't make the connection until everything is saved.
2.Illegal modifier for the class MyAlertDialogFragment; only public, abstract & final are permitted
Take out the static modifier:
public class MyAlertDialogFragment extends DialogFragment {
or keep static and move this Fragment so it is enclosed within the Activity you want. This means that MyAlertDialogFragment should be inside your Activity, before that Activity's closing brace.
I'm new to Java/Android development
Don't start off with something so complicated. Learn Java then move to Android.
Hi try these code to implement alert dialog
AlertDialog.Builder alert2 = new AlertDialog.Builder(this);
alert2.setTitle("Your Title");
alert2.setMessage("Your Messages");
final EditText input2 = new EditText(this);
input2.setInputType(InputType.TYPE_CLASS_PHONE);
alert2.setView(input2);
alert2.setPositiveButton(GButton, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Do something with value!
try
{
// do your stuff here
}
catch(Exception e)
{
}
}
});
alert2.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Canceled.
}
});
alert2.show();
Idea taken from Android: Blurring and dimming background windows from dialog. I'm having trouble getting the content under my dialog to blur. When calling eula.getWindow() I receive this error:
The method getWindow() is undefined for the type AlertDialog.Builder
The eula is displayed with this bit of code from the main activity:
EulaHelper.showEula(false, this);
Any help is greatly appreciated.
public static void showEula(final boolean accepted, final FragmentActivity activity) {
AlertDialog.Builder eula = new AlertDialog.Builder(activity)
.setTitle(R.string.eula_title)
.setIcon(android.R.drawable.ic_dialog_info)
.setMessage(activity.getString(R.raw.eula))
.setCancelable(accepted);
if (accepted) {
// If they've accepted the EULA allow, show an OK to dismiss.
eula.setPositiveButton(android.R.string.ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
} else {
// If they haven't accepted the EULA allow, show accept/decline buttons and exit on
// decline.
eula
.setPositiveButton(R.string.accept,
new android.content.DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
setAcceptedEula(activity);
dialog.dismiss();
}
})
.setNegativeButton(R.string.decline,
new android.content.DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
activity.finish();
}
});
}
eula.show();
WindowManager.LayoutParams lp = eula.getWindow().getAttributes();
lp.dimAmount = 0.0F;
eula.getWindow().setAttributes(lp);
eula.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
}
getWindow() is a method of the dialog class, not of the dialog builder. Your code should rather look like this:
AlertDialog dlg = eula.show();
WindowManager.LayoutParams lp = dlg.getWindow().getAttributes();
lp.dimAmount = 0.0F;
dlg.getWindow().setAttributes(lp);
dlg.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
Note though that the FLAG_BLUR_BEHIND constant is deprecated now, blurring behind windows is no longer supported. So your code might break in the future.
eula is the Builder, not the dialog itself. Try:
final AlertDialog eulaDialog = eula.create();
eulaDialog.show();
WindowManager.LayoutParams lp = eulaDialog.getWindow().getAttributes();
lp.dimAmount = 0.0F;
eulaDialog.getWindow().setAttributes(lp);
eulaDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);