ActionView/Spinner with match_parent in action bar - android

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) {
}
});
}

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: Customized menu in toolbar

Currently, I have a spinner menu attached to my toolbar.
toolbar_spinner_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/overview_spinner"
app:actionViewClass="android.widget.Spinner"
app:showAsAction="always"
android:title="" />
</menu>
I inflated the menu in my fragment by calling
AFragment.java
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.toolbar_spinner_menu, menu);
final int[] previousSelection = {0};
final MenuItem item = menu.findItem(R.id.overview_spinner);
final Spinner mSpinner = (Spinner) MenuItemCompat.getActionView(item);
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), R.layout.spinner_item, SPINNER_LIST);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mSpinner.setAdapter(adapter);
mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
switch (position) {
case 0: // Recent
break;
case 1: // Now
break;
default:
break;
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
super.onCreateOptionsMenu(menu, inflater);
}
R.layout.spinner_item
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/spinner_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textColor="#FFFFFF"
android:textStyle="bold"
android:gravity="start" />
When an item is selected from the spinner, the user will be prompted to select a month and year from a dialog box. Upon clicking on "OK", the spinner's text should be updated into the choice made.
But this is what i get when I try to update the text dynamically using the following block of code, and as you can see, the text got cut off.
mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
TextView selectedText = (TextView) getActivity().findViewById(R.id.spinner_text);
switch (position) {
case 0: // Recent
selectedText.setText("Since 1/8/2016");
break;
case 1: // Now
What I want to achieve is as following in the image illustration from Citymapper.
Any help rendered will be much appreciated!
I think the problem is with the spinner item width..your textview is getting too much space from the action bar rather than the space available..try lowering the textsize or you can remove the title from action bar as shown in the green one.
Solved the issue by modifying R.layout.spinner_item by changing gravity to start from right and setting a fixed layout_width in addition to some padding so it does not stick too close to the icon.
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/spinner_text"
android:layout_width="150dp"
android:paddingRight="8dp"
android:layout_height="wrap_content"
android:textSize="15sp"
android:textColor="#FFFFFF"
android:textStyle="bold"
android:gravity="right" />

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.

Is it possible to use sliding tabs and a spinner in the action bar in Android

I currently have an Activity with sliding tabs on it and I have been using ActionBarSherlock, but I wanted to have a spinner in the action bar, so that the user could select items from the spinner and each item is a specific file which opens up values to re-populate the views on each tab. I have looked to see how you can do this and so far have not had any luck. I don't want them to both navigate through fragments, but instead have the spinner to re-populate views on the tabs. I looked at a similar question Is it possible to use dropdown AND tabs as navigation in the action bar? which is how I want it to look, but his question was to use two different types of navigation. Please can anyone help me and let me know if this is possible. Thank you in advance. I have also just looked at an Android application that I have which is the Eurosport app and that has the type of layout that I am looking for if anyone has that application to see what I mean.
Here is my code:
public class SlidingTabsActivity extends SherlockFragmentActivity
{
ViewPager viewPager;
TabsAdapter tabsAdapter;
ActionBar actionBarTabs;
Spinner spinner;
ArrayAdapter<String> arrayAdapter;
LayoutInflater spinnerLayoutInflater;
View spinnerView;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
viewPager = new ViewPager(this);
viewPager.setId(R.id.pager);
setContentView(viewPager);
arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, R.array.device_description);
arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerLayoutInflater = (LayoutInflater) this.getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
spinnerView = spinnerLayoutInflater.inflate(R.layout.spinner, null);
spinner = (Spinner) spinnerView.findViewById(R.id.tabsSpinner);
spinner.setAdapter(arrayAdapter);
spinner.setOnItemSelectedListener(new OnItemSelectedListener()
{
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3)
{
// TODO Auto-generated method stub
}
#Override
public void onNothingSelected(AdapterView<?> arg0)
{
// TODO Auto-generated method stub
}
});
actionBarTabs = getSupportActionBar();
actionBarTabs.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBarTabs.setCustomView(spinnerView);
actionBarTabs.setDisplayShowCustomEnabled(true);
actionBarTabs.setDisplayHomeAsUpEnabled(true);
tabsAdapter = new TabsAdapter(this, viewPager); // Declares the tabs adapter class with the view pager view
/* Adds fragments to the tabs adapter */
tabsAdapter.addTab(actionBarTabs.newTab().setText("PV"), Fragment_1.class, null);
tabsAdapter.addTab(actionBarTabs.newTab().setText("CONFIG"), Fragment_2.class, null);
tabsAdapter.addTab(actionBarTabs.newTab().setText("DIAG"), Fragment_3.class, null);
}
Here is my Spinner code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Spinner
android:id="#+id/tabsSpinner"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" />
</RelativeLayout>
I get this message in the logcat when pressing the Spinner:
11-26 12:22:44.282: W/InputEventReceiver(7217): Attempted to finish an input event but the input event receiver has already been disposed.
11-26 12:22:44.282: W/InputMethodManagerService(525): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy#4216a290 attribute=null, token = android.os.BinderProxy#42618330
So, first of all create a markup file to place your Spinner, e.g:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent" >
<Spinner
android:id="#+id/spinner"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:spinnerMode="dropdown" >
</Spinner>
</RelativeLayout>
Next create a menu resource, e.g. main_menu.xml and put it to res/menu folder:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_change_content"
android:actionLayout="#layout/spinner_layout"
android:orderInCategory="100"
android:showAsAction="always"
android:title="#string/empty">
</item>
</menu>
Paid your attention that we specified custom action layout for this menu item - this is a layout we have created earlier and it contains our Spinner. Next in your Activity(or Fragment) you must inflate this menu:
Use this if you creating this menu in Activity
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
...// init spinner, will be explained below
return true;
}
Or if you creating menu in Fragment:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.clear();
inflater.inflate(R.menu.card_storage, menu);
...// init spinner
}
If you creating this menu for Fragment don't forget to call setHasOptionMenu(true) somewhere in Fragment.onCreate().
Put this instead of ... // init spinner
MenuItem item = menu.findItem(R.id.action_change_content); // id of our custom menu item
CustomSpinnerAdapter adapter = new CustomSpinnerAdapter(); // your adapter
Spinner spinner = (Spinner) item.getActionView().findViewById(R.id.spinner);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
I hope I didn't missed out something. Good luck!

OnClickListener to Spinner ? ??

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;
}

Categories

Resources