0

In my application i want get user City name from Latitude and Longitude.
For this i write below codes, but show me city name fro English language such as "Tehran"

But i want show this city name for Farsi , such as "تهران"

How can i it for change language?

My codes :

public class MainActivity extends Activity {

    private SimpleLocation mLocation;
    private Geocoder geocoder;
    private List<Address> addresses;
    private String cityName;

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // construct a new instance
        mLocation = new SimpleLocation(this);

        // reduce the precision to 5,000m for privacy reasons
        mLocation.setBlurRadius(5000);

        geocoder = new Geocoder(this, Locale.getDefault());

        // if we can't access the location yet
        if (!mLocation.hasLocationEnabled()) {
            // ask the user to enable location access
            SimpleLocation.openSettings(this);
        }

        final double latitude = mLocation.getLatitude();
        final double longitude = mLocation.getLongitude();

        try {
            addresses = geocoder.getFromLocation(latitude, longitude, 1);
            cityName = addresses.get(0).getAddressLine(0);
        } catch (IOException e) {
            e.printStackTrace();
        }

        findViewById(R.id.textView).setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                Toast.makeText(MainActivity.this, "" + cityName, Toast.LENGTH_SHORT).show();
            }

        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        // make the device update its location
        mLocation.beginUpdates();
    }

    @Override
    protected void onPause() {
        // stop location updates (saves battery)
        mLocation.endUpdates();

        super.onPause();
    }
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Dr.Kook
  • 3
  • 1

4 Answers4

2

Just pass the locale in the constructor below:

Geocoder geo = new Geocoder(MainActivity.this, new Locale("Your desired locale code"));
kishna.147
  • 167
  • 8
  • @Dr.Kook In general, you need to let the user choose the locale. So, either use default locale or let him choose it in some configuration form. Preferably the first. – Jean-Baptiste Yunès Mar 13 '18 at 08:15
  • @Dr.Kook check this link https://stackoverflow.com/questions/7973023/what-is-the-list-of-supported-languages-locales-on-android – kishna.147 Mar 13 '18 at 08:21
1

Change TextView with locale

Locale loc = new Locale("ar"); // change locale as per your requirement
textView.setTextLocale(loc);
textView.setText("Farsi");
Nik
  • 1,991
  • 2
  • 13
  • 30
0

User localization for changing the language using string.xml. pl follow the https://developer.android.com/guide/topics/resources/localization.html to know more about localization in android application.

RAAAAM
  • 3,378
  • 19
  • 59
  • 108
0

Instead of passing Locale.getDefault(), pass new Locale("fa_IR").

And use addresses.get(0).getLocality() to get the name of the city.

See list of supported locales.

https://stackoverflow.com/a/7989085/5722608

Faraz
  • 2,144
  • 1
  • 18
  • 28
  • but in my code show me state, city, street name and country name. i want just show city name. how can i it? can you help me? – Dr.Kook Mar 13 '18 at 08:18
  • @Dr.Kook Check my updated answer. To get a clear picture, place a brea kpoint and check what you are receiving in the address object. – Faraz Mar 13 '18 at 08:25