2

This is my code that receive country code by input(in onCreate() method):

        countryCodeEdt = (EditText)findViewById(R.id.register_country_code_tv);
        countryCodeEdt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String newCode = s.toString();

                System.out.println("this is new code value "+newCode);
                if (newCode == null || newCode.length() < 1){
                    pickupCountryTv.setText("Choose a country");
                }
                else {
                    if (countryMaps.containsKey(newCode)){
                        countryCode = newCode;
                        countryName = countryMaps.get(newCode);
                        pickupCountryTv.setText(countryName);
                    }
                    else{
                        countryCode = "";
                        countryName = "";
                        pickupCountryTv.setText("Wrong country code");
                    }
                }
            }
        });

Now I want to pre-select the country code automatically without input by people. I found the answer using TelephonyManager. But I'm confusing of how to set up for my countryCodeEdt variable to get the value from this method:

public static String getUserCountry(Context context) {
    try {
        final TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        final String simCountry = tm.getSimCountryIso();
        if (simCountry != null && simCountry.length() == 2) { // SIM country code is available
            return simCountry.toLowerCase(Locale.US);
        }
        else if (tm.getPhoneType() != TelephonyManager.PHONE_TYPE_CDMA) { // device is not 3G (would be unreliable)
            String networkCountry = tm.getNetworkCountryIso();
            if (networkCountry != null && networkCountry.length() == 2) { // network country code is available
                return networkCountry.toLowerCase(Locale.US);
            }
        }
    }
    catch (Exception e) { }
    return null;
}

Can someone give me some suggestion? Thanks in advance.

Community
  • 1
  • 1
DinhNgocHien
  • 707
  • 3
  • 17
  • 34

1 Answers1

1

Simply place the getUserCountry method at your MainActivity, define a String variable, and call -

String countryCode = getUserCountry(getApplicationContext());

Now you can use it for your TextView -

if (countryCode != null) {
   countryCodeEdt.setText(countryName);
} else {
//some error?
}

Pay attention that getUserCountry returns the country's code, not country's name i.e. GB and not Great Britain.
You should also handle the case that the SIM card is not presented inside the device.

TDG
  • 5,909
  • 3
  • 30
  • 51
  • Do you mean define `countryCode` inside `afterTextChanged(Editable s)` method? – DinhNgocHien Aug 30 '15 at 17:00
  • No. Define it as a seperate method, like `onCreate`, 'onResume` and so on. – TDG Aug 30 '15 at 17:31
  • ok I got it, however, `countryCode` always return `null`. I didn't know where the error come from, cause by `getUserCountry` return `null` or due to my device(i used genymotion as a tested device) – DinhNgocHien Aug 30 '15 at 17:38
  • I've tested it with an actuall device and got the correct code. – TDG Aug 30 '15 at 17:39
  • Cool, so could you share your simple test on github? I have no actual device right now to test, to make clear what exactly where the error come from. – DinhNgocHien Aug 30 '15 at 17:49
  • I've used exectly your code to get the country code and printed it to the LogCat... – TDG Aug 30 '15 at 18:00