2

I have searched a lot to find the right answer to my query but unfortunately got no working solution.

I have a Spinner,

<Spinner
    android:id="@+id/dropdown_payment_methods"
    android:layout_width="match_parent"
    android:layout_height="@dimen/height_list_of_societies_xhdpi"
    android:gravity="center"
    android:layout_gravity="center"
    android:layout_marginLeft="@dimen/margin_left_right_dropdown_payment_methods_xhdpi"
    android:layout_marginRight="@dimen/margin_left_right_dropdown_payment_methods_xhdpi"
    android:entries="@array/various_payment_methods"
    android:popupBackground="@android:color/white"
    android:spinnerMode="dialog" />

Note: I am not opening my Spinner as default dropdown option but like a dialog. The closest reference I could find was the link below.

Android spinner divider color

but it also mentions that the style only works for default dropdown option.

Need help with how I can customize my divider color and height when I am opening my Spinner as a Dialog.

Community
  • 1
  • 1
Gaurav Saluja
  • 357
  • 3
  • 11
  • Create custom adapter with custom view – NickF Feb 03 '15 at 08:18
  • @NickF: Already tried it. No use. public View getCustomView(int position, View convertView, ViewGroup parent) { View row = inflater.inflate(R.layout.dropdown_payment_methods, parent, false); TextView paymentMethodName = (TextView) row.findViewById(R.id.payment_methods_list); paymentMethodName.setText(data[position].trim()); paymentMethodName.setTypeface(tf); paymentMethodName.setTextColor(context.getResources().getColor(R.color.mygreenbox_grey)); return row; } This will populate the Spinner but will not remove the divider. Creating custom adapter is good enough for dropdown. – Gaurav Saluja Feb 03 '15 at 08:48
  • @GauravSaluja did you find a solution? – ilyamuromets Apr 05 '17 at 13:47
  • 1
    @ilyamuromets No. I had to switch to a different UI requirement and avoided Spinner. – Gaurav Saluja Apr 06 '17 at 06:19

2 Answers2

0

create a custom item layout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background_spinner_data" >
    <TextView
        android:id="@+id/spinner_item"
        style="@style/TextNormal14Grey"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:ellipsize="end" />
<TextView
        android:id="@+id/border"
        android:background="@android:color/black"
        android:layout_width="fill_parent"
        android:layout_height="2dp"
        android:ellipsize="end" />

</RelativeLayout>

The border corresponds to your divider.

then use an array Adapter

 ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.custom_item,R.id.spinner_item,getResources.getStringArray(R.id.various_payment_methods));

Then set this to the spinner in your activity

((Spinner)findViewById(R.id.dropdown_payment_methods)).setAdapter(adapter);
Droidekas
  • 3,464
  • 2
  • 26
  • 40
0

Dialog spinners use AlertDialog to display their contents. AlertDialog has a reference to AlertController which controls the ListView within the dialog. You need to access that listview to change the divider color of spinner. And it seems like only way to access that Listview is creating a custom spinner adapter by extending ArrayAdapter.

@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) { 
    removeDividers(parent);
    ..
}
private void removeDividers(View view) {
    if (view instanceof ListView) {
        ((ListView) view).setDivider(new ColorDrawable(ContextCompat.getColor(getContext(), android.R.color.holo_blue_dark)));
        ((ListView) view).setDividerHeight(10);
    }
}
MertNYuksel
  • 311
  • 1
  • 8