OnClickListener to Spinner ? ?? - android

I need an OnClickListener to my Spinner.
I tryed everything i found on the internet, but none of them worked.
Please give me a solution for this:
Got my spinner here:
spinner = (Spinner)findViewById(R.id.spinner1);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource
(this, R.array.gyerekek_array, R.layout.my_spinner);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new ItemChooser());
And this is my_spinner.xml:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:gravity="center_vertical|center_horizontal"
android:textColor="#000000"
android:paddingLeft="50dp"
android:text="Sample Text"
android:textStyle="bold"
android:textSize="31sp"
>
</TextView>
I tryed everything from stackoverflow and everything else from the internet. I just cant make it...
I cannot add onClickListener to the spinner's default TextView because i dont use the default spinner, i got my own my_spinner.xml.
In Fact, if i add an "ID" to my Text View inside the my_spinner.xml i got a null pointer exception.
I even tryed to add an invisible TextView atop of the Spinner, but then only one view can be sensored by OnClick.
Please help me guys.

use this
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View view, int position, long id) {
int item = spinner.getSelectedItemPosition();
}
public void onNothingSelected(AdapterView<?> arg0) { }
});

you can't add an onclicklistener on any adapter view try to use setOntouchlistener hope it will help you
spinner = (Spinner)findViewById(R.id.spinner1);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource
(this, R.array.gyerekek_array, R.layout.my_spinner);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setOnTouchlistener(this);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new ItemChooser());
//////////////////////////// your listener
public boolean onTouch(View v, MotionEvent event) {
Log.d("Spinner","clicked");
return false;
}

Related

Android spinner doesn't show items in lollipop emulator

I have a spinner widget in my activity. If I run the app on my device (Pie version) the spinner works fine but if I run it on emulator (which has lollipop version- requirements of work)- spinner doesn't show any items at all.
So this is my activity code for the spinner (inside onCreate):
spinner = (Spinner) findViewById(R.id.spinner_reminder_times);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> spinnerAdapter = ArrayAdapter.createFromResource(getApplicationContext(),
R.array.reminder_times_array, android.R.layout.simple_spinner_dropdown_item);
// Specify the layout to use when the list of choices appears
spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the spinnerAdapter to the spinner
spinner.setAdapter(spinnerAdapter);
spinner.setOnItemSelectedListener(this);
And this is the implemented method:
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
switch (parent.getSelectedItemPosition()) {
case 0:
// do stuff
break;
case 1:
// do stuff
break;
case 2:
// do stuff
break;
}
}
The spinner xml in activity_layout:
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/spinner_reminder_times"
android:theme="#style/ThemeOverlay.AppCompat.Light"
android:spinnerMode="dropdown">
</Spinner>
What may be the problem? how comes it works perfectly on my device but on android lollipop emulator doesn't show anything?
Thanks!
Create a new layout file, called my_spinner_dropdown_list_item.xml (probably change textColor once you confirm it works):
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
style="?android:attr/spinnerDropDownItemStyle"
android:singleLine="true"
android:layout_width="match_parent"
android:textColor="#FF0000"
android:layout_height="?android:attr/dropdownListPreferredItemHeight"
android:ellipsize="marquee"/>
And assign this layout as your Spinner's new dropdown resource:
spinner = (Spinner) findViewById(R.id.spinner_reminder_times);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> spinnerAdapter = ArrayAdapter.createFromResource(getApplicationContext(),
R.array.reminder_times_array, R.layout.my_spinner_dropdown_list_item);
// Specify the layout to use when the list of choices appears
spinnerAdapter.setDropDownViewResource(R.layout.my_spinner_dropdown_list_item);
// Apply the spinnerAdapter to the spinner
spinner.setAdapter(spinnerAdapter);
spinner.setOnItemSelectedListener(this);
enter code here
android:popupBackground="#59b0b9"
// try this to change the popupBackground color
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/spinner_reminder_times"
android:theme="#style/ThemeOverlay.AppCompat.Light"
android:popupBackground="#59b0b9"
android:spinnerMode="dropdown"/>

Android GridView setSelection

When a Fragment is inflated, by default I want to item no 10 to be selected.
I tried it with gridview.setSelection(10); but it is not working for me. I read in the forum that others have done it successfully , but I am not achieving the correct output. what should I do?
orari_fine = (GridView) findViewById(R.id.gridView4);
ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, orari_f);
orari_fine.setAdapter(adapter1);
orari_fine.setSelection(10);
orari_fine.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
OraFine = ((TextView) v).getText().toString();
}
});
XML
<GridView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:listSelector="#drawable/selector"
android:id="#+id/gridView4"
android:background="#9051585B"
android:choiceMode="singleChoice"
android:numColumns="8"
android:textAlignment="center"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:layout_below="#+id/textView13" />
instead of using gridview.setSelection(10); try using gridview.setItemChecked(10, true);
try do this
// set item postion you wanna select
orari_fine.setSelection(10);
orari_fine.setFocusableInTouchMode(true);
orari_fine.requestFocus();
// Notifies the attached observers that the underlying data has been changed
adapter1.notifydatasetchanged();

ActionView/Spinner with match_parent in action bar

I need spinner in Action Bar for filtering content, looking something similar to this:
But spinner should fill full length of action bar.
What i tried:
1) actionBar.setListNavigationCallbacks(navigationAdapter, this);
Yes, it works. But i need this filter only in 1 fragment. Yes, i can use ActionBar.NAVIGATION_MODE_LIST only in this fragment, and change it to ActionBar.NAVIGATION_MODE_STANDARD in others. But if i will want to add differenat filter in another fragment. So, i should to setting type, adapter and etc in every fragment. It's very very awfully.
2) Better solution is to use menu. In onCreateOptionsMenu of fragment i can add item with actionViewClass="android.widget.Spinner" only in fragment where i need this. Almost good solution except that this item will align right and doesn't fill full action bar.
So, my question is: how to make menu item fill full action bat length?
Or can you advise better solution?
Btw, setCustomView for ActionBar - bad solution for the same reasons that setListNavigationCallbacks...
Found solution. You should user your own layout for spinner item.
Instead of:
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getActivity(), R.array.projects_filteres, android.R.layout.simple_spinner_item);
Use:
ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(getActivity(), R.layout.projects_filter_item,
android.R.id.text1, getActivity().getResources().getStringArray(R.array.projects_filteres));
Where R.array.projects_filteres - just simple array from resources, and R.layout.projects_filter_item is RelativeLayout with android:layout_width="match_parent":
<?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="wrap_content"
android:gravity="fill_horizontal"
android:orientation="vertical" >
<TextView
android:textColor="#000"
android:id="#android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
So, whole code of onCreateOptionsMenu in fragment looks something like this:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
menu.clear();
inflater.inflate(R.menu.action_bar_context_menu, menu);
android.view.MenuItem filter = menu.findItem(R.id.context_menu_filter);
ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(getActivity(), R.layout.projects_filter_item,
android.R.id.text1, getActivity().getResources().getStringArray(R.array.projects_filteres));
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Spinner spinner = (Spinner)MenuItemCompat.getActionView( filter);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
}

Trying to implement an android spinner. but when I touch it, the dropdown menu doesn't open. nothing happens. why?

I was trying to implement an android spinner, a button, if I click should open a dropdown menu showing three things: "bluetooth, speak, headphones"
but all i see now is the button, (no text), and when I click on it, nothing happens. what am i missing?
here is my code:
in the xml i have:
<RelativeLayout
android:id="#+id/audio_routing"
android:layout_width="40dp"
android:layout_height="40dp"
android:padding="10dp" >
<Spinner
android:id="#+id/audio_routing_spinner"
android:layout_width="20dp"
android:layout_height="40dp"
android:contentDescription="#string/audio_routing_desc"
android:scaleType="fitXY"
android:src="#drawable/bluetooth" />
</RelativeLayout>
in the strings file i have:
<?xml version="1.0" encoding="utf-8"?>
<resources>
...
<string name="audio_routing_desc">Audio Routing Button</string>
<!-- Audio Routing Drop Down List -->
<string-array name="audio_routing_array">
<item>Bluetooth</item>
<item>Headphones</item>
<item>Speaker</item>
</string-array>
in my java file i have:
void audio_routing() {
kcLogger.info("audio_routing", "audio_routing function is called");
Spinner spinner = (Spinner) mKCSWindowView.findViewById(R.id.audio_routing_spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(service,R.array.audio_routing_array,android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// TODO Auto-generated method stub
kcLogger.info("audio_routing", "onItemSelected");
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
kcLogger.info("audio_routing", "onNothingSelected");
}
});
}
Change android:contentDescription to android:entries
<Spinner
android:id="#+id/audio_routing_spinner"
android:layout_width="20dp"
android:layout_height="40dp"
android:entries="#array/audio_routing_desc"
android:scaleType="fitXY"
android:src="#drawable/bluetooth" />
android:contentDescription is for accessibility features.
android:entries is used to set array data to UI like spinner, listview, gridview,etc.

Change color of a spinner that use android.R.layout.simple_spinner_item

Use android.R.layout.simple_spinner_item.
I use this class java:
final Spinner s = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter adapter = ArrayAdapter.createFromResource(
this, R.array.seleziona, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s.setAdapter(
new NothingSelectedSpinnerAdapter(
adapter,
R.drawable.contact_spinner_row_nothing_selected,
// R.layout.contact_spinner_nothing_selected_dropdown, // Optional
this));
s.setPrompt("Seleziona");
s.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
String voceSelezionata = (String) s.getSelectedItem();
}
public void onNothingSelected(AdapterView<?> arg0) {
}
});
}
By default, the color is blue.
How change only color in red?`
You mean textcolor?
Make custom xml file for your spinner item.
spinner_item.xml:
give your customized color and size to text in this file.
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="20dip"
android:gravity="left"
android:textColor="#FF0000"
android:padding="5dip"
/>
Now use this file to show your spinner items like:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.spinner_item,list);
You don't need to set drop down resource.it will take spinner_item.xml only to show your items in spinner.

Categories

Resources