app
-
(Android) Delay method call
This is how you could delay a method call for a given amount of time. I’ve used this to have an alert dialog pop up a few second after starting an activity. This will fire myMethod() after 3000 miliseconds: [java] Handler handler = new Handler(); handler.postDelayed(new Runnable() { @Override public void run() { myMethod(); }},…
-
(Android) Check if Google Play Store is installed on device
Here’s a quick way to check if Google Play Store is installed. I use this to keep activities that use in-app billing from crashing on devices that doesn’t have Google Play Store. Works fine in the emulator as well. If the method returns false, I’ll just give the user an alert telling them they need…
-
(Android) Simple confirm dialog
[java] private void confirmDialog() { AlertDialog.Builder builder = new AlertDialog.Builder(context); builder .setMessage("Are you sure?") .setPositiveButton("Yes", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int id) { // Yes-code } }) .setNegativeButton("No", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog,int id) { dialog.cancel(); } }) .show(); } [/java]
-
(Android) Fast Android emulator
This Android emulator is fantastic www.genymotion.com
-
(Android) Custom view orientation change – restore state.
Here’s one pretty simple way to get your custom view to restore its current state after an orientation change: You can of course put any values in there, for example if you need to restore fields. (quickly plotted code. Haven’t run it, but it should work) [java] @Override protected Parcelable onSaveInstanceState() { Bundle bundle =…
-
(Android) Convert Density-independent Pixels to Pixels (dp 2 px)
A simple function to convert dp into pixels. Use when you have to pass pixels to a method and want to keep everything to scale. [java] private int dp2px(int dp) { float scale = getResources().getDisplayMetrics().density; int pixels = (int) (dp * scale + 0.5f); return pixels; } [/java] From Android Developer Center : pxPixels –…
-
(Android) NumberPicker with leading zero
Set your number picker to show leading zeros by applying a formatter to it. Example is using an anonymous instance, but of course it doesn’t have to. [java] myNumberPicker.setFormatter(new NumberPicker.Formatter() { @Override public String format(int value) { return String.format("%02d", value); } }); [/java] For %02d this goes: 0 for filling with zeros 2 for length…
-
Open weblinks in external browser when using Phonegap
To prevent a Phonegap-wrapped app from opening external links in the apps webview, use javascript to open the link in the external browser. This works on Android (probably other platforms as well) [html]<a href="#" data-rel="external" target="_blank" onclick="window.open(‘http://www.jymden.com’, ‘_system’)">Jymden</a>[/html]