protected void onCreate()

in AutofillFramework/Application/src/main/java/com/example/android/autofill/app/commoncases/CreditCardSpinnersActivity.java [46:104]


    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.credit_card_spinners_activity);
        mCcExpirationDaySpinner = findViewById(R.id.expirationDay);
        mCcExpirationMonthSpinner = findViewById(R.id.expirationMonth);
        mCcExpirationYearSpinner = findViewById(R.id.expirationYear);
        mCcCardNumber = findViewById(R.id.creditCardNumberField);
        mCcSecurityCode = findViewById(R.id.creditCardSecurityCode);

        // Create an ArrayAdapter using the string array and a default spinner layout
        ArrayAdapter<CharSequence> dayAdapter = ArrayAdapter.createFromResource
                (this, R.array.day_array, android.R.layout.simple_spinner_item);
        // Specify the layout to use when the list of choices appears
        dayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        // Apply the adapter to the spinner
        mCcExpirationDaySpinner.setAdapter(dayAdapter);

        /*
        R.array.month_array could be an array of Strings like "Jan", "Feb", "March", etc., and
        the AutofillService would know how to autofill it. However, for the sake of keeping the
        AutofillService simple, we will stick to a list of numbers (1, 2, ... 12) to represent
        months; it makes it much easier to generate fake autofill data in the service that can still
        autofill this spinner.
        */
        ArrayAdapter<CharSequence> monthAdapter = ArrayAdapter.createFromResource(
                this, R.array.month_array, android.R.layout.simple_spinner_item);
        // Adapter created from resource has getAutofillOptions() implemented by default.
        monthAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        mCcExpirationMonthSpinner.setAdapter(monthAdapter);

        int year = Calendar.getInstance().get(Calendar.YEAR);
        for (int i = 0; i < years.length; i++) {
            years[i] = Integer.toString(year + i);
        }
        // Since the years Spinner uses a custom adapter, it needs to implement getAutofillOptions.
        mCcExpirationYearSpinner.setAdapter(
                new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, years) {
                    @Override
                    public CharSequence[] getAutofillOptions() {
                        return years;
                    }
                });
        findViewById(R.id.submit).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                submit();
            }
        });
        findViewById(R.id.clear).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AutofillManager afm = getSystemService(AutofillManager.class);
                if (afm != null) {
                    afm.cancel();
                }
                resetFields();
            }
        });
    }