IBunny
IBunny

Reputation: 319

Items are not selectable in spinner (Custom Spinner Adapter)

I used custom spinner adapter for color spinner in android application. The drop down is working fine. But once I select a color(item) from spinner, it is not selectable. Also I do not need to show selected item as it is selected. I only want to identify the selected color without displaying it.

Below is code for my CustomSpinnerAdapter:

@Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {
        TextView rowView=null;
        if(convertView == null){
            convertView=inflater.inflate(R.layout.spinner_layout, null);
        }
        rowView=(TextView) convertView.findViewById(R.id.spinnerColorview);
        rowView.setBackgroundColor(Color.parseColor(itemList.get(position)));

        return convertView;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        TextView rowView=null;
        if(convertView == null){
            convertView=inflater.inflate(R.layout.spinner_layout, null);
        }
        rowView=(TextView) convertView.findViewById(R.id.spinnerColorview);
        rowView.setBackgroundColor(Color.parseColor(itemList.get(position)));

        return convertView;

    }

EDIT: MORE INFORMATION

My drop down list in spinner not selectable. When I clicked on the spinner it is displaying the list. But when I select one item from that list, nothing happen. I cannot identify selected item.

When I print the position inside getView(int position, View convertView, ViewGroup parent) method, it prints all item ids.

I only need to identify selected item and I do not need to display it at the top of spinner as it usually does. This is my spinner_layout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:clickable="true"
    android:orientation="horizontal"
    android:paddingLeft="40dp"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/spinnerColorview"
        android:layout_width="200px"
        android:layout_height="50px"
        android:clickable="true"
        android:gravity="center_vertical"
         >
    </TextView>
</LinearLayout>

Upvotes: 6

Views: 4560

Answers (1)

Thilek
Thilek

Reputation: 676

I added style="?android:attr/spinnerItemStyle" to the textview and it works. Not sure that the best solutions but it's a start and a quick fix.

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
      style="?android:attr/spinnerItemStyle"
      android:id="@+id/TextView"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:padding="8dp"
      android:ellipsize="end"
      android:gravity="left|center"
      android:singleLine="true"
      android:text="Option One"
      android:textColor="@color/Petrol"
      android:textSize="@dimen/font_size_big"
      android:textStyle="bold"/>

Upvotes: 1

Related Questions