TheJadav

Check language is supported in android device 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 - Android

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.

Share this content:

Share:

More Posts

Introduction to Kotlin: A Versatile and Modern Programming Language

Kotlin, a versatile and modern programming language, offers developers a concise, safe, and interoperable coding experience. With features like null safety, extension functions, and coroutines, Kotlin enhances productivity and readability, making it an attractive choice for Android development and beyond. Whether you’re new to programming or an experienced developer, exploring Kotlin opens up a world of possibilities for building robust and expressive applications.

Mastering the Android Activity Lifecycle: A Comprehensive Guide

The Android Activity Lifecycle is a fundamental concept for Android developers. It defines how an Activity behaves during its lifecycle, from creation to destruction. Understanding the Activity Lifecycle is crucial for managing resources efficiently and delivering a smooth user experience. In this blog post, we’ll briefly introduce the different states an Activity can be in and the main callback methods associated with each state. Let’s dive in and explore this important aspect of Android app development!

Table of Contents

Send Us A Message