I want to show spinner element after click to TextView.
I use the next code
final Spinner spin = new Spinner(context);
String[] tlt = {"Apple", "Orange", "Plum"};
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(context, android.R.layout.simple_spinner_dropdown_item, tlt);
spin.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
Log.v("itemNO", position+"");
}
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});
spin.setAdapter(arrayAdapter);
txt = (TextView)findViewById(R.id.lang);
txt.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
spin.performClick();
}
});
In my .xml file only TextView present (may be it's wrong?)
After TextView click appear Spinner menu but when I choose some item nothing happen. It looks like setOnItemSelectedListener() does not work.
Have you any ideas?
Thank you for answers!
Please check this example from the google developer site:
http://developer.android.com/resources/tutorials/views/hello-spinner.html
You should add the Spinner to the xml file.
Related
enter image description hereI wanted to add a function to my spinner
I want to ADD a PRODUCT in my Food TABLE, I input a name of product "ADOBO" and I input a PRICE "50" and I want to put a "ADOBO" in FOOD CATEGORY and there's two item in my spinner, Category food and Category drinks. How do I put my Product in FOOD TABLE using Spinner
//SPINNER DROPDOWN
List<String> List = new ArrayList<>();
List.add("Food");
List.add("Drinks");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, List);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerCT.setAdapter(adapter);
}
public void Addproduct(){
btnSave.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String name = NameOfProduct.getText().toString().trim();
String price = PriceOfProduct.getText().toString().trim();
spinnerCT.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String itemvalue = parent.getItemAtPosition(position).toString();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
});
Remove Spinner onItemSelectedListener from onClick
And inside onClick use:
spinnerCT.getSelectedItem()
It gives you the category
And if you need to set the onItemSelectedListener, please set it in onCreate after setting adapter.
i got a spinner with some options, and i want to change visibility of text box when the specific "other" option get selected , how should i do this ?
this.arraySpinner = new String[] {"Mobile", "Gaming", "Media Features", "Medical", "Sports And Nutrition","Security","Space","Other"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, arraySpinner);
categories.setAdapter(adapter);
Try this:
.................
........................
this.arraySpinner = new String[] {"Mobile", "Gaming", "Media Features", "Medical", "Sports And Nutrition","Security","Space","Other"};
// Views
categories= (Spinner) findViewById(R.id.spinner);
textView = (TextView) findViewById(R.id.textView);
// Adapter
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, arraySpinner);
categories.setAdapter(adapter);
// Set onItemSelected listener to spinner
categories.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
TextView spinnerText = (TextView) view;
// Checking weather selected item is "Other" or not
if (spinnerText.getText().equals("Other"))
{
// Hide TextView
textView.setVisibility(View.INVISIBLE); // or View.GONE
}
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
.............
...................
Hope this will help
you can Implement OnItemSelected in the spinner and do the things what you want.
categories.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
if(arraySpinner!=null && arraySpinner[position].equalIgnoreCase("Other")){
// your code here
}
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});
I have a problem with my spinner. I add the items on the spinner dinamically, from a edittext when a button it's pressed. The items are correctly shown when the spinner is pressed, but no selection can be done and there's no display on the spinner.
Here's the code:
final ArrayList<String> players = new ArrayList<String>();
ArrayAdapter<String> adapter = new ArrayAdapter<String> (this, android.R.layout.simple_spinner_dropdown_item, players);
final Spinner spinnerplayers = (Spinner)findViewById(R.id.spinnerplayers);
final ImageButton addbutton = (ImageButton)findViewById(R.id.addbutton);
final EditText editname = (EditText)findViewById(R.id.editname);
final EditText editnum = (EditText)findViewById(R.id.editnum);
addbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
players.add(editname.getText().toString()+ " " + editnum.getText().toString());
editname.setText("");
editnum.setText("");
}
});
spinnerplayers.setAdapter(adapter);
final ImageButton removebutton = (ImageButton)findViewById(R.id.removebutton);
removebutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
//players.remove(spinnerplayers.getSelectedItem().toString());
}
});
If it's needed, the spinner xml is:
<Spinner
android:id="#+id/spinnerplayers"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1" />
Any idea about where's the problem?
Thanks in advance.
Have you tried to specify:
adapter.setNotifyOnChange(true);
to refresh adapter showing each add/remove you make ?
The reason is you set it with android.R.layout.simple_spinner_item.
You can write your own Adapter class:
Spinner spinner = (Spinner) findViewById(R.id.mySpinner);
spinner.setAdapter(new MyCustomAdapter(CLASS.this, R.layout.spinner_layout, players));
Then it will look like the way you design in spinner_layout
You have to make a custom adapter for this if you mean the clicklistenrs on your button are not working. You will apply listeners in getView method of the adapter.
Add you have to add OnItemSelectedListener.
YOUR_SPINNER.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int position, long arg3) {
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
The position gives you your item.
I have Async task to provide me list of some cities and after I have the list, I want to show me an OPEN spinner directly without any dialogs. My code opens spinner with propper list, but clickListener doesn't seems to work.
my code:
ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, spinnerCities);
listAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner = new Spinner(this);
spinner.setAdapter(listAdapter);
spinner.performClick();
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> aView, View selectedItemView,
int position, long longID) {
System.out.println("ON ITEM CLICK LISTENER HERE");
}
public void onNothingSelected(AdapterView<?> aView) {
}
});
What I am missing here?
So after several hours, I figured it out. I need to use Dialog, but result is the same I have wanted. Here is source in case, someone is find useful.
private void MyMethod(){
ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item,
(String[])MyClass.getListOfCities().toArray());
AlertDialog.Builder ad = new AlertDialog.Builder(this);
ad.setIcon(icon);
ad.setTitle("Title");
ad.setSingleChoiceItems( listAdapter, -1, new OnClickListener() {
public void onClick(DialogInterface dialog, int position) {
// DO something when I click on item
dialog.dismiss();
}
});
ad.show();
}
Im new to android. i have tried to built an application which has 2 spinner...
i have few doubts to clear before going deep to my application..
please give brief answer to my question...
is it possible to have 2 spinners in one view?
can i add setOnItemSelectedListener(this) for both?
if yes, how could i know which option is selected on both the spinners? like spinner.getSelectedItemPosition(); on both the spinners?
waiting for ur valuable replies. reference materials are also appreciable
Yes, you can have multiple spinner's that can be managed by single listener or you can have seperate Listener for each.
For Single Listener you can do something like this,
Set the Listener
spinner1.setOnItemSelectedListener(this);
spinner2.setOnItemSelectedListener(this);
Then you can use onItemSelected to get perform the action related to the spinner's.
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position,long arg3)
{
int id = parent.getId();
switch (id)
{
case R.id.spinner1:
// your stuff here
break;
case R.id.spinner2:
// your stuff here
break;
}
Check this:
Spinner spinner0, spinner1, spinner2;
TextView textView0, textView1, textView2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView0 = (TextView) findViewById(R.id.text0);
spinner0 = (Spinner) findViewById(R.id.spinner0);
ArrayAdapter<String> adapter0 = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_item, text0);
adapter0.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner0.setAdapter(adapter0);
spinner0.setOnItemSelectedListener(onItemSelectedListener0);
textView1 = (TextView) findViewById(R.id.text1);
spinner1 = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_item, text1);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(adapter1);
spinner1.setSelection(0,false); //make sure only one listener is
//active in the onCreate method by setting the other listener as false
spinner1.setOnItemSelectedListener(onItemSelectedListener1);
textView2 = (TextView) findViewById(R.id.text2);
spinner2 = (Spinner) findViewById(R.id.spinner2);
MySpinnerAdapter adapter2 = new MySpinnerAdapter(MainActivity.this, android.R.layout.simple_spinner_item, obj2);
//adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(adapter2);
spinner2.setSelection(0,false);
spinner2.setOnItemSelectedListener(onItemSelectedListener2);
}
OnItemSelectedListener onItemSelectedListener0 = new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//do your stuff spinner0
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
};
OnItemSelectedListener onItemSelectedListener1 = new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//do your stuff spinner1
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
};
OnItemSelectedListener onItemSelectedListener2 = new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//do your stuff spinner2
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
};
}
If the reference of two spinners are different(which will be , if you are using 2 spinners) , give different IDs for them and you can uniquely identify them, Yes you can set click listener to both of them individually.
spinner.setOnItemSelectedListener(new OnItemSelectedListener() { ... } );
spinner1.setOnItemSelectedListener(new OnItemSelectedListener() { ... } );
yes
yes
spinner1.getSelectedItemPosition(), spinner2.getSelectedItemPosition()