Reputation: 107
I need to customize spinner DropDown. It should be wits rounded corners. Now it looks like this:
This solution doesn't work.
Layout:
<androidx.appcompat.widget.AppCompatSpinner
android:id="@+id/messageSubject"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textAppearance="@style/Text.Default.Normal"
android:background="@color/transparent"
android:minHeight="@dimen/grid_6_25"
android:layout_marginTop="@dimen/grid_1_75"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/supportQuestion" />
Fragment:
class SupportFragment : Fragment(R.layout.fragment_support) {
private val viewBinding by viewBinding<FragmentSupportBinding>()
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
initView()
}
private fun initView() {
val adapter = ArrayAdapter.createFromResource(
requireContext(),
R.array.message_subject,
R.layout.spinner_selected
).also { adapter ->
adapter.setDropDownViewResource(R.layout.spinner_dropdown)
}
viewBinding.messageSubject.adapter = NothingSelectedSpinnerAdapter(requireContext(), adapter, R.layout.spinner_hint)
}
companion object {
fun newInstance() = SupportFragment()
}
}
Upvotes: 8
Views: 3710
Reputation: 454
Just use androidx.appcompat.widget.AppCompatSpinner
not Spinner
, and set rounded background.
Upvotes: 0
Reputation: 807
Create a Round Background spinnerbg.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#3B3B3B" />
<corners android:radius="@dimen/_4sdp" />
</shape>
Add it to Styles.
<style name="SpinnerTheme" parent="android:Widget.Material.Spinner.Underlined">
<item name="android:background">@drawable/spinnerbg
</item>
<item name="android:popupBackground">@drawable/spinnerbg
</item>
<item name="android:textAlignment">textStart</item>
</style>
Use the style
<androidx.appcompat.widget.AppCompatSpinner
android:id="@+id/spinner_season"
style="@style/SpinnerTheme"
android:layout_width="@dimen/_100sdp"
android:layout_height="@dimen/_30sdp"
android:layout_alignParentEnd="true"
android:layout_gravity="end"
android:layout_marginEnd="@dimen/_10sdp"
android:spinnerMode="dropdown" />
In the java file Add your custom layout if you need.
ArrayAdapter aa = new ArrayAdapter(context, R.layout.item_spinner, R.id.textview, season1);
aa.setDropDownViewResource(R.layout.item_spinner_dropdown);
Upvotes: 11
Reputation: 566
Create a background.xml finl and set this to the spinner background
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners
android:radius="30dp"/>
<solid
android:color="@color/colorWhite"/>
<stroke
android:color="@color/colorGray"
android:width="2dp"/>
</shape>
@color/colorWhite and @color/colorGray you can choose accordingly to your needs.
Then set it like this:
<androidx.appcompat.widget.AppCompatSpinner
android:id="@+id/messageSubject"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textAppearance="@style/Text.Default.Normal"
android:background="@drawable/background"
android:minHeight="@dimen/grid_6_25"
android:layout_marginTop="@dimen/grid_1_75"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/supportQuestion" />
Upvotes: 0