Pages

Thursday, March 31, 2016

android improved emulator detection

Detects droid4x, genymotion and the default android studio emulator.

   public static boolean deviceIsEmulator() {  
     int rating = 0;  
     if (Build.PRODUCT.contains("sdk") ||  
         Build.PRODUCT.contains("google_sdk") ||  
         Build.PRODUCT.contains("Droid4X") ||  
         Build.PRODUCT.contains("sdk_x86") ||  
         Build.PRODUCT.contains("sdk_google") ||  
         Build.PRODUCT.contains("vbox86p")) {  
       rating++;  
     }  
     if (Build.MANUFACTURER.equals("unknown") ||  
         Build.MANUFACTURER.equals("Genymotion")) {  
       rating++;  
     }  
     if (Build.BRAND.equals("generic") ||  
         Build.BRAND.equals("generic_x86")) {  
       rating++;  
     }  
     if (Build.DEVICE.contains("generic") ||  
         Build.DEVICE.contains("generic_x86") ||  
         Build.DEVICE.contains("Droid4X") ||  
         Build.DEVICE.contains("generic_x86_64") ||  
         Build.DEVICE.contains("vbox86p")) {  
       rating++;  
     }  
     if (Build.MODEL.equals("sdk") ||  
         Build.MODEL.equals("google_sdk") ||  
         Build.MODEL.contains("Droid4X") ||  
         Build.MODEL.equals("Android SDK built for x86_64") ||  
         Build.MODEL.equals("Android SDK built for x86")) {  
       rating++;  
     }  
     if (Build.HARDWARE.equals("goldfish") ||  
         Build.HARDWARE.equals("vbox86")) {  
       rating++;  
     }  
     if (Build.FINGERPRINT.contains("generic/sdk/generic") ||  
         Build.FINGERPRINT.contains("generic_x86/sdk_x86/generic_x86") ||  
         Build.FINGERPRINT.contains("generic_x86_64") ||  
         Build.FINGERPRINT.contains("generic/google_sdk/generic") ||  
         Build.FINGERPRINT.contains("vbox86p") ||  
         Build.FINGERPRINT.contains("generic/vbox86p/vbox86p")) {  
       rating++;  
     }  
     return rating > 4;  
   }  

Thursday, March 17, 2016

Android multiple fragments with lists, context menu uses same onContextItemSelected method

I have several fragments in one activity, each with a listview. Each listview has context menu on long click. So far all works ok. But when i select an option from the menu, only one fragments onContextItemSelected method is called, and it uses it's activity's list.

How it should work - the onContextItemSelected should return false if it is not the correct one to be used. I found the answer here:

http://stackoverflow.com/questions/25096534/context-menu-in-fragment-uses-listview-from-a-different-fragment-registerforcon

This way all the unwanted methods will return false and the one you need will be invoked.

@Override
public boolean onContextItemSelected(MenuItem item) {
    if (getUserVisibleHint()) {
            //your code
        }
    }
    return false;
}

Thursday, March 10, 2016

Rounded corners problems/error in android sdk prior to v12

So i had a problem when rounded corners were broken on lower sdk's.

For example, you made a drawable/selector/etc for your button with rounded corners on the left and not rounded on the right. Like on picture (1).


If you check how it looks on a device with sdk prior to 12 (I checked on a genymotion 2.3.7 android device), the button will look something like (2). Because, apparently, there is a bug that flips bottom corners.

The fix is very easy. Specify rounded corner values in the dimens.xml file and use them like android:padding="@dimen/attr1" this. Create a values-v12 folder and make a dimens.xml file there.

Sample contents:
<resources><dimen name="name">10dp</dimen></resources>


Here, specify the correct values for each rounded corner. For (1) it would be 0dp for top right, 0dp for bottom right, 10dp for top left, 10dp for bottom left corners.

These values will be used on devices starting with sdk lvl 12.

So in the original dimens.xml you should specify the fixed values to workaround the bug, swap places the bottom corners like this: 0dp top right, 10dp bottom right, 10dp topleft, 0dp bottom left.

This will result in a shape (1).

Oh, btw, the preview in Android Studio anyway shows gibberish with the corners, just ignore it.