Tuesday 19 December 2017

Change Language Programmatically in Android

While developing your awesome application, sometimes you are required to add a feature to change the language of your app on the fly. However, Android OS does not directly support this behaviour. And therefore, you need to solve this situation in some other ways.

Android by default uses the locale of the device to select the appropriate language
dependent resources. And most of the time this behaviour is enough for common applications.


There are some difficulties which you have to overcome to change the language programmatically. 

  1. Your application will not remember your language change after it is closed or recreated during the configuration change. 
  2. You should update currently visible UI properly according to the selected language. 


// set language prefix in shared preference

LANGUAGE_TYPE = "en" || "hi" || "gu" etc..

Utils.setStringPreference(activity, AppConstants.PREF_APP_LANGUAGE, LANGUAGE_TYPE);
// get language prefix from shared preference and set to Locale of Android System
public void setLanguage() {

    String defaultLANG = Locale.getDefault().getLanguage();

    String languageToLoad = "en"; // your language
    switch (Utils.getStringPreference(MainActivity.this, AppConstants.PREF_APP_LANGUAGE, "0")) {

        case "0":
            languageToLoad = "en";
            break;

        case "1":
            languageToLoad = "hi";
            break;

        case "2":
            languageToLoad = "gu";
            break;
    }

    Locale locale = new Locale(languageToLoad);
    Locale.setDefault(locale);
    Configuration config = new Configuration();
    config.locale = locale;
    getBaseContext().getResources().updateConfiguration(config,
            getBaseContext().getResources().getDisplayMetrics());
}

Saturday 3 June 2017

How to add a new contact programmatically in android?

We all know how to add a new contact to our android devices without programming right.
OK that’s a normal man’s use.
But what from a programmer’s view.




import java.util.ArrayList;
import android.app.Activity;
import android.content.ContentProviderOperation;
import android.content.ContentValues;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.Contacts.People;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.widget.Toast;
public class AddContactDemo extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        addContact("Hardik","1234567890");
        addContact("Alex","5656215521");
        addContact("Kenedy","4545454545");
        addContact("Peter","9632587410");
        addContact("John","4561237890");
    }
   private void addContact(String name, String phone) {
        ContentValues values = new ContentValues();
        values.put(People.NUMBER, phone);
        values.put(People.TYPE, Phone.TYPE_CUSTOM);
        values.put(People.LABEL, name);
        values.put(People.NAME, name);
        Uri dataUri = getContentResolver().insert(People.CONTENT_URI, values);
        Uri updateUri = Uri.withAppendedPath(dataUri, People.Phones.CONTENT_DIRECTORY);
        values.clear();
        values.put(People.Phones.TYPE, People.TYPE_MOBILE);
        values.put(People.NUMBER, phone);
        updateUri = getContentResolver().insert(updateUri, values);
      }
}