Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>
like image 861
IBunny Avatar asked Feb 05 '14 06:02

IBunny


People also ask

How do you make a clickable spinner?

Try removing the xml attribute android:clickable="true" from the Spinner widget. It could be that the entire spinner is registering the click event rather than the individual spinner items.

How to use spinner in Android App explain with an example?

Example to demonstrate the SpinnerUse ArrayAdapter to store the courses list. Create a single MainActivity that contains the spinner and on clicking any item of spinner Toast with that course name will be shown. Creating the activities: There will be one activity and hence one XML file for MainActivity. activity_main.


1 Answers

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"/>
like image 175
Thilek Avatar answered Sep 27 '22 18:09

Thilek