Tuesday 12 January 2021

Android Get Country Code List

 How to get country code list , so here is the code how to get country code list.




create java class name Country.java

import android.content.Context;
import android.telephony.TelephonyManager;
import android.text.TextUtils;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Locale;


public class DV_Country {
public static final DV_Country[] DV_COUNTRIES = {
new DV_Country("AD", "Andorra", "+376", R.drawable.flag_ad),
new DV_Country("AE", "United Arab Emirates", "+971", R.drawable.flag_ae),
new DV_Country("AF", "Afghanistan", "+93", R.drawable.flag_af),
new DV_Country("AG", "Antigua and Barbuda", "+1", R.drawable.flag_ag),
new DV_Country("AI", "Anguilla", "+1", R.drawable.flag_ai),
new DV_Country("AL", "Albania", "+355", R.drawable.flag_al),
new DV_Country("AM", "Armenia", "+374", R.drawable.flag_am),
new DV_Country("AO", "Angola", "+244", R.drawable.flag_ao),
new DV_Country("AQ", "Antarctica", "+672", R.drawable.flag_aq),
new DV_Country("AR", "Argentina", "+54", R.drawable.flag_ar),
new DV_Country("AS", "AmericanSamoa", "+1", R.drawable.flag_as),
new DV_Country("AT", "Austria", "+43", R.drawable.flag_at),
new DV_Country("AU", "Australia", "+61", R.drawable.flag_au),
new DV_Country("AW", "Aruba", "+297", R.drawable.flag_aw),
new DV_Country("AX", "Åland Islands", "+358", R.drawable.flag_ax),
new DV_Country("AZ", "Azerbaijan", "+994", R.drawable.flag_az),
new DV_Country("BA", "Bosnia and Herzegovina", "+387", R.drawable.flag_ba),
new DV_Country("BB", "Barbados", "+1", R.drawable.flag_bb),
new DV_Country("BD", "Bangladesh", "+880", R.drawable.flag_bd),
new DV_Country("BE", "Belgium", "+32", R.drawable.flag_be),
new DV_Country("BF", "Burkina Faso", "+226", R.drawable.flag_bf),
new DV_Country("BG", "Bulgaria", "+359", R.drawable.flag_bg),
new DV_Country("BH", "Bahrain", "+973", R.drawable.flag_bh),
new DV_Country("BI", "Burundi", "+257", R.drawable.flag_bi),
new DV_Country("BJ", "Benin", "+229", R.drawable.flag_bj),
new DV_Country("BL", "Saint Barthélemy", "+590", R.drawable.flag_bl),
new DV_Country("BM", "Bermuda", "+1", R.drawable.flag_bm),
new DV_Country("BN", "Brunei Darussalam", "+673", R.drawable.flag_bn),
new DV_Country("BO", "Bolivia, Plurinational State of", "+591", R.drawable.flag_bo),
new DV_Country("BQ", "Bonaire", "+599", R.drawable.flag_bq),
new DV_Country("BR", "Brazil", "+55", R.drawable.flag_br),
new DV_Country("BS", "Bahamas", "+1", R.drawable.flag_bs),
new DV_Country("BT", "Bhutan", "+975", R.drawable.flag_bt),
new DV_Country("BV", "Bouvet Island", "+47", R.drawable.flag_bv),
new DV_Country("BW", "Botswana", "+267", R.drawable.flag_bw),
new DV_Country("BY", "Belarus", "+375", R.drawable.flag_by),
new DV_Country("BZ", "Belize", "+501", R.drawable.flag_bz),
new DV_Country("CA", "Canada", "+1", R.drawable.flag_ca),
new DV_Country("CC", "Cocos (Keeling) Islands", "+61", R.drawable.flag_cc),
new DV_Country("CD", "Congo, The Democratic Republic of the", "+243", R.drawable.flag_cd),
new DV_Country("CF", "Central African Republic", "+236", R.drawable.flag_cf),
new DV_Country("CG", "Congo", "+242", R.drawable.flag_cg),
new DV_Country("CH", "Switzerland", "+41", R.drawable.flag_ch),
new DV_Country("CI", "Ivory Coast", "+225", R.drawable.flag_ci),
new DV_Country("CK", "Cook Islands", "+682", R.drawable.flag_ck),
new DV_Country("CL", "Chile", "+56", R.drawable.flag_cl),
new DV_Country("CM", "Cameroon", "+237", R.drawable.flag_cm),
new DV_Country("CN", "China", "+86", R.drawable.flag_cn),
new DV_Country("CO", "Colombia", "+57", R.drawable.flag_co),
new DV_Country("CR", "Costa Rica", "+506", R.drawable.flag_cr),
new DV_Country("CU", "Cuba", "+53", R.drawable.flag_cu),
new DV_Country("CV", "Cape Verde", "+238", R.drawable.flag_cv),
new DV_Country("CW", "Curacao", "+599", R.drawable.flag_cw),
new DV_Country("CX", "Christmas Island", "+61", R.drawable.flag_cx),
new DV_Country("CY", "Cyprus", "+357", R.drawable.flag_cy),
new DV_Country("CZ", "Czech Republic", "+420", R.drawable.flag_cz),
new DV_Country("DE", "Germany", "+49", R.drawable.flag_de),
new DV_Country("DJ", "Djibouti", "+253", R.drawable.flag_dj),
new DV_Country("DK", "Denmark", "+45", R.drawable.flag_dk),
new DV_Country("DM", "Dominica", "+1", R.drawable.flag_dm),
new DV_Country("DO", "Dominican Republic", "+1", R.drawable.flag_do),
new DV_Country("DZ", "Algeria", "+213", R.drawable.flag_dz),
new DV_Country("EC", "Ecuador", "+593", R.drawable.flag_ec),
new DV_Country("EE", "Estonia", "+372", R.drawable.flag_ee),
new DV_Country("EG", "Egypt", "+20", R.drawable.flag_eg),
new DV_Country("EH", "Western Sahara", "+212", R.drawable.flag_eh),
new DV_Country("ER", "Eritrea", "+291", R.drawable.flag_er),
new DV_Country("ES", "Spain", "+34", R.drawable.flag_es),
new DV_Country("ET", "Ethiopia", "+251", R.drawable.flag_et),
new DV_Country("FI", "Finland", "+358", R.drawable.flag_fi),
new DV_Country("FJ", "Fiji", "+679", R.drawable.flag_fj),
new DV_Country("FK", "Falkland Islands (Malvinas)", "+500", R.drawable.flag_fk),
new DV_Country("FM", "Micronesia, Federated States of", "+691", R.drawable.flag_fm),
new DV_Country("FO", "Faroe Islands", "+298", R.drawable.flag_fo),
new DV_Country("FR", "France", "+33", R.drawable.flag_fr),
new DV_Country("GA", "Gabon", "+241", R.drawable.flag_ga),
new DV_Country("GB", "United Kingdom", "+44", R.drawable.flag_gb),
new DV_Country("GD", "Grenada", "+1", R.drawable.flag_gd),
new DV_Country("GE", "Georgia", "+995", R.drawable.flag_ge),
new DV_Country("GF", "French Guiana", "+594", R.drawable.flag_gf),
new DV_Country("GG", "Guernsey", "+44", R.drawable.flag_gg),
new DV_Country("GH", "Ghana", "+233", R.drawable.flag_gh),
new DV_Country("GI", "Gibraltar", "+350", R.drawable.flag_gi),
new DV_Country("GL", "Greenland", "+299", R.drawable.flag_gl),
new DV_Country("GM", "Gambia", "+220", R.drawable.flag_gm),
new DV_Country("GN", "Guinea", "+224", R.drawable.flag_gn),
new DV_Country("GP", "Guadeloupe", "+590", R.drawable.flag_gp),
new DV_Country("GQ", "Equatorial Guinea", "+240", R.drawable.flag_gq),
new DV_Country("GR", "Greece", "+30", R.drawable.flag_gr),
new DV_Country("GS", "South Georgia and the South Sandwich Islands", "+500", R.drawable.flag_gs),
new DV_Country("GT", "Guatemala", "+502", R.drawable.flag_gt),
new DV_Country("GU", "Guam", "+1", R.drawable.flag_gu),
new DV_Country("GW", "Guinea-Bissau", "+245", R.drawable.flag_gw),
new DV_Country("GY", "Guyana", "+595", R.drawable.flag_gy),
new DV_Country("HK", "Hong Kong", "+852", R.drawable.flag_hk),
new DV_Country("HM", "Heard Island and McDonald Islands", "", R.drawable.flag_hm),
new DV_Country("HN", "Honduras", "+504", R.drawable.flag_hn),
new DV_Country("HR", "Croatia", "+385", R.drawable.flag_hr),
new DV_Country("HT", "Haiti", "+509", R.drawable.flag_ht),
new DV_Country("HU", "Hungary", "+36", R.drawable.flag_hu),
new DV_Country("ID", "Indonesia", "+62", R.drawable.flag_id),
new DV_Country("IE", "Ireland", "+353", R.drawable.flag_ie),
new DV_Country("IL", "Israel", "+972", R.drawable.flag_il),
new DV_Country("IM", "Isle of Man", "+44", R.drawable.flag_im),
new DV_Country("IN", "India", "+91", R.drawable.flag_in),
new DV_Country("IO", "British Indian Ocean Territory", "+246", R.drawable.flag_io),
new DV_Country("IQ", "Iraq", "+964", R.drawable.flag_iq),
new DV_Country("IR", "Iran, Islamic Republic of", "+98", R.drawable.flag_ir),
new DV_Country("IS", "Iceland", "+354", R.drawable.flag_is),
new DV_Country("IT", "Italy", "+39", R.drawable.flag_it),
new DV_Country("JE", "Jersey", "+44", R.drawable.flag_je),
new DV_Country("JM", "Jamaica", "+1", R.drawable.flag_jm),
new DV_Country("JO", "Jordan", "+962", R.drawable.flag_jo),
new DV_Country("JP", "Japan", "+81", R.drawable.flag_jp),
new DV_Country("KE", "Kenya", "+254", R.drawable.flag_ke),
new DV_Country("KG", "Kyrgyzstan", "+996", R.drawable.flag_kg),
new DV_Country("KH", "Cambodia", "+855", R.drawable.flag_kh),
new DV_Country("KI", "Kiribati", "+686", R.drawable.flag_ki),
new DV_Country("KM", "Comoros", "+269", R.drawable.flag_km),
new DV_Country("KN", "Saint Kitts and Nevis", "+1", R.drawable.flag_kn),
new DV_Country("KP", "North Korea", "+850", R.drawable.flag_kp),
new DV_Country("KR", "South Korea", "+82", R.drawable.flag_kr),
new DV_Country("KW", "Kuwait", "+965", R.drawable.flag_kw),
new DV_Country("KY", "Cayman Islands", "+345", R.drawable.flag_ky),
new DV_Country("KZ", "Kazakhstan", "+7", R.drawable.flag_kz),
new DV_Country("LA", "Lao People's Democratic Republic", "+856", R.drawable.flag_la),
new DV_Country("LB", "Lebanon", "+961", R.drawable.flag_lb),
new DV_Country("LC", "Saint Lucia", "+1", R.drawable.flag_lc),
new DV_Country("LI", "Liechtenstein", "+423", R.drawable.flag_li),
new DV_Country("LK", "Sri Lanka", "+94", R.drawable.flag_lk),
new DV_Country("LR", "Liberia", "+231", R.drawable.flag_lr),
new DV_Country("LS", "Lesotho", "+266", R.drawable.flag_ls),
new DV_Country("LT", "Lithuania", "+370", R.drawable.flag_lt),
new DV_Country("LU", "Luxembourg", "+352", R.drawable.flag_lu),
new DV_Country("LV", "Latvia", "+371", R.drawable.flag_lv),
new DV_Country("LY", "Libyan Arab Jamahiriya", "+218", R.drawable.flag_ly),
new DV_Country("MA", "Morocco", "+212", R.drawable.flag_ma),
new DV_Country("MC", "Monaco", "+377", R.drawable.flag_mc),
new DV_Country("MD", "Moldova, Republic of", "+373", R.drawable.flag_md),
new DV_Country("ME", "Montenegro", "+382", R.drawable.flag_me),
new DV_Country("MF", "Saint Martin", "+590", R.drawable.flag_mf),
new DV_Country("MG", "Madagascar", "+261", R.drawable.flag_mg),
new DV_Country("MH", "Marshall Islands", "+692", R.drawable.flag_mh),
new DV_Country("MK", "Macedonia, The Former Yugoslav Republic of", "+389", R.drawable.flag_mk),
new DV_Country("ML", "Mali", "+223", R.drawable.flag_ml),
new DV_Country("MM", "Myanmar", "+95", R.drawable.flag_mm),
new DV_Country("MN", "Mongolia", "+976", R.drawable.flag_mn),
new DV_Country("MO", "Macao", "+853", R.drawable.flag_mo),
new DV_Country("MP", "Northern Mariana Islands", "+1", R.drawable.flag_mp),
new DV_Country("MQ", "Martinique", "+596", R.drawable.flag_mq),
new DV_Country("MR", "Mauritania", "+222", R.drawable.flag_mr),
new DV_Country("MS", "Montserrat", "+1", R.drawable.flag_ms),
new DV_Country("MT", "Malta", "+356", R.drawable.flag_mt),
new DV_Country("MU", "Mauritius", "+230", R.drawable.flag_mu),
new DV_Country("MV", "Maldives", "+960", R.drawable.flag_mv),
new DV_Country("MW", "Malawi", "+265", R.drawable.flag_mw),
new DV_Country("MX", "Mexico", "+52", R.drawable.flag_mx),
new DV_Country("MY", "Malaysia", "+60", R.drawable.flag_my),
new DV_Country("MZ", "Mozambique", "+258", R.drawable.flag_mz),
new DV_Country("NA", "Namibia", "+264", R.drawable.flag_na),
new DV_Country("NC", "New Caledonia", "+687", R.drawable.flag_nc),
new DV_Country("NE", "Niger", "+227", R.drawable.flag_ne),
new DV_Country("NF", "Norfolk Island", "+672", R.drawable.flag_nf),
new DV_Country("NG", "Nigeria", "+234", R.drawable.flag_ng),
new DV_Country("NI", "Nicaragua", "+505", R.drawable.flag_ni),
new DV_Country("NL", "Netherlands", "+31", R.drawable.flag_nl),
new DV_Country("NO", "Norway", "+47", R.drawable.flag_no),
new DV_Country("NP", "Nepal", "+977", R.drawable.flag_np),
new DV_Country("NR", "Nauru", "+674", R.drawable.flag_nr),
new DV_Country("NU", "Niue", "+683", R.drawable.flag_nu),
new DV_Country("NZ", "New Zealand", "+64", R.drawable.flag_nz),
new DV_Country("OM", "Oman", "+968", R.drawable.flag_om),
new DV_Country("PA", "Panama", "+507", R.drawable.flag_pa),
new DV_Country("PE", "Peru", "+51", R.drawable.flag_pe),
new DV_Country("PF", "French Polynesia", "+689", R.drawable.flag_pf),
new DV_Country("PG", "Papua New Guinea", "+675", R.drawable.flag_pg),
new DV_Country("PH", "Philippines", "+63", R.drawable.flag_ph),
new DV_Country("PK", "Pakistan", "+92", R.drawable.flag_pk),
new DV_Country("PL", "Poland", "+48", R.drawable.flag_pl),
new DV_Country("PM", "Saint Pierre and Miquelon", "+508", R.drawable.flag_pm),
new DV_Country("PN", "Pitcairn", "+872", R.drawable.flag_pn),
new DV_Country("PR", "Puerto Rico", "+1", R.drawable.flag_pr),
new DV_Country("PS", "Palestinian Territory, Occupied", "+970", R.drawable.flag_ps),
new DV_Country("PT", "Portugal", "+351", R.drawable.flag_pt),
new DV_Country("PW", "Palau", "+680", R.drawable.flag_pw),
new DV_Country("PY", "Paraguay", "+595", R.drawable.flag_py),
new DV_Country("QA", "Qatar", "+974", R.drawable.flag_qa),
new DV_Country("RE", "Réunion", "+262", R.drawable.flag_re),
new DV_Country("RO", "Romania", "+40", R.drawable.flag_ro),
new DV_Country("RS", "Serbia", "+381", R.drawable.flag_rs),
new DV_Country("RU", "Russia", "+7", R.drawable.flag_ru),
new DV_Country("RW", "Rwanda", "+250", R.drawable.flag_rw),
new DV_Country("SA", "Saudi Arabia", "+966", R.drawable.flag_sa),
new DV_Country("SB", "Solomon Islands", "+677", R.drawable.flag_sb),
new DV_Country("SC", "Seychelles", "+248", R.drawable.flag_sc),
new DV_Country("SD", "Sudan", "+249", R.drawable.flag_sd),
new DV_Country("SE", "Sweden", "+46", R.drawable.flag_se),
new DV_Country("SG", "Singapore", "+65", R.drawable.flag_sg),
new DV_Country("SH", "Saint Helena, Ascension and Tristan Da Cunha", "+290", R.drawable.flag_sh),
new DV_Country("SI", "Slovenia", "+386", R.drawable.flag_si),
new DV_Country("SJ", "Svalbard and Jan Mayen", "+47", R.drawable.flag_sj),
new DV_Country("SK", "Slovakia", "+421", R.drawable.flag_sk),
new DV_Country("SL", "Sierra Leone", "+232", R.drawable.flag_sl),
new DV_Country("SM", "San Marino", "+378", R.drawable.flag_sm),
new DV_Country("SN", "Senegal", "+221", R.drawable.flag_sn),
new DV_Country("SO", "Somalia", "+252", R.drawable.flag_so),
new DV_Country("SR", "Suriname", "+597", R.drawable.flag_sr),
new DV_Country("SS", "South Sudan", "+211", R.drawable.flag_ss),
new DV_Country("ST", "Sao Tome and Principe", "+239", R.drawable.flag_st),
new DV_Country("SV", "El Salvador", "+503", R.drawable.flag_sv),
new DV_Country("SX", "Sint Maarten", "+1", R.drawable.flag_sx),
new DV_Country("SY", "Syrian Arab Republic", "+963", R.drawable.flag_sy),
new DV_Country("SZ", "Swaziland", "+268", R.drawable.flag_sz),
new DV_Country("TC", "Turks and Caicos Islands", "+1", R.drawable.flag_tc),
new DV_Country("TD", "Chad", "+235", R.drawable.flag_td),
new DV_Country("TF", "French Southern Territories", "+262", R.drawable.flag_tf),
new DV_Country("TG", "Togo", "+228", R.drawable.flag_tg),
new DV_Country("TH", "Thailand", "+66", R.drawable.flag_th),
new DV_Country("TJ", "Tajikistan", "+992", R.drawable.flag_tj),
new DV_Country("TK", "Tokelau", "+690", R.drawable.flag_tk),
new DV_Country("TL", "East Timor", "+670", R.drawable.flag_tl),
new DV_Country("TM", "Turkmenistan", "+993", R.drawable.flag_tm),
new DV_Country("TN", "Tunisia", "+216", R.drawable.flag_tn),
new DV_Country("TO", "Tonga", "+676", R.drawable.flag_to),
new DV_Country("TR", "Turkey", "+90", R.drawable.flag_tr),
new DV_Country("TT", "Trinidad and Tobago", "+1", R.drawable.flag_tt),
new DV_Country("TV", "Tuvalu", "+688", R.drawable.flag_tv),
new DV_Country("TW", "Taiwan", "+886", R.drawable.flag_tw),
new DV_Country("TZ", "Tanzania, United Republic of", "+255", R.drawable.flag_tz),
new DV_Country("UA", "Ukraine", "+380", R.drawable.flag_ua),
new DV_Country("UG", "Uganda", "+256", R.drawable.flag_ug),
new DV_Country("UM", "U.S. Minor Outlying Islands", "", R.drawable.flag_um),
new DV_Country("US", "United States", "+1", R.drawable.flag_us),
new DV_Country("UY", "Uruguay", "+598", R.drawable.flag_uy),
new DV_Country("UZ", "Uzbekistan", "+998", R.drawable.flag_uz),
new DV_Country("VA", "Holy See (Vatican City State)", "+379", R.drawable.flag_va),
new DV_Country("VC", "Saint Vincent and the Grenadines", "+1", R.drawable.flag_vc),
new DV_Country("VE", "Venezuela, Bolivarian Republic of", "+58", R.drawable.flag_ve),
new DV_Country("VG", "Virgin Islands, British", "+1", R.drawable.flag_vg),
new DV_Country("VI", "Virgin Islands, U.S.", "+1", R.drawable.flag_vi),
new DV_Country("VN", "Viet Nam", "+84", R.drawable.flag_vn),
new DV_Country("VU", "Vanuatu", "+678", R.drawable.flag_vu),
new DV_Country("WF", "Wallis and Futuna", "+681", R.drawable.flag_wf),
new DV_Country("WS", "Samoa", "+685", R.drawable.flag_ws),
new DV_Country("XK", "Kosovo", "+383", R.drawable.flag_xk),
new DV_Country("YE", "Yemen", "+967", R.drawable.flag_ye),
new DV_Country("YT", "Mayotte", "+262", R.drawable.flag_yt),
new DV_Country("ZA", "South Africa", "+27", R.drawable.flag_za),
new DV_Country("ZM", "Zambia", "+260", R.drawable.flag_zm),
new DV_Country("ZW", "Zimbabwe", "+263", R.drawable.flag_zw),
};
private static List<DV_Country> dv_allCountriesList;
private String code;
private String name;
private String dialCode;
private int flag = -1;

public DV_Country(String code, String name, String dialCode, int flag) {
this.code = code;
this.name = name;
this.dialCode = dialCode;
this.flag = flag;
}


public DV_Country() {
}

public static List<DV_Country> getAllCountries() {
if (dv_allCountriesList == null) {
dv_allCountriesList = Arrays.asList(DV_COUNTRIES);
}
return dv_allCountriesList;
}

public static DV_Country getCountryByISO(String countryIsoCode) {
countryIsoCode = countryIsoCode.toUpperCase();

DV_Country c = new DV_Country();
c.setCode(countryIsoCode);

int i = Arrays.binarySearch(DV_COUNTRIES, c, new ISOCodeComparator());

if (i < 0) {
return null;
} else {
return DV_COUNTRIES[i];
}
}
public static DV_Country getCountryFromSIM(Context context) {
TelephonyManager telephonyManager =
(TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
if (telephonyManager.getSimState() != TelephonyManager.SIM_STATE_ABSENT) {
return DV_Country.getCountryByISO(telephonyManager.getSimCountryIso());
}
return null;
}

public String getDialCode() {
return dialCode;
}

public String getCode() {
return code;
}

public void setCode(String code) {
this.code = code;
if (TextUtils.isEmpty(name)) {
name = new Locale("", code).getDisplayName();
}
}

public String getName() {
return name;
}

public int getFlag() {
return flag;
}

public void loadFlagByCode(Context context) {
if (this.flag != -1)
return;

try {
this.flag = context.getResources()
.getIdentifier("flag_" + this.code.toLowerCase(Locale.ENGLISH), "drawable",
context.getPackageName());
} catch (Exception e) {
e.printStackTrace();
this.flag = -1;
}
}

public static class ISOCodeComparator implements Comparator<DV_Country> {
@Override
public int compare(DV_Country country, DV_Country t1) {
return country.code.compareTo(t1.code);
}
}

}

above code is the create list of all countries with flag image , now create interface name 

CountryPickerListener.java
public interface DV_CountryPickerListener {
public void DV_onSelectCountry(String name, String code, String dialCode, int flagDrawableResID);
}

now create dialog to display country code list ,create class name CountryPicker.java

import android.annotation.SuppressLint;
import android.app.DialogFragment;
import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;


import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import androidx.fragment.app.FragmentManager;
public class DV_CountryPicker extends DialogFragment {

private EditText searchEditText;
private ListView countryListView;
private com.japp.dv_countrylist.DV_CountryListAdapter adapter;
private List<com.japp.dv_countrylist.DV_Country> DV_countriesList = new ArrayList<>();
private List<com.japp.dv_countrylist.DV_Country> DV_selectedCountriesList = new ArrayList<>();
private com.japp.dv_countrylist.DV_CountryPickerListener DV_listener;
private Context context;

public DV_CountryPicker() {
setCountriesList(com.japp.dv_countrylist.DV_Country.getAllCountries());
}


public static DV_CountryPicker newInstance(String dialogTitle) {
DV_CountryPicker picker = new DV_CountryPicker();
Bundle bundle = new Bundle();
bundle.putString("dialogTitle", dialogTitle);
picker.setArguments(bundle);
return picker;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.country_picker, null);
Bundle args = getArguments();
if (args != null) {
String dialogTitle = args.getString("dialogTitle");
getDialog().setTitle(dialogTitle);


int width = getResources().getDimensionPixelSize(R.dimen.cp_dialog_width);
int height = getResources().getDimensionPixelSize(R.dimen.cp_dialog_height);
getDialog().getWindow().setLayout(width, height);
}
searchEditText = (EditText) view.findViewById(R.id.country_code_picker_search);
countryListView = (ListView) view.findViewById(R.id.country_code_picker_listview);

DV_selectedCountriesList = new ArrayList<>(DV_countriesList.size());
DV_selectedCountriesList.addAll(DV_countriesList);

adapter = new com.japp.dv_countrylist.DV_CountryListAdapter(getActivity(), DV_selectedCountriesList);
countryListView.setAdapter(adapter);

countryListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (DV_listener != null) {
com.japp.dv_countrylist.DV_Country country = DV_selectedCountriesList.get(position);
DV_listener.DV_onSelectCountry(country.getName(), country.getCode(), country.getDialCode(),
country.getFlag());
}
}
});

searchEditText.addTextChangedListener(new TextWatcher() {

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

@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}

@Override
public void afterTextChanged(Editable s) {
search(s.toString());
}
});

return view;
}

public void setListener(com.japp.dv_countrylist.DV_CountryPickerListener listener) {
this.DV_listener = listener;
}

@SuppressLint("DefaultLocale")
private void search(String text) {
DV_selectedCountriesList.clear();
for (com.japp.dv_countrylist.DV_Country country : DV_countriesList) {
if (country.getName().toLowerCase(Locale.ENGLISH).contains(text.toLowerCase())) {
DV_selectedCountriesList.add(country);
}
}
adapter.notifyDataSetChanged();
}

public void setCountriesList(List<com.japp.dv_countrylist.DV_Country> newCountries) {
this.DV_countriesList.clear();
this.DV_countriesList.addAll(newCountries);
}

}

now create adapter for display list ,create adapter name CountryListAdapter.java

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;


import java.util.List;
public class DV_CountryListAdapter extends BaseAdapter {

List<com.japp.dv_countrylist.DV_Country> dv_countries;
LayoutInflater inflater;
private Context mContext;

public DV_CountryListAdapter(Context context, List<com.japp.dv_countrylist.DV_Country> countries) {
super();
this.mContext = context;
this.dv_countries = countries;
inflater = LayoutInflater.from(context);
}

@Override
public int getCount() {
return dv_countries.size();
}

@Override
public Object getItem(int arg0) {
return null;
}

@Override
public long getItemId(int arg0) {
return 0;
}

@Override
public View getView(int position, View view, ViewGroup parent) {
com.japp.dv_countrylist.DV_Country country = dv_countries.get(position);

if (view == null)
view = inflater.inflate(R.layout.raw, null);

DV_Cell cell = DV_Cell.from(view);
cell.textView.setText(country.getName());

country.loadFlagByCode(mContext);
if (country.getFlag() != -1)
cell.imageView.setImageResource(country.getFlag());
return view;
}

static class DV_Cell {
public TextView textView;
public ImageView imageView;

static DV_Cell from(View view) {
if (view == null)
return null;

if (view.getTag() == null) {
DV_Cell cell = new DV_Cell();
cell.textView = (TextView) view.findViewById(R.id.row_title);
cell.imageView = (ImageView) view.findViewById(R.id.row_icon);
view.setTag(cell);
return cell;
} else {
return (DV_Cell) view.getTag();
}
}
}
}

now here is the main activity
import android.content.Context;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

Context mContext;
    ImageView dv_countryImage;
EditText ed_mobile_number;
TextView dv_countryNumber;
String country_code = "+27";

private com.japp.dv_countrylist.DV_CountryPicker mCountryPicker;

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

mContext = MainActivity.this;

dv_countryImage = (ImageView)findViewById(R.id.dv_countryImage);
ed_mobile_number = (EditText)findViewById(R.id.ed_mobile_number);
dv_countryNumber = (TextView)findViewById(R.id.dv_countryNumber);

mCountryPicker = com.japp.dv_countrylist.DV_CountryPicker.newInstance("Select Country");
List<com.japp.dv_countrylist.DV_Country> countryList = com.japp.dv_countrylist.DV_Country.getAllCountries();
Collections.sort(countryList, new Comparator<com.japp.dv_countrylist.DV_Country>() {
@Override
public int compare(com.japp.dv_countrylist.DV_Country s1, com.japp.dv_countrylist.DV_Country s2) {
return s1.getName().compareToIgnoreCase(s2.getName());
}
});
mCountryPicker.setCountriesList(countryList);

getUserCountryInfo();

mCountryPicker.setListener(new com.japp.dv_countrylist.DV_CountryPickerListener() {
@Override
public void DV_onSelectCountry(String name, String code, String dialCode,
int flagDrawableResID) {
dv_countryNumber.setText(dialCode);
country_code = dialCode;
dv_countryImage.setImageResource(flagDrawableResID);
mCountryPicker.dismiss();
}
});
dv_countryImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mCountryPicker.show(getFragmentManager(), "COUNTRY_PICKER");
}
});

dv_countryNumber.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mCountryPicker.show(getFragmentManager(), "COUNTRY_PICKER");
}
});

}

private void getUserCountryInfo() {
com.japp.dv_countrylist.DV_Country country = com.japp.dv_countrylist.DV_Country.getCountryFromSIM(MainActivity.this);
if (country != null) {
dv_countryImage.setImageResource(country.getFlag());
dv_countryNumber.setText(country.getDialCode());
country_code = country.getDialCode();
} else {
com.japp.dv_countrylist.DV_Country us = new com.japp.dv_countrylist.DV_Country("IN", "INDIA", "+91", R.drawable.flag_in);
dv_countryImage.setImageResource(us.getFlag());
dv_countryNumber.setText(us.getDialCode());
country_code = us.getDialCode();
}
}
}











So Here is the Source code Source Code

No comments:

Post a Comment

Comments

Find Hours Diffrence in Kotlin

  In Kotlin, determining the difference in hours between two timestamps is a common task, especially in scenarios involving time-based calcu...