Android: Spinner tooltip

Is there any hint of a spinner that looks like a hint that is provided for editing text fields. I know that you can use a tooltip that gives you a title bar, but still leaves a blank field in the start column until you click on the counter. I currently have a rough way of setting a dummy field as the first part of a spinner array, which is a question, then check at the end to make sure the counter does not match the question line. Is there a cleaner / better way to do this?

Thank!

+46
java android spinner
Jul 06 '11 at 20:12
source share
3 answers

Here's a solution, which is probably a bit simpler than Ravi Weiss's code (thanks for the inspiration!):

ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_dropdown_item) { @Override public View getView(int position, View convertView, ViewGroup parent) { View v = super.getView(position, convertView, parent); if (position == getCount()) { ((TextView)v.findViewById(android.R.id.text1)).setText(""); ((TextView)v.findViewById(android.R.id.text1)).setHint(getItem(getCount())); //"Hint to be displayed" } return v; } @Override public int getCount() { return super.getCount()-1; // you dont display last item. It is used as hint. } }; adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); adapter.add("Item 1"); adapter.add("Item 2"); adapter.add("Hint to be displayed"); spinner.setAdapter(adapter); spinner.setSelection(adapter.getCount()); //display hint 
+131
Sep 12 '12 at 11:13
source share

You can configure your own asynchronous adapter and override the getView method to display a tooltip instead of an element. I created a sample project on github, check it out here

+21
Jul 12 '11 at 19:35
source share

Even easier than setting up your own adapter counter, just use the button and style it as a spinner with

 android:background="@android:drawable/btn_dropdown" 

Then set the onClick button to open the single-element selection dialog box. Then you can do whatever you want with the button text.

This has been my preferred way of handling this for a while. Hope this helps someone.

EDIT: I recently communicated with this (and someone asked me to post an example some time ago). This strategy will look a little different if you use the Holo theme. However, if you use other themes such as Theme.Black, this will look the same.

To demonstrate this, I made a simple application that has both a regular Spinner and its own custom counter. I threw this in a GitHub repo , but here is what the action looks like:

 package com.stevebergamini.spinnerbutton; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Spinner; public class MainActivity extends Activity { Spinner spinner1; Button button1; AlertDialog ad; String[] countries; int selected = -1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); spinner1 = (Spinner) findViewById(R.id.spinner1); button1 = (Button) findViewById(R.id.button1); countries = getResources().getStringArray(R.array.country_names); // You can also use an adapter for the allert dialog if you'd like // ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, countries); ad = new AlertDialog.Builder(MainActivity.this).setSingleChoiceItems(countries, selected, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { button1.setText(countries[which]); selected = which; ad.dismiss(); }}).setTitle(R.string.select_country).create(); button1.setOnClickListener( new OnClickListener(){ @Override public void onClick(View v) { ad.getListView().setSelection(selected); ad.show(); }}); } } 
+13
Jan 31 '12 at 20:13
source share



All Articles