Monday, September 9, 2013

Get details of Locale

Last exercise list available Locale of the system. It's modified to display details of the clicked Locale.

Get details of Locale


package com.example.androidlocale;

import java.util.Locale;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
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());

  final 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);
  
  listViewAvailableLocale.setOnItemClickListener(
    new OnItemClickListener(){

   @Override
   public void onItemClick(AdapterView<?> parent, 
     View view, int position, long id) {
    Locale item = availableLocales[position];

    Toast.makeText(getApplicationContext(), 
      "Language: " + item.getLanguage() + "\n"
      + "Country: " + item.getCountry() + "\n"
      + "DisplayName: " + item.getDisplayName() + "\n"
      + "DisplayLanguage: " + item.getDisplayLanguage() + "\n"
      + "DisplayCountry: " + item.getDisplayCountry(), 
      Toast.LENGTH_LONG).show();
   }});

 }

}


Keep using the layout in last exercise.

Next: Create SimpleDateFormat for specified Locale

No comments:

Post a Comment