Posts

How to check android application in background or not?

Check if application in background or not snippet public static boolean isApplicationInBackground(Context context) { ActivityManager am = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE); List taskList = am.getRunningTasks(1); if (taskList != null && !taskList.isEmpty()) { ComponentName topActivity = taskList.get(0).topActivity; if (topActivity != null && !topActivity.getPackageName().equals(context.getPackageName())) { return true; } } return false; } whether the application is in the background we need to define permission android.permission.GET_TASKS in Manifest.xml This function will return true if the application in the background otherwise it will return false.

Android status bar and navigation bar height

Android NavigationBar Height Snippet public static int getNavigationBarHeight() { int result = 0; int resourceId = mApplicationContent.getResources().getIdentifier("navigation_bar_height", "dimen", "android"); if (resourceId > 0) { result = mApplicationContent.getResources().getDimensionPixelSize(resourceId); } return result; } Android Phone Statusbar height snippet public static int getStatusBarHeight() { int result = 0; int resourceId = mApplicationContent.getResources().getIdentifier("status_bar_height", "dimen", "android"); if (resourceId > 0) { result = mApplicationContent.getResources().getDimensionPixelSize(resourceId); } return result; }

How to check internet connection is available or not in android

Declare Permission in manifest file <uses-permission android:name="android.permission.INTERNET"> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE">     Now Check using this function is network available or not public static boolean isNetWorkAvilable() { ConnectivityManager connectivityManager = (ConnectivityManager) mApplicationContent .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo(); if (activeNetInfo == null || !activeNetInfo.isAvailable()) { return false; } else { return true; } }

How to save bitmap to file?

Save bitmap to file snippet public static String SaveToFile(Bitmap croppedImage,String fileName) { String directory="CanvasImages"; String path=Environment.getExternalStorageDirectory()+"/"+directory; File folder = new File(path); if(!folder.exists()) { folder.mkdirs(); } File mediaFile = new File(folder,fileName); try { FileOutputStream out = new FileOutputStream(mediaFile); croppedImage.compress(Bitmap.CompressFormat.PNG,100,out); out.close(); } catch (Exception e) { e.printStackTrace(); } return mediaFile.toString(); }

how to scale font size for different screen size in android?

Scale font size for different support device Different screen density using the following size scale: xxxhdpi : 4.0 xxhdpi : 3.0 xhdpi : 2.0 hdpi : 1.5 mdpi : 1.0 (baseline) ldpi : 0.75 A set of six generalized  densities : ldpi  (low) ~120dpi mdpi  (medium) ~160dpi hdpi  (high) ~240dpi xhdpi  (extra-high) ~320dpi xxhdpi  (extra-extra-high) ~480dpi xxxhdpi  (extra-extra-extra-high) ~640dpi Below function use for convert dp(Density indipendent pixel) to PX. public static int dp2px(Context context, float dpValue) { final float scale = context.getResources().getDisplayMetrics().density; return (int) (dpValue * scale + 0.5f); } Now We are going to set dynamic font size for all device  Note: It will work only on the device, not on the tablet. we need to write different logic for the tablet. textView.setTextSize(TypedValue. COMPLEX_UNIT_PX , dp2px ( this , 12f )); How It works on different screen....

How to show,hide and close soft keyboard in Android

Android Soft Keyboard Show,Hide, and Close Method to Show Soft Keyboard  public static void showSoftKeyboard(View view,Context context) { if (view.requestFocus()) { InputMethodManager imm = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE); imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT); } } Method to Hiding Soft Keyboard public static void hideSoftKeyboard(View view,Context context) { if (view.requestFocus()) { InputMethodManager imm = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(view.getWindowToken(), 0); } } Method to Close Soft Keyboard public static void closeKeyboard(Context c, IBinder windowToken) { InputMethodManager mgr = (InputMethodManager) c.getSystemService(Context.INPUT_METHOD_SERVICE); mgr.hideSoftInputFromWindow(windowToken, 0); }

Android Platform Codenames, Versions, API Levels

Image
Android Platform Codenames ,Versions, and API Levels References Links: https://source.android.com/source/build-numbers.html#platform-code-names-versions-api-levels-and-ndk-releases