private void resetFormFields(int countryCodeIndex, boolean autoFocusFirstField) {
    // Save field text so we can restore it after updating the fields for the current country,
    // and reset mAddressFields.
    String[] fieldText = new String[mAddressFields.length];
    for (int i = 0; i < mAddressFields.length; i++) {
      if (mAddressFields[i] != null) {
        fieldText[i] = mAddressFields[i].getEditText().getText().toString();
        mAddressFields[i] = null;
      }
    }

    // Remove all address form fields.
    mWidgetRoot.removeAllViews();

    // Get address fields for the selected country.
    List<Pair<Integer, String>> fields =
        mAutofillProfileBridge.getAddressUiComponents(
            mCountryCodes.get(countryCodeIndex), mLanguageCodeString);
    if (!mUseSavedProfileLanguage) {
      mLanguageCodeString = mAutofillProfileBridge.getCurrentBestLanguageCode();
    }

    // Create form fields and focus the first field if autoFocusFirstField is true.
    boolean firstField = true;
    for (Pair<Integer, String> field : fields) {
      int fieldId = field.first;
      String fieldLabel = field.second;
      FloatLabelLayout fieldFloatLabel =
          (FloatLabelLayout)
              mInflater.inflate(R.layout.preference_address_float_label_layout, mWidgetRoot, false);
      fieldFloatLabel.setHint(fieldLabel);

      EditText fieldEditText = (EditText) fieldFloatLabel.findViewById(R.id.address_edit_text);
      fieldEditText.setHint(fieldLabel);
      fieldEditText.setContentDescription(fieldLabel);
      fieldEditText.addTextChangedListener(this);
      if (fieldId == AddressField.STREET_ADDRESS) {
        fieldEditText.setSingleLine(false);
      }

      mAddressFields[fieldId] = fieldFloatLabel;
      mWidgetRoot.addView(fieldFloatLabel);

      if (firstField && autoFocusFirstField) {
        fieldFloatLabel.focusWithoutAnimation();
        firstField = false;
      }
    }

    // Add back saved field text.
    for (int i = 0; i < mAddressFields.length; i++) {
      if (mAddressFields[i] != null && fieldText[i] != null && !TextUtils.isEmpty(fieldText[i])) {
        mAddressFields[i].setText(fieldText[i]);
      }
    }
  }
  private void createAndPopulateEditFields() {
    AutofillProfile profile = PersonalDataManager.getInstance().getProfile(mGUID);

    if (profile != null) {
      if (!TextUtils.isEmpty(profile.getPhoneNumber())) {
        mPhoneLabel.setText(profile.getPhoneNumber());
      }

      if (!TextUtils.isEmpty(profile.getEmailAddress())) {
        mEmailLabel.setText(profile.getEmailAddress());
      }

      mLanguageCodeString = profile.getLanguageCode();
      mUseSavedProfileLanguage = true;

      mCurrentCountryPos = mCountryCodes.indexOf(profile.getCountryCode());
      if (mCurrentCountryPos == -1) {
        // Use the default country code if profile code is invalid.
        mCurrentCountryPos = mCountryCodes.indexOf(AutofillProfileBridge.getDefaultCountryCode());
        if (mCurrentCountryPos == -1) {
          // Use the first item in country spinner if the default country code is
          // invalid.
          mCurrentCountryPos = 0;
        }
      }

      resetFormFields(mCurrentCountryPos, false);

      setFieldText(AddressField.ADMIN_AREA, profile.getRegion());
      setFieldText(AddressField.LOCALITY, profile.getLocality());
      setFieldText(AddressField.DEPENDENT_LOCALITY, profile.getDependentLocality());
      setFieldText(AddressField.SORTING_CODE, profile.getSortingCode());
      setFieldText(AddressField.POSTAL_CODE, profile.getPostalCode());
      setFieldText(AddressField.STREET_ADDRESS, profile.getStreetAddress());
      setFieldText(AddressField.ORGANIZATION, profile.getCompanyName());
      setFieldText(AddressField.RECIPIENT, profile.getFullName());
    } else {
      mCurrentCountryPos = mCountryCodes.indexOf(AutofillProfileBridge.getDefaultCountryCode());
      if (mCurrentCountryPos == -1) {
        // Use the first item in country spinner if the default country code is
        // invalid.
        mCurrentCountryPos = 0;
      }
      resetFormFields(mCurrentCountryPos, true);
    }

    mCountriesSpinner.setSelection(mCurrentCountryPos);
  }
  private void populateCountriesSpinner() {
    List<Country> countries = AutofillProfileBridge.getSupportedCountries();
    mCountryCodes = new ArrayList<String>();

    for (Country country : countries) {
      mCountryCodes.add(country.mCode);
    }

    ArrayAdapter<Country> countriesAdapter =
        new ArrayAdapter<Country>(getActivity(), android.R.layout.simple_spinner_item, countries);
    countriesAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    mCountriesSpinner.setAdapter(countriesAdapter);
  }