Android - Create an AutoCompleteTextView Programmatically (no xml at all)

Viewed 42

I would like to add an AutoCompleteTextView Programmatically for an app, without referencing any items in a XML layout.

This is what I would like to achieve:

enter image description here

The XML code is the following:

<com.google.android.material.textfield.TextInputLayout
    android:id="@+id/weekDropdown"
    
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.ExposedDropdownMenu"
    android:hint="@string/select_week"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginStart="32dp"
    android:layout_marginEnd="32dp"
    android:layout_marginTop="5dp"
    android:layout_marginBottom="5dp"
    android:labelFor="@+id/weekSelected"
    app:startIconDrawable="@drawable/ic_round_calendar_today_24">

    <AutoCompleteTextView
        android:id="@+id/weekSelected"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:padding="14dp"
        android:textSize="16sp" />

</com.google.android.material.textfield.TextInputLayout>

------------- UPDATE ---------------------

So this is the state that i reached... its almost there, I just can't get the outline box to show up correctly

enter image description here

Here is the code:

// Create AutoCompleteTextView Dynamically
AutoCompleteTextView textView = new AutoCompleteTextView(this.context);

TextInputLayout.textInputLayout = new TextInputLayout(new ContextThemeWrapper(getContext(), R.style.Widget_MaterialComponents_TextInputLayout_OutlinedBox_ExposedDropdownMenu));

this.textInputLayout.setId(View.generateViewId());
this.textView.setId(View.generateViewId());

LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);


textInputLayout.setHint("Select Week");


textInputLayout.setBoxBackgroundColor(ContextCompat.getColor(getContext(), android.R.color.transparent));

textInputLayout.setBoxBackgroundMode(TextInputLayout.BOX_BACKGROUND_OUTLINE);

textInputLayout.setStartIconDrawable(R.drawable.ic_round_calendar_today_24);

textInputLayout.setStartIconTintList(ContextCompat.getColorStateList(this.context, R.color.black));

textInputLayout.setBoxCornerRadii(5, 5, 5, 5);


LinearLayout.LayoutParams layoutParamsTextView = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);

weeksArray = new ArrayList<>();
//
weeksArray.add("1");
weeksArray.add("2");
weeksArray.add("3");
weeksArray.add("4");
weeksArray.add("5");
weeksArray.add("6");
weeksArray.add("7");
weeksArray.add("8");

weeksAdapter = new ArrayAdapter<>(getContext(), android.R.layout.simple_list_item_1, weeksArray);
    
textView.setAdapter(weeksAdapter);
    
textView.setText(String.valueOf(currWeek), false);

textInputLayout.addView(this.textView, layoutParamsTextView);

this.addView(this.textInputLayout, layoutParams);

Does anyone see what i am missing, or how i can achieve the expected result?

1 Answers

Add autoCompleteTextView into your textInputLayout

in Kotlin you can do like this

val autoCompleteTextView = AutoCompleteTextView(this)
autoCompleteTextView.id = View.generateViewId()
textInputLayout.addView(autoCompleteTextView)

Java Version

AutoCompleteTextView autoCompleteTextView = new AutoCompleteTextView(context);
autoCompleteTextView.setId(View.generateViewId());
textInputLayout.addView(autoCompleteTextView);

if you don't like use XML to create UI, you can migrate to Jetpack Compose

Related