Reputation: 542
I am using this to display a Spinner
type of view for TextInputLayout
but I am not getting how can I set any default value to it.
<com.google.android.material.textfield.TextInputLayout
style="@style/Widget.MaterialComponents.TextInputLayout.FilledBox.ExposedDropdownMenu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:hintTextColor="@color/primary_color_3">
<AutoCompleteTextView
android:id="@+id/accType_dropdown"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:backgroundTint="@android:color/transparent"
android:inputType="none"
android:text="@={viewModel.mytext}"
android:lineSpacingExtra="5sp"
android:textColor="@color/name_primary_color"
android:textSize="14sp" />
</com.google.android.material.textfield.TextInputLayout>
The placeholder only works when it is in focus mode so please suggest a workaround.
Edit:
I am using two way data binding in this textview and it seems due to this no solution is working in my case. Even i try to set default value for binded object then it automatically pop ups the spinner on application launch which i don't need so please suggest me something.
Upvotes: 5
Views: 4457
Reputation: 363895
The AutoCompleteTextView
can work with an Adapter
.
Just for example:
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/til"
..>
<com.google.android.material.textfield.MaterialAutoCompleteTextView
.../>
</com.google.android.material.textfield.TextInputLayout>
Create the adapter:
ArrayList<String> items = new ArrayList<>();
items.add("Material");
items.add("Design");
items.add("Components");
ArrayAdapter<String> adapter = new ArrayAdapter<>(this,R.layout.item, items);
TextInputLayout textInputLayout = findViewById(R.id.til);
Set the default value and the adapter:
((MaterialAutoCompleteTextView) textInputLayout.getEditText()).setAdapter(adapter);
((MaterialAutoCompleteTextView) textInputLayout.getEditText()).setText(adapter.getItem(1),false);
It is important to set the filter false
in setText(...,false)
to
display the entire list in dropdown and not only the single value.
Upvotes: 10
Reputation: 7230
Use android:text
attribute on AutoCompleteTextView
like this
<com.google.android.material.textfield.TextInputLayout
style="@style/Widget.MaterialComponents.TextInputLayout.FilledBox.ExposedDropdownMenu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:hintTextColor="@color/primary_color_3">
<AutoCompleteTextView
android:id="@+id/accType_dropdown"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Select Something"
android:backgroundTint="@android:color/transparent"
android:inputType="none"
android:lineSpacingExtra="5sp"
android:textColor="@color/name_primary_color"
android:textSize="14sp" />
</com.google.android.material.textfield.TextInputLayout>
Upvotes: 3