Tuesday, August 11, 2015

Android: Detect 7" or 10" tablet programmatically (get smallest width dp)


Some other solutions out there are outdated.

This solution is when you are targeting API 13+. (You should now be targeting 14+ - written in 2015.08.11)

     int smallestDp = getResources().getConfiguration().smallestScreenWidthDp;  
     if(smallestDp >= 600 && smallestDp < 720) { // 7" ~ <10"  
     }  
     else if(smallestDp >= 720) { // 10"+  
     }  



Thursday, July 23, 2015

Android: adjustViewBounds="true" doesn't work on some devices. Here's the solution.

I am so disappointed on Android platform.

       <ImageView  
         android:layout_width="match_parent"  
         android:layout_height="wrap_content"  
         android:adjustViewBounds="true"  
         android:scaleType="fitCenter"  
         android:src="@drawable/photo" />  

If you want to have an image's width matches to the screen width and have the height change dynamically. Above is how to code. (scaleType="fitXY") also works.

Not so simple!

Although the code works on the most Android devices, it doesn't work on some devices (such as Samsung S4).

Here's the solution:
       <com.example.code.KeepRatioImageView  
         android:layout_width="match_parent"  
         android:layout_height="wrap_content"  
         android:scaleType="fitCenter"  
         android:src="@drawable/photo" />  


 public class KeepRatioImageView extends ImageView {  
   public KeepRatioImageView(final Context context, final AttributeSet attrs) {  
     super(context, attrs);  
   }  
   @Override  
   protected void onMeasure(final int widthMeasureSpec, final int heightMeasureSpec) {  
     final Drawable d = this.getDrawable();  
     if (d != null) {  
       final int width = MeasureSpec.getSize(widthMeasureSpec);  
       final int height = (int) Math.ceil(width * (float) d.getIntrinsicHeight() / d.getIntrinsicWidth());  
       this.setMeasuredDimension(width, height);  
     } else {  
       super.onMeasure(widthMeasureSpec, heightMeasureSpec);  
     }  
   }  
 }  

Tuesday, July 21, 2015

Android: Get device screen refresh rate



Typically most devices have 60fps refresh rate. (eg. Nexus 5, Samsung S5)
Here's how to find the refresh rate of a device.
 Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();  
 float refreshRating = display.getRefreshRate();  

Tuesday, June 9, 2015

Converting array to list in Android


Simple solution:

public void arrayToList() {

    String sArray[] = new String[]{"A", "B", "C"};
    // This will work as is. However, if you try to add another 
    // String then you will get java.lang.UnsupportedOperationException    
    List lList = Arrays.asList(sArray);

}

However, you can't add another value if you just use asList().


Solution: use new ArrayList()
public void arrayToList() {

    String sArray[] = new String[]{"A", "B", "C"};
    List lList = new ArrayList(Arrays.asList(sArray));        // now add(String) works    lList.add("D");}