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>
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.
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.
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"/>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With