Skip to content

Instantly share code, notes, and snippets.

@Kursulla
Created March 13, 2014 15:15
Show Gist options
  • Save Kursulla/9530370 to your computer and use it in GitHub Desktop.
Save Kursulla/9530370 to your computer and use it in GitHub Desktop.
Small utils for Android
import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
/**
* Created by kursulla on 3/13/14.
*/
public class AndroidUtil {
public static boolean isTablet(Context context) {
boolean xlarge = ((context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == 4);
boolean large = ((context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE);
return (xlarge || large);
}
/**
* Set only portrait orientation for phones, and only landscape orientation for tablets
*
* @param activity Activity for accessing system resources
*/
public static void setScreenOrientation(Activity activity) {
if (AndroidUtil.isTablet(activity)) {
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
} else {
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment