Tag : android

If you play around with height and width for a android device you often need to convert between real pixel (px) and device indipenden pixel (dp, dip). The following methods are a easy solution to convert between px to dp private float px2Dp(float px, Context ctx) { return px / ctx.getResources().getDisplayMetrics().density; } private float dp2Px(float ..

Read more

Short answer Yes Long answer Shared Preference are saved in the file system of your device, and the Shared Preferences Mode defines whether other applications have the right to read or write the Shared Preferences or not. Android SharedPreferences with mode “MODE_PRIVATE”, “MODE_WORLD_READABLE” and “MODE_WORLD_WRITABLE” are possible in android. MODE_PRIVATE This is the default mode, ..

Read more

Sometimes it can happen, that the app has to prevent android from taking a screenshot of the app. This can make sense if the app is being pushed into the background and/or for security reasons. So use FLAG_SECURE: getWindow().setFlags(LayoutParams.FLAG_SECURE, LayoutParams.FLAG_SECURE); This line secures against manual screenshots and automatic screenshots from the ICS recent-tasks history. You ..

Read more

If you want to try downloading something unless you have wifi. You can use the following code snippet to be able to check if wifi is enabled. You will be able to use the ConnectivityManager to get the state of the Wifi adapter. From there you can check if it is connected or even available. ConnectivityManager ..

Read more

A dialog can have a custom layout with buttons, text views, list, images, checkboxes, etc. You can create a layout for your dialog and show the dialog with the following simple method. The showDialog method can be a part of your activity class. In that case you don’t need to explicitly declare the context as ..

Read more

With the getAll function of sharedPreferences it is possible to save the shared preferences to a file. The following code example is probably the easyest way to do it. With the saveSharedPreferences method you can save all shared preferences a file to the root of your sdcard. Make sure your App has the following permission. ..

Read more

Google has started to use ripple animations in Material Design UIs. The setHotspot() method is added in API Level 21. This teaches the drawable a “hot spot”, and the RippleDrawable apparently uses this as the emanation point for the ripple effect. The setHotspot() method of a view take the x,y values from the MotionEvent to ..

Read more

If you have to create an object with default constructor, you can invoke the newInstance() method on a Class. This simple class creates the instance of a named class using the empty default constructor by calling the newInstance method: public class ReflectionTester throws InstantiationException, IllegalAccessException, ClassNotFoundException { public static Object createObject(String className) { return Class.forName(className).newInstance(); ..

Read more

Request to hide the soft input window from the context of the window that is currently accepting input. This should be called as a result of the user doing some actually than fairly explicitly requests to have the input window hidden. /** * This method hides the soft input window, if it’s open. * * ..

Read more