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");}



Friday, November 21, 2014

Android: Custom toast example (different text size, different background color)


toast.getView().setBackgroundColor() will change the background color.
toast.getView().findViewById(android.R.id.message).setTextColor() will change the text color.

    private void makeCustomToast(String message) {
        Toast toast = Toast.makeText(mContext, message, Toast.LENGTH_SHORT);
        TextView v = (TextView) toast.getView().findViewById(android.R.id.message);
        toast.getView().setBackgroundColor(mContext.getResources().getColor(R.color.blue));
        v.setTextColor(mContext.getResources().getColor(R.color.white));
        toast.show();
    }

Android: Get app version programmatically example


Get version name & version code

String sVersionName;
int iVersionCode;
try {
    PackageInfo pInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
    sVersionName = pInfo.versionName;
    iVersionCode = pInfo.versionCode;
} catch (PackageManager.NameNotFoundException e) {
    sVersionName = "Unknown";
    iVersionCode = 1;
}

Version name & version code is from what you have defined in AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.example1"
    android:versionCode="14"
    android:versionName="1.2.2">





Friday, September 26, 2014

Support Chinese Translation (Support Traditional & Simplified) but also Hong Kong

Up until now I was using:

"values-zh-rCN" -> Simplified Chinese
"values-zh-rTW" -> Traditional Chinese

I thought I covered entire Chinese. I was wrong!
There's 'zh-rHK' for Hong Kong.

Since I didn't cover Hong Kong Traditional Chinese, it showed default language which is English.

Hong Kong Traditional Chinese is same as Taiwan Traditional Chinese.
So, you should change to:

"values-zh-rCN" -> Simplified Chinese
"values-zh" -> Traditional Chinese (Covering Taiwan, Hong Kong, Macau, and other places)

Why I didn't add 'zh-rHK'?
Because if there is some other Chinese languages that I wasn't aware, then values-zh should cover them all.

Thursday, May 1, 2014

[Android] How to code 'android:background="?android:attr/selectableItemBackground"' programmatically example

You can't (umm.. there is but hard). So there's a work around.

layout\textview_selectable.xml
 <TextView xmlns:android="http://schemas.android.com/apk/res/android"  
   android:background="?android:attr/selectableItemBackground"
   android:layout_width="match_parent"  
   android:layout_height="wrap_content"  
   />  

then in your code:
 TextView tv = (TextView)getLayoutInflater().inflate(R.layout.textview_selectable, null);

[Android] Set style programatically example

You can't. So there's a work around.

layout\textview_mystyle.xml
 <TextView xmlns:android="http://schemas.android.com/apk/res/android"  
   style="@style/myStyleText"  
   android:layout_width="match_parent"  
   android:layout_height="wrap_content"  
   />  

then in your code:
 TextView tv = (TextView)getLayoutInflater().inflate(R.layout.textview_mystyle, null);

Wednesday, April 30, 2014

Java Date long to C# Datetime long


java date long -> C# datetime long:
 TimeSpan ts = TimeSpan.FromMilliseconds(javaNum);  
 DateTime dateTime = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Local);  
 DateTime dateTime2 = dateTime.Add(ts);  
 DateTime final = dateTime2.ToUniversalTime(); // Change to local-time  

C # -> java long
  private static DateTime Jan1st1970 = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);   
     
  public static long CurrentTimeMillis()   
  {   
     return (long) (DateTime.UtcNow - Jan1st1970).TotalMilliseconds;   
  }