I have a spinner that i fill with 4 elements. During startup onItemSelected method gets executed correctly and the toast message is displayed. However when i open the spinner and try to click any item, no event is called and the spinner popup will not close unless i press the spinner arrow. In other words I can not interact with spinner items.
Below is the activity code for the spinner
<Spinner
android:id="@+id/spinner"
android:popupBackground="@color/red"
android:clickable="true"
style="@style/spinner250"/>
Below is the style for the spinner
<style name="spinner250">
<item name="android:layout_width"> 250dp </item>
<item name="android:layout_height"> match_parent </item>
<item name="android:textColor"> @color/blue </item>
<item name="android:textSize"> 12sp </item>
</style>
And this is my java code for interacting with the spinner package com.example.android.gjobat;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;
import org.w3c.dom.Text;
import static android.R.attr.country;
import static android.widget.ArrayAdapter.createFromResource;
public class MainActivity extends AppCompatActivity implements OnItemSelectedListener {
public Spinner spinner;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final String[] items = {"Item 1", "Item 2", "Item 3", "Item 4"};
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, items);
Spinner spinner = (Spinner) findViewById(R.id.spinner);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
String item = parent.getItemAtPosition(position).toString();
// User selected item
Toast.makeText(getApplicationContext(), item + " selected!", Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent)
{
}
}
You can use onItemClickListener like below
After spending entirely one day with this i figured out that the problem was caused by
that was in my styles.xml file. I have no idea why this line of code caused the error but at least it works now after i removed that line. Below a full copy of my styles.xml file prior to removing the faulty line
I hope this helps someone in the future
Try removing the xml attribute
android:clickable="true"
from theSpinner
widget. It could be that the entire spinner is registering the click event rather than the individual spinner items.Possible duplicate of Spinner functionality issues in Android 6.0 Marshmallow
SOMETIMES THE ISSUE MAY ALSO BE BECAUSE YOU HAVE PLACED YOUR SPINNER OUTSIDE THE LAYOUT YOU ARE CALLING . THIS WILL NOT SHOW ANY ERROR BUT YOU WILL NOT BE ABLE TO INTERACT WITH THE SPINNER VIEW