Saturday, September 7, 2013

Get default Locale and available Locales

Example to get default Locale and available Locales:

Get default Locale and available Locales


package com.example.androidlocale;

import java.util.Locale;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.app.Activity;

public class MainActivity extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  
  TextView textViewDefaultLocale = (TextView)findViewById(R.id.defaultlocale);
  TextView textViewNoOfLocale = (TextView)findViewById(R.id.numberoflocale);
  ListView listViewAvailableLocale = (ListView)findViewById(R.id.listviewlocale);
  
  Locale defaultLocale = Locale.getDefault();
  textViewDefaultLocale.setText("Default Locale: " + defaultLocale.toString());

  Locale[] availableLocales = Locale.getAvailableLocales();
  textViewNoOfLocale.setText("Number of available Locale: " + availableLocales.length);
  String[] availableLocalesString = new String[availableLocales.length];
  
  for (int i=0; i<availableLocales.length; i++){
   availableLocalesString[i] = availableLocales[i].toString();
  }
  
  ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 
            android.R.layout.simple_list_item_1, availableLocalesString);
  listViewAvailableLocale.setAdapter(adapter);

 }

}


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:autoLink="web"
        android:text="http://android-er.blogspot.com/"
        android:textStyle="bold" />
    <TextView 
        android:id="@+id/defaultlocale"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    <TextView 
        android:id="@+id/numberoflocale"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    <ListView
        android:id="@+id/listviewlocale"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</LinearLayout>

next: Get details of Locale

No comments: