Friday, December 2, 2016

Ensuring customizable text in toast - Android coding

So you want to write an app that can be used in different languages

So every text in the app needs to be customizable to the language

Say you are creating a toast, You don't want to programatically hardcode the text else it won't customize
here is a simple function that allows you to keep the text in strings.xml and create a toast
sampletext is the name of string defined in strings.xml

Strings.xml
<resources>
    <string name="sampletext">Hello, person</string>
</resources>


Your activity

Define a separate function in your activity
public static void ToastMemoryShort (Context context) {

    Toast.makeText(context, context.getString(R.string.sampletext), Toast.LENGTH_SHORT).show();
    return;
}

Just call this function in your activity where required using the following

ToastMemoryShort(getApplicationContext());

Hope this helped you. Let me know :)

No comments:

Post a Comment