Supporting Different Languages

Android will run on many devices in many regions and therefore, the phone will be in different languages. To reach the most users, your application should handle text and graphics in ways appropriate to the locales where your application will be used.

To add support for more languages, create additional values directories inside res/ that include a hyphen and the ISO country code at the end of the directory name. For example, values-es/ is the directory containing simple resourcess for the Locales with the language code “es”. Android loads the appropriate resources according to the locale settings of the device at run time.

Once you’ve decided on the languages you will support, create the resource subdirectories and string resource files. For example:

MyProject/
    res/
       values/
           strings.xml
       values-es/
           strings.xml

Add the string values for each locale into the appropriate file.

At runtime, the Android system uses the appropriate set of string resources based on the locale currently set for the user’s device.

For example, the following are some different string resource files for different languages.

English (default locale), /values/strings.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="title">My Application</string>
<string name="hello_world">Hello World!</string>
</resources>

Spanish, /values-es/strings.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="title">Mi Aplicación</string>
<string name="hello_world">¡Hola Mundo!</string>
</resources>

The same with images

MyProject/
    res/
       drawable/
           background.png
       drawable-es/
           background.xml

You can download this code: Download code

Custom fonts TextView in Android

Custom fonts TextView in Android

Nowadays, than ever before, everybody would like to use custom fonts in android TextViews.
It’s very easy to do it.

First step is to put the font in assets/fonts/Lato_Heavy.ttf

In this example we use Lato_Regular.ttf

Now, we are going to make a custom TextView, like this:

com.thedeveloperworldisyours
 
import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.TextView;
 
public class TextView_Lota_Heavy extends TextView {


    public TextView_Lota_Heavy(Context context) {
        super(context);
        applyCustomFont(context);
    }

    public TextView_Lota_Heavy(Context context, AttributeSet attrs) {
        super(context, attrs);
        applyCustomFont(context);
    }

    public TextView_Lota_Heavy(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        applyCustomFont(context);
    }

    private void applyCustomFont(Context context) {
        Typeface customFont = FontCache.getTypeface("fonts/Lato_Heavy.ttf", context);
        setTypeface(customFont);
    }
}

And FontCache

public class FontCache {
    private static HashMap<String, Typeface> fontCache = new HashMap<>();

    public static Typeface getTypeface(String fontname, Context context) {
        Typeface typeface = fontCache.get(fontname);

        if (typeface == null) {
            try {
                typeface = Typeface.createFromAsset(context.getAssets(), fontname);
            } catch (Exception e) {
                return null;
            }

            fontCache.put(fontname, typeface);
        }

        return typeface;
    }
}

when you want to load one of your custom fonts, you’d say something like:

Typefaces.get("Lato_Heavy");

basically this class ensures Android only loads each font once per instance of your app. the tradeoff, of course, is that each requested typeface object will remain in memory until your app is totally stopped by the OS (even though a given activity may not require each font).

Now we add our custom TextView in our layou.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background" >
  
    <com.thedeveloperworldisyours.TextView_Lota_Regular
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
  
</LinearLayout>

You can download this code: Download code

Percentage of screen in android

For doing percentage of screen in android, we use PercentRelativeLayout:

Subclass of RelativeLayout that supports percentage based dimensions and margins. You can specify dimension or a margin of child by using attributes with “Percent” suffix.

The attributes that you can use are:

  • layout_widthPercent
  • layout_heightPercent
  • layout_marginPercent
  • layout_marginLeftPercent
  • layout_marginTopPercent
  • layout_marginRightPercent
  • layout_marginBottomPercent
  • layout_marginStartPercent
  • layout_marginEndPercent
  • layout_aspectRatio

In our dependencies

compile 'com.android.support:percent:22.2.0'

Follow this example:

<?xml version="1.0" encoding="utf-8"?>
<android.support.percent.PercentRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.thedeveloperworldisyours.percentrelativelayouts.MainActivity">
 
    <TextView
        android:id="@+id/activity_main_text_view"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_centerVertical="true"
        android:background="@android:color/darker_gray"
        android:gravity="center"
        android:text="width 69%"
        app:layout_heightPercent="10%"
        app:layout_marginLeftPercent="4%"
        app:layout_widthPercent="69%" />
 
    <TextView
        android:id="@+id/activity_main_second_text_view"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_centerVertical="true"
        android:layout_toRightOf="@+id/activity_main_text_view"
        android:background="@android:color/holo_red_dark"
        android:gravity="center"
        android:text="width 23%"
        app:layout_heightPercent="10%"
        app:layout_marginRightPercent="4%"
        app:layout_widthPercent="23%" />
</android.support.percent.PercentRelativeLayout>

We can see a big example:
null

<?xml version="1.0" encoding="utf-8"?>
<android.support.percent.PercentRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/activity_calculator_black_transparent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingTop="90dp"
    android:weightSum="5"
    tools:context="com.thedeveloperworldisyours.unitconverterpro.calculator.CalculatorActivity">

    <View
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_centerVertical="true"
        android:background="@drawable/calculator_background"
        app:layout_heightPercent="53%"
        app:layout_marginLeftPercent="2%"
        app:layout_marginRightPercent="2%"
        app:layout_widthPercent="100%" />

    <com.thedeveloperworldisyours.unitconverterpro.common.view.AutoResizeTextView
        android:id="@+id/activity_calculator_result"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_above="@+id/activity_calculator_divide"
        android:layout_toLeftOf="@+id/activity_calculator_remove"
        android:background="@drawable/calculator_text"
        android:inputType="text"
        android:maxLines="1"
        android:gravity="end"
        android:paddingRight="12dp"
        android:paddingLeft="12dp"
        android:textSize="@dimen/activity_calculator_size_text"
        app:layout_heightPercent="10%"
        app:layout_marginLeftPercent="4%"
        app:layout_widthPercent="69%" />

    <ImageButton
        android:id="@+id/activity_calculator_remove"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_above="@+id/activity_calculator_divide"
        android:layout_alignParentRight="true"
        android:src="@mipmap/ic_backspace_calculator"
        android:background="@drawable/calculator_remove_button"
        app:layout_heightPercent="10%"
        app:layout_marginRightPercent="4%"
        app:layout_widthPercent="23%" />

    <!--fourth-->

    <com.thedeveloperworldisyours.unitconverterpro.common.view.StarjediButton
        android:id="@+id/activity_calculator_seven"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_above="@+id/activity_calculator_multiply"
        android:layout_toLeftOf="@+id/activity_calculator_eight"
        android:background="@drawable/calculator_button"
        android:text="@string/activity_calculator_seven"
        android:textSize="@dimen/activity_calculator_size_text"
        app:layout_heightPercent="10%"
        app:layout_marginLeftPercent="4%"
        app:layout_widthPercent="23%" />

    <com.thedeveloperworldisyours.unitconverterpro.common.view.StarjediButton
        android:id="@+id/activity_calculator_eight"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_above="@+id/activity_calculator_multiply"
        android:layout_toLeftOf="@+id/activity_calculator_nine"
        android:background="@drawable/calculator_button"
        android:text="@string/activity_calculator_eight"
        android:textSize="@dimen/activity_calculator_size_text"
        app:layout_heightPercent="10%"
        app:layout_widthPercent="23%" />

    <com.thedeveloperworldisyours.unitconverterpro.common.view.StarjediButton
        android:id="@+id/activity_calculator_nine"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_above="@+id/activity_calculator_multiply"
        android:layout_toLeftOf="@+id/activity_calculator_divide"
        android:background="@drawable/calculator_button"
        android:text="@string/activity_calculator_nine"
        android:textSize="@dimen/activity_calculator_size_text"
        app:layout_heightPercent="10%"
        app:layout_widthPercent="23%" />

    <com.thedeveloperworldisyours.unitconverterpro.common.view.BlackplotanButton
        android:id="@+id/activity_calculator_divide"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_above="@+id/activity_calculator_multiply"
        android:layout_alignParentRight="true"
        android:background="@drawable/calculator_operator_button"
        android:text="@string/activity_calculator_divide"
        android:textSize="@dimen/activity_calculator_size_text_operator"
        app:layout_heightPercent="10%"
        app:layout_marginRightPercent="4%"
        app:layout_widthPercent="23%" />

    <!--third-->

    <com.thedeveloperworldisyours.unitconverterpro.common.view.StarjediButton
        android:id="@+id/activity_calculator_four"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_centerVertical="true"
        android:layout_toLeftOf="@+id/activity_calculator_five"
        android:background="@drawable/calculator_button"
        android:text="@string/activity_calculator_four"
        android:textSize="@dimen/activity_calculator_size_text"
        app:layout_heightPercent="10%"
        app:layout_marginLeftPercent="4%"
        app:layout_widthPercent="23%" />

    <com.thedeveloperworldisyours.unitconverterpro.common.view.StarjediButton
        android:id="@+id/activity_calculator_five"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_centerVertical="true"
        android:layout_toLeftOf="@+id/activity_calculator_six"
        android:background="@drawable/calculator_button"
        android:text="@string/activity_calculator_five"
        android:textSize="@dimen/activity_calculator_size_text"
        app:layout_heightPercent="10%"
        app:layout_widthPercent="23%" />

    <com.thedeveloperworldisyours.unitconverterpro.common.view.StarjediButton
        android:id="@+id/activity_calculator_six"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_centerVertical="true"
        android:layout_toLeftOf="@+id/activity_calculator_multiply"
        android:background="@drawable/calculator_button"
        android:text="@string/activity_calculator_six"
        android:textSize="@dimen/activity_calculator_size_text"
        app:layout_heightPercent="10%"
        app:layout_widthPercent="23%" />

    <com.thedeveloperworldisyours.unitconverterpro.common.view.BlackplotanButton
        android:id="@+id/activity_calculator_multiply"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:background="@drawable/calculator_operator_button"
        android:text="@string/activity_calculator_multiply"
        android:textSize="@dimen/activity_calculator_size_text_operator"
        app:layout_heightPercent="10%"
        app:layout_marginRightPercent="4%"
        app:layout_widthPercent="23%" />

    <!--second-->

    <com.thedeveloperworldisyours.unitconverterpro.common.view.StarjediButton
        android:id="@+id/activity_calculator_one"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_below="@+id/activity_calculator_multiply"
        android:layout_toLeftOf="@+id/activity_calculator_two"
        android:background="@drawable/calculator_button"
        android:text="@string/activity_calculator_one"
        android:textSize="@dimen/activity_calculator_size_text"
        app:layout_heightPercent="10%"
        app:layout_marginLeftPercent="4%"
        app:layout_widthPercent="23%" />

    <com.thedeveloperworldisyours.unitconverterpro.common.view.StarjediButton
        android:id="@+id/activity_calculator_two"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_below="@+id/activity_calculator_multiply"
        android:layout_toLeftOf="@+id/activity_calculator_three"
        android:background="@drawable/calculator_button"
        android:text="@string/activity_calculator_two"
        android:textSize="@dimen/activity_calculator_size_text"
        app:layout_heightPercent="10%"
        app:layout_widthPercent="23%" />

    <com.thedeveloperworldisyours.unitconverterpro.common.view.StarjediButton
        android:id="@+id/activity_calculator_three"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_below="@+id/activity_calculator_multiply"
        android:layout_toLeftOf="@+id/activity_calculator_diminish"
        android:background="@drawable/calculator_button"
        android:text="@string/activity_calculator_three"
        android:textSize="@dimen/activity_calculator_size_text"
        app:layout_heightPercent="10%"
        app:layout_widthPercent="23%" />

    <com.thedeveloperworldisyours.unitconverterpro.common.view.BlackplotanButton
        android:id="@+id/activity_calculator_diminish"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/activity_calculator_multiply"
        android:background="@drawable/calculator_operator_button"
        android:text="@string/activity_calculator_diminish"
        android:textSize="@dimen/activity_calculator_size_text_operator"
        app:layout_heightPercent="10%"
        app:layout_marginRightPercent="4%"
        app:layout_widthPercent="23%" />

    <!--last-->

    <com.thedeveloperworldisyours.unitconverterpro.common.view.StarjediButton
        android:id="@+id/activity_calculator_dot"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_below="@+id/activity_calculator_diminish"
        android:layout_toLeftOf="@+id/activity_calculator_zero"
        android:background="@drawable/calculator_button"
        android:text="@string/activity_calculator_dot"
        android:textSize="@dimen/activity_calculator_size_text"
        app:layout_heightPercent="10%"
        app:layout_marginLeftPercent="4%"
        app:layout_widthPercent="23%" />

    <com.thedeveloperworldisyours.unitconverterpro.common.view.StarjediButton
        android:id="@+id/activity_calculator_zero"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_below="@+id/activity_calculator_diminish"
        android:layout_toLeftOf="@+id/activity_calculator_equals"
        android:background="@drawable/calculator_button"
        android:text="@string/activity_calculator_zero"
        android:textSize="@dimen/activity_calculator_size_text"
        app:layout_heightPercent="10%"
        app:layout_widthPercent="23%" />

    <com.thedeveloperworldisyours.unitconverterpro.common.view.BlackplotanButton
        android:id="@+id/activity_calculator_equals"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_below="@+id/activity_calculator_diminish"
        android:layout_toLeftOf="@+id/activity_calculator_add"
        android:background="@drawable/calculator_equal_button"
        android:text="@string/activity_calculator_equals"
        android:textSize="@dimen/activity_calculator_size_text_operator"
        app:layout_heightPercent="10%"
        app:layout_widthPercent="23%" />

    <com.thedeveloperworldisyours.unitconverterpro.common.view.BlackplotanButton
        android:id="@+id/activity_calculator_add"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/activity_calculator_diminish"
        android:background="@drawable/calculator_operator_button"
        android:text="@string/activity_calculator_plus"
        android:textSize="@dimen/activity_calculator_size_text_operator"
        app:layout_heightPercent="10%"
        app:layout_marginRightPercent="4%"
        app:layout_widthPercent="23%" />
</android.support.percent.PercentRelativeLayout>

Shared preferences

We can say that “shared preferences” initial functionality is worth remembering and share it with other methods or activities. What is really interesting about this feature is that this value will continue recorded and remembered though we close our application or reboot the phone.
For example, many applications we install on our phones have the particularity that it is the first time I run gives us a number of options, if you’ve already performed before, we offer different alternatives. This can be done easily with “shared preferences”.
Another example of this is the recording functionality users, I mean, let us take the example that we are working on an application that will be able to be executed by some registered users and each take a particular condition. Most often in these cases is to access a database, but if we are not talking about a very high number, we can create the characteristics of each user with “shared preference” will be discussed as these permanently remembered.

For this occasion I mounted a simple interface with two buttons and a text field.
The first “Save” button serves to keep within our “shared preferred” the value that we put into our text field. The second “Show” will be in charge of recovering the value at the time we ask.
At this point we recall the important feature of this function. Say we write in our text field “Hello Android” and click the save button thereupon turned our phone, we take it on and run our application again, if directly click on the button “Show”, we will recover the previously saved value “Hello android”.

import android.content.Context;
import android.content.SharedPreferences;

public class PreferencesManager {

    private static final String PREF_NAME = "com.example.app.PREF_NAME";
    private static final String UPDATE = "com.thedeveloperworldisyours.unitconverterpro.UPDATE";

    private static PreferencesManager sInstance;
    private final SharedPreferences mSharedPreferences;

    private PreferencesManager(Context context) {
        mSharedPreferences = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
    }

    public static synchronized void initializeInstance(Context context) {
        if (sInstance == null) {
            sInstance = new PreferencesManager(context);
        }
    }

    public static synchronized PreferencesManager getInstance() {
        if (sInstance == null) {
            throw new IllegalStateException(PreferencesManager.class.getSimpleName() +
                    " is not initialized, call initializeInstance(..) method first.");
        }
        return sInstance;
    }

    public void setUpdate(String date) {
        mSharedPreferences.edit()
                .putString(UPDATE, date)
                .commit();
    }

    public String getUpdate() {
        return mSharedPreferences.getString(UPDATE, "");
    }

    public void remove(String key) {
        mSharedPreferences.edit()
                .remove(key)
                .commit();
    }

    public boolean clear() {
        return mSharedPreferences.edit()
                .clear()
                .commit();
    }
}

In your activity

you can set and get the string

PreferencesManager.getInstance().setUpdate("17/5/2012");

myDate = PreferencesManager.getInstance().getUpdate();

Android, CheckBox and RadioButton Custom Style

android checkbox and radiobutton custom styleYou might want to create your own CheckBox custom style, and RadioButton custom style in Android, for your applications.

This is how you can achieve it, in a few steps:
-Create Image Drawables for their two stater (checked/unchecked).
-Create selector for this two drawables. The sample content should be something like this:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
	<item android:state_checked="true" android:drawable="@drawable/unchecked" />
	<item android:state_checked="false" android:drawable="@drawable/checked" />
</selector>

-Add this selector as android:button attribute for CheckBox or RadioButton

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@drawable/background">

    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:button="@drawable/checkbox_selector"
        android:text="Custom CheckBox" />

    <RadioButton
        android:id="@+id/radioButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:button="@drawable/checkbox_selector"
        android:text="Custom RadioButton" />

</LinearLayout>

You can download this code: download code