@Override public void onCountryChanged(String countryCode) { renderCountrySpecificLabels(mCountryAutoCompleteTextView.getSelectedCountryCode()); } });
/** * @param countryCode specify a country code to display in the input. The input will display * the full country display name. */ void setCountrySelected(String countryCode) { if (countryCode == null) { return; } Locale locale = new Locale("", countryCode); updateUIForCountryEntered(locale.getDisplayCountry()); }
public CountryAutoCompleteTextView(Context context) { super(context); initView(); }
private void initView() { inflate(getContext(), R.layout.country_autocomplete_textview, this); mCountryAutocomplete = findViewById(R.id.autocomplete_country_cat); mCountryNameToCode = CountryUtils.getCountryNameToCodeMap(); final ArrayAdapter countryAdapter = new CountryAdapter(getContext(), new ArrayList<>(mCountryNameToCode.keySet())); mCountryAutocomplete.setThreshold(0); mCountryAutocomplete.setAdapter(countryAdapter); mCountryAutocomplete.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { String countryEntered = mCountryAutocomplete.getText().toString(); updateUIForCountryEntered(countryEntered); } }); String defaultCountryEntered = (String) countryAdapter.getItem(0); updateUIForCountryEntered(defaultCountryEntered); mCountryAutocomplete.setText(defaultCountryEntered); mCountryAutocomplete.setOnFocusChangeListener(new OnFocusChangeListener() { @Override public void onFocusChange(View view, boolean focused) { String countryEntered = mCountryAutocomplete.getText().toString(); if (focused) { mCountryAutocomplete.showDropDown(); } else { updateUIForCountryEntered(countryEntered); } } }); }
@Test public void updateUIForCountryEntered_whenValidCountry_UIUpdates() { assertEquals(Locale.US.getCountry(), mCountryAutoCompleteTextView.getSelectedCountryCode()); mCountryAutoCompleteTextView.setCountrySelected(Locale.UK.getCountry()); assertEquals(Locale.UK.getCountry(), mCountryAutoCompleteTextView.getSelectedCountryCode()); }
private void initView() { setOrientation(VERTICAL); inflate(getContext(), R.layout.add_address_widget, this); mCountryAutoCompleteTextView = findViewById(R.id.country_autocomplete_aaw); mAddressLine1TextInputLayout = findViewById(R.id.tl_address_line1_aaw); mAddressLine2TextInputLayout = findViewById(R.id.tl_address_line2_aaw); mCityTextInputLayout = findViewById(R.id.tl_city_aaw); mNameTextInputLayout = findViewById(R.id.tl_name_aaw); mPostalCodeTextInputLayout = findViewById(R.id.tl_postal_code_aaw); mStateTextInputLayout = findViewById(R.id.tl_state_aaw); mAddressEditText = findViewById(R.id.et_address_line_one_aaw); mAddressEditText2 = findViewById(R.id.et_address_line_two_aaw); mCityEditText = findViewById(R.id.et_city_aaw); mNameEditText = findViewById(R.id.et_name_aaw); mPostalCodeEditText = findViewById(R.id.et_postal_code_aaw); mStateEditText = findViewById(R.id.et_state_aaw); mPhoneNumberEditText = findViewById(R.id.et_phone_number_aaw); mPhoneNumberTextInputLayout = findViewById(R.id.tl_phone_number_aaw); mCountryAutoCompleteTextView.setCountryChangeListener(new CountryAutoCompleteTextView .CountryChangeListener() { @Override public void onCountryChanged(String countryCode) { renderCountrySpecificLabels(mCountryAutoCompleteTextView.getSelectedCountryCode()); } }); mPhoneNumberEditText.addTextChangedListener(new PhoneNumberFormattingTextWatcher()); setupErrorHandling(); renderLabels(); renderCountrySpecificLabels(mCountryAutoCompleteTextView.getSelectedCountryCode()); }
@Before public void setup() { MockitoAnnotations.initMocks(this); Locale.setDefault(Locale.US); ActivityController<ShippingInfoTestActivity> activityController = Robolectric.buildActivity(ShippingInfoTestActivity.class).create().start(); mCountryAutoCompleteTextView = activityController.get().findViewById(R.id.country_autocomplete_aaw); mAutoCompleteTextView = mCountryAutoCompleteTextView.findViewById(R.id.autocomplete_country_cat); }
/** * @param shippingInformation shippingInformation to populated into the widget input fields. */ public void populateShippingInfo(@Nullable ShippingInformation shippingInformation) { if (shippingInformation == null) { return; } Address address = shippingInformation.getAddress(); if (address != null) { mCityEditText.setText(address.getCity()); if (address.getCountry() != null && !address.getCountry().isEmpty()) { mCountryAutoCompleteTextView.setCountrySelected(address.getCountry()); } mAddressEditText.setText(address.getLine1()); mAddressEditText2.setText(address.getLine2()); mPostalCodeEditText.setText(address.getPostalCode()); mStateEditText.setText(address.getState()); } mNameEditText.setText(shippingInformation.getName()); mPhoneNumberEditText.setText(shippingInformation.getPhone()); }
@Test public void updateUIForCountryEntered_whenInvalidCountry_revertsToLastCountry() { String previousValidCountryCode = mCountryAutoCompleteTextView.getSelectedCountryCode(); mCountryAutoCompleteTextView.setCountrySelected("FAKE COUNTRY CODE"); assertNull(mAutoCompleteTextView.getError()); assertEquals(mAutoCompleteTextView.getText().toString(), new Locale("", previousValidCountryCode).getDisplayCountry()); mCountryAutoCompleteTextView.setCountrySelected(Locale.UK.getCountry()); assertNotEquals(mAutoCompleteTextView.getText().toString(), new Locale("", previousValidCountryCode).getDisplayCountry()); assertEquals(mAutoCompleteTextView.getText().toString(), Locale.UK.getDisplayCountry()); }
@Override public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { String countryEntered = mCountryAutocomplete.getText().toString(); updateUIForCountryEntered(countryEntered); } });
/** * @param optionalAddressFields address fields that should be optional. */ public void setOptionalFields(@Nullable List<String> optionalAddressFields) { if (optionalAddressFields != null) { mOptionalShippingInfoFields = optionalAddressFields; } else { mOptionalShippingInfoFields = new ArrayList<>(); } renderLabels(); renderCountrySpecificLabels(mCountryAutoCompleteTextView.getSelectedCountryCode()); }
public CountryAutoCompleteTextView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); initView(); }
@Override public void onFocusChange(View view, boolean focused) { String countryEntered = mCountryAutocomplete.getText().toString(); if (focused) { mCountryAutocomplete.showDropDown(); } else { updateUIForCountryEntered(countryEntered); } } });
/** * @param hiddenAddressFields address fields that should be hidden. Hidden fields are * automatically optional. */ public void setHiddenFields(@Nullable List<String> hiddenAddressFields) { if (hiddenAddressFields != null) { mHiddenShippingInfoFields = hiddenAddressFields; } else { mHiddenShippingInfoFields = new ArrayList<>(); } renderLabels(); renderCountrySpecificLabels(mCountryAutoCompleteTextView.getSelectedCountryCode()); }
public CountryAutoCompleteTextView(Context context, AttributeSet attrs) { super(context, attrs); initView(); }
@Test public void updateUIForCountryEntered_whenCountrySelectedNullAndNoLocale_doesNotCrash() { Locale.setDefault(Locale.CHINA); mCountryAutoCompleteTextView.mCountrySelected = null; mCountryAutoCompleteTextView.updateUIForCountryEntered(null); }
String countrySelected = mCountryAutoCompleteTextView.getSelectedCountryCode(); if (mPostalCodeEditText.getText().toString().isEmpty() && (mOptionalShippingInfoFields .contains(POSTAL_CODE_FIELD) || mHiddenShippingInfoFields.contains
@Test public void shippingInfoWidget_whenFieldsOptional_markedAsOptional(){ assertEquals(mPostalCodeTextInputLayout.getHint().toString(), mShippingInfoWidget.getResources().getString(R.string.address_label_zip_code)); assertEquals(mNameTextInputLayout.getHint().toString(), mShippingInfoWidget.getResources().getString(R.string.address_label_name)); List<String> optionalFields = new ArrayList<>(); optionalFields.add(ShippingInfoWidget.POSTAL_CODE_FIELD); mShippingInfoWidget.setOptionalFields(optionalFields); assertEquals(mPostalCodeTextInputLayout.getHint().toString(), mShippingInfoWidget.getResources().getString(R.string.address_label_zip_code_optional)); assertEquals(mNameTextInputLayout.getHint().toString(), mShippingInfoWidget.getResources().getString(R.string.address_label_name)); mCountryAutoCompleteTextView.updateUIForCountryEntered(Locale.CANADA.getDisplayCountry()); assertEquals(mStateTextInputLayout.getHint().toString(), mShippingInfoWidget.getResources().getString(R.string.address_label_province)); optionalFields.add(ShippingInfoWidget.STATE_FIELD); mShippingInfoWidget.setOptionalFields(optionalFields); assertEquals(mStateTextInputLayout.getHint().toString(), mShippingInfoWidget.getResources().getString(R.string.address_label_province_optional)); }
public ShippingInformation getShippingInformation() { if (!validateAllFields()) { return null; } Address address = new Address.Builder().setCity(mCityEditText.getText().toString()) .setCountry(mCountryAutoCompleteTextView.getSelectedCountryCode()).setLine1 (mAddressEditText.getText().toString()).setLine2 (mAddressEditText2.getText().toString()).setPostalCode (mPostalCodeEditText.getText().toString()).setState(mStateEditText .getText().toString()).build(); ShippingInformation shippingInformation = new ShippingInformation(address, mNameEditText .getText().toString(), mPhoneNumberEditText.getText().toString()); return shippingInformation; }
@Test public void shippingInfoWidget_whenErrorOccurs_errorsRenderInternationalized() { mCountryAutoCompleteTextView.updateUIForCountryEntered(Locale.US.getDisplayCountry()); mShippingInfoWidget.validateAllFields(); assertEquals(mStateTextInputLayout.getError(), mShippingInfoWidget.getResources().getString(R.string.address_state_required)); assertEquals(mPostalCodeTextInputLayout.getError(), mShippingInfoWidget.getResources().getString(R.string.address_zip_invalid)); mCountryAutoCompleteTextView.updateUIForCountryEntered(Locale.UK.getDisplayCountry()); mShippingInfoWidget.validateAllFields(); assertEquals(mStateTextInputLayout.getError(), mShippingInfoWidget.getResources().getString(R.string.address_county_required)); assertEquals(mPostalCodeTextInputLayout.getError(), mShippingInfoWidget.getResources().getString(R.string.address_postcode_invalid)); mCountryAutoCompleteTextView.updateUIForCountryEntered( Locale.CANADA.getDisplayCountry()); mShippingInfoWidget.validateAllFields(); assertEquals(mStateTextInputLayout.getError(), mShippingInfoWidget.getResources().getString(R.string.address_province_required)); assertEquals(mPostalCodeTextInputLayout.getError(), mShippingInfoWidget.getResources().getString(R.string.address_postal_code_invalid)); mCountryAutoCompleteTextView.updateUIForCountryEntered(new Locale("", mNoPostalCodeCountry).getDisplayCountry()); mShippingInfoWidget.validateAllFields(); assertEquals(mStateTextInputLayout.getError(), mShippingInfoWidget.getResources().getString(R.string.address_region_generic_required)); }