Hello!
I work with this library . Spinner has a list of countries with phone codes. I need to get the country code in EditText with a phone number when choosing a country.
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_phone_book); ccp = (CountryCodePicker) findViewById(R.id.country_cod_picker); phoneNumberEditText = (EditText) findViewById(R.id.phone_number_edit_text); ccp.setOnCountryChangeListener(new CountryCodePicker.OnCountryChangeListener() { @Override public void onCountrySelected() { countryCode = ccp.getSelectedCountryCode(); phoneNumberEditText.setText(countryCode); } }); } Xml code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".PhoneBookActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_margin="@dimen/activity_vertical_margin"> <TextView android:layout_weight="3" android:layout_width="0dp" android:layout_height="wrap_content" android:text="@string/country" android:textColor="@color/black" android:textSize="@dimen/phone_book_text_size"/> <com.hbb20.CountryCodePicker app:textSize="@dimen/phone_book_widgets_text_size" app:showFullName="true" app:defaultNameCode="UA" app:contentColor="@color/black" android:layout_weight="7" android:id="@+id/country_cod_picker" android:layout_width="0dp" android:layout_height="36dp" android:theme="@style/EditTextStyle" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_marginLeft="@dimen/activity_horizontal_margin" android:layout_marginRight="@dimen/activity_horizontal_margin"> <TextView android:layout_weight="3" android:layout_width="0dp" android:layout_height="wrap_content" android:text="@string/phone_number" android:textColor="@color/black" android:textSize="@dimen/phone_book_text_size"/> <EditText android:layout_weight="7" android:id="@+id/phone_number_edit_text" android:layout_width="0dp" android:layout_height="wrap_content" android:inputType="phone" android:textSize="@dimen/phone_book_text_size" android:theme="@style/EditTextStyle" /> </LinearLayout> </LinearLayout>