August 13, 2021

Check language is supported in android device or not

By lj007

Check language is supported in android device or not

Today, multi-language application is very common. Everyone want his application is usable by all people regardless person knows specific language or not. I am developing one application with multiple language support, so I need to check whether language is supported by particular device or not. So for that I have find one useful method to check language is supported or not.
This method tells you whether device supports particular language or not.

public static boolean isSupported(Context context, String text) {
        int w = 200, h = 80;

        Resources resources = context.getResources();
        float scale = resources.getDisplayMetrics().density;
        Bitmap.Config conf = Bitmap.Config.ARGB_8888;
        Bitmap bitmap = Bitmap.createBitmap(w, h, conf); // this creates a MUTABLE bitmap
        Bitmap orig = bitmap.copy(conf, false);
        Canvas canvas = new Canvas(bitmap);
        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setColor(Color.rgb(0, 0, 0));
        paint.setTextSize((int) (14 * scale));

        // draw text to the Canvas center
        Rect bounds = new Rect();
        paint.getTextBounds(text, 0, text.length(), bounds);
        int x = (bitmap.getWidth() - bounds.width()) / 2;
        int y = (bitmap.getHeight() + bounds.height()) / 2;

        canvas.drawText(text, x, y, paint);
        boolean res = !orig.sameAs(bitmap);
        orig.recycle();
        bitmap.recycle();
        return res;
}

To use this method you need to pass string in particular language. To display language list check for every language like below:

Ex.

if (isSupported(baseContext, "हिन्दी"))
    languageList.add("हिन्दी")

How this method works?

It creates an empty bitmap file and another bitmap file with text drawn on it. If device supports language than it draws text on second bitmap or else not. Then it compare those two bitmaps. If both are same, means text is not drawn on bitmap, then language is not supported.