I want the spinner to open up by itself when I run the activity, so I'm using the performClick() method but it's showing this error:
Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?
What should I do?
Here is my code
public class FacilityComplaint extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
private Spinner spinner1;
private static final String[] suggestions = {"Select from suggestions", "Switch not working",
"Switch faulty", "Switch light not working", "Switch handle faulty"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_facility_complaint);
spinner1 = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(FacilityComplaint.this,
android.R.layout.simple_spinner_item, suggestions);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(adapter);
spinner1.setOnItemSelectedListener(this);
spinner1.performClick();
}
public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
switch (position) {
case 0:
//do something
break;
case 1:
//do something
break;
case 2:
//do something
break;
case 3:
//do something
break;
case 4:
//do something
break;
}
}
public void onNothingSelected(AdapterView<?> parent) {
}
}
For spinner you have don't need to implement "AdapterView.OnItemSelectedListener" Create like this .
spinner1 = (Spinner) findViewById(R.id. spinner1);
ArrayAdapter adapter = ArrayAdapter.createFromResource(this,
suggestions, android.R.layout.simple_spinner_item);
adapter1.setDropDownViewResource(
android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(adapter);
spinner1.performClick();
// Get selecte index or Suggestions
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
String selectedSugg = suggestions[pos];
Log.d("Suggestions ", selectedSugg);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
Related
Can someone please help me. I have just started Android development and I want to create an app which has a drop down menu. There seems to be two errors in my code which I am unable to resole. The errors are at implements OnItemSelectedListner and at spinner.setOnItemSelectedListener(this);
public class MainActivity extends ActionBarActivity implements OnItemSelectedListener{
int Cups = 1;
int Price = 1;
int Sum = 0;
private Spinner spinner;
private static final String[]paths = {"item 1", "item 2", "item 3", "item 4", "item 5"};
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner = (Spinner)findViewById(R.id.spinner);
ArrayAdapter<String>adapter = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item,paths);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
}
public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
switch (position)
{
case 0:
Price=0.5;
break;
case 1:
Price=1;
break;
case 2:
Price=2;
break;
case 3:
Price=3;
break;
case 4:
Price=4;
break;
}
}
The first Error is to implement the correct OnItemClickListener
implements AdapterView.OnItemClickListener
The second is to override the correct method from the listener which is
#Override
onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
Why is this code not working? is there any problem with switch case?
I have 2 spinners (of branch and semester). When a branch is selected it must show a Toast of selected branch and same with the semester.
public class MainActivity extends Activity implements OnItemSelectedListener, OnClickListener{
Spinner branch, semester;
Button go;
TextView branchText, semText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
branch = (Spinner) findViewById(R.id.spinner1);
semester = (Spinner) findViewById(R.id.spinner2);
go = (Button) findViewById(R.id.button1);
ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.Branch, android.R.layout.simple_spinner_item);
branch.setAdapter(adapter);
branch.setOnItemSelectedListener(this);
ArrayAdapter adapter1 = ArrayAdapter.createFromResource(this, R.array.Semester, android.R.layout.simple_spinner_item);
semester.setAdapter(adapter1);
semester.setOnItemSelectedListener(this);
go.setOnClickListener(this);
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
switch (view.getId())
{
case R.id.spinner1:
branchText = (TextView) view;
Toast.makeText(this, branchText.getText(), Toast.LENGTH_SHORT).show();
break;
case R.id.spinner2:
semText = (TextView) view;
Toast.makeText(this, semText.getText(), Toast.LENGTH_SHORT).show();
break;
}
}
You should get the id of selected item by "AdapterView parent" not "View view".
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
switch (parent.getId()){
case R.id.spinner1:
branchText = (TextView) view;
Toast.makeText(this, branchText.getText(), Toast.LENGTH_SHORT).show();
break;
case R.id.spinner2:
semText = (TextView) view;
Toast.makeText(this, semText.getText(), Toast.LENGTH_SHORT).show();
break;
}
}
Change Toast.makeText(this, branchText.getText(), Toast.LENGTH_SHORT).show(); to Toast.makeText(this, branchText.getText().tostring(), Toast.LENGTH_SHORT).show();
change all branchText.getText() to branchText.getText().tostring()
Reference branch array and use position to retrieve branch text:
String[] branchArr = getResources().getStringArray(R.array.Branch);
case R.id.spinner1:
Toast.makeText(this, branchArr[position], Toast.LENGTH_SHORT).show();
break;
public class StartCalvert extends Activity {
Spinner spnr,spnr2;
String[] numsys = {
"Binary",
"Decimal",
"Octal",
"Hexadecimal"
};
// this numsys how will i declare
String[] numsys2 = {
"Binary",
"Decimal",
"Octal",
"Hexadecimal"
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.startcalvert);
spnr2 = (Spinner)findViewById(R.id.spinner2);
spnr = (Spinner)findViewById(R.id.spinner);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item, numsys);
//declare here , like what above alike .
spnr.setAdapter(adapter);
spnr.setOnItemSelectedListener(
new AdapterView.OnItemSelectedListener()
Each Spinner has it's adapter has bind its Datasource with view on it so , most likely you need to make one for each Spinner and any other Data Views
in onCreate
spinner1 = (Spinner) findViewById(R.id.spinner1 );
spinner2 = (Spinner) findViewById(R.id.spinner2 );
ArrayAdapter<String> adapterSpinner1 = new ArrayAdapter<String>(
SpinnerActivity.this, R.layout.simple_spinner_item,
numsys);
spinner1.setDropDownViewResource(R.layout.simple_spinner_item);
spinner1.setAdapter(adapterSpinner1);
ArrayAdapter<String> adapterSpinner2 = new ArrayAdapter<String>(
SpinnerActivity.this, R.layout.simple_spinner_item,
numsys2);
spinner2.setDropDownViewResource(R.layout.simple_spinner_item);
spinner2.setAdapter(adapterSpinner2);
ItemSelectedListener itemSelectedListener = new ItemSelectedListener();
spinner1.setOnItemSelectedListener(itemSelectedListener);
spinner2.setOnItemSelectedListener(itemSelectedListener);
Declare inner class
class ItemSelectedListener implements OnItemSelectedListener {
#Override
public void onItemSelected(AdapterView<?> parent, View v, int position,
long arg3) {
LoggerGeneral.info("OnItemselected pressed");
switch (parent.getId()) {
case R.id.spinner1:
break;
case R.id.spinner2:
break;
default:
break;
}
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
}
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()
I am very new to android. I want to use 2 spinners in my application, one shows the countries list, when any country is selected the other spinner should show the list of cities of that country. when city is selected some action is performed. plz help me with some sample code. thanks in anticipation
Here is something what we can use to add options to spinner2 w.r.t to spinner 1.
public class Activity extends Activity implements View.OnClickListener
{
private Spinner spinner0, spinner1, spinner2, spinner3;
private Button submit, cancel;
private String country[], state[], city[], area[];
Australia aus = new Australia();
Object object;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
spinner0 = (Spinner)findViewById(R.id.spinnerCountry);
spinner1 = (Spinner)findViewById(R.id.spinnerQ1);
spinner2 = (Spinner)findViewById(R.id.spinnerQ2);
spinner3 = (Spinner)findViewById(R.id.spinnerQ3);
submit = (Button)findViewById(R.id.btnSubmit);
cancel = (Button)findViewById(R.id.btnCancel);
submit.setOnClickListener(this);
cancel.setOnClickListener(this);
country = new String[] {"Select Country", "Australia", "USA", "UK", "New Zealand", "EU", "Europe", "China", "Hong Kong",
"India", "Malaysia", "Canada", "International", "Asia", "Africa"};
ArrayAdapter<String> adapter0 = new ArrayAdapter<String>(Activity.this, android.R.layout.simple_spinner_item, country);
adapter0.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
spinner0.setAdapter(adapter0);
Log.i("AAA","spinner0");
spinner0.setOnItemSelectedListener(new OnItemSelectedListener()
{
#Override
public void onItemSelected(AdapterView<?> arg0, View view1, int pos, long id)
{
Log.i("AAA","OnItemSelected");
int loc;
loc = pos;
switch (loc)
{
case 1:
state = aus.getState();
object = aus;
Log.i("AAA","ArrayAdapter1");
ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(Activity.this, android.R.layout.simple_spinner_item, state);
adapter1.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
spinner1.setAdapter(adapter1); Log.i("AAA","spinner1");
break;
default:
Log.i("AAA","default 0");
break;
}
}
#Override
public void onNothingSelected(AdapterView<?> arg1)
{
Log.i("AAA","Nothing S0");
}
});
spinner1.setOnItemSelectedListener(new OnItemSelectedListener()
{
#Override
public void onItemSelected(AdapterView<?> arg0, View view1, int pos, long id)
{
Log.i("AAA","OnItemSelected S1");
int loc = pos;
switch(loc)
{
case 1:
Log.i("AAA","Australia");
if(object.equals(aus))
{
city = aus.getType(loc);
}
else
{
break;
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(Activity.this, android.R.layout.simple_spinner_item, city);
adapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
spinner2.setAdapter(adapter); Log.i("AAA","spinner2");
break;
default:
Log.i("AAA", "default 1");
break;
}
}
#Override
public void onNothingSelected(AdapterView<?> arg0)
{
Log.i("AAA","Nothing S1");
}
});
spinner2.setOnItemSelectedListener(new OnItemSelectedListener()
{
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int pos, long id)
{
int loc = pos;
switch (loc)
{
case 1:
if(object.equals(aus))
{
area = aus.getTitle(loc);
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(Activity.this, android.R.layout.simple_spinner_item, area);
adapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
spinner3.setAdapter(adapter); Log.i("","spinner3");
break;
default:
break;
}
}
#Override
public void onNothingSelected(AdapterView<?> arg0)
{
// TODO Auto-generated method stub
}
});
}// on-create
#Override
public void onClick(View v)
{
switch (v.getId())
{
case R.id.btnSubmit:
break;
case R.id.btnCancel:
finish();
break;
default:
break;
}
}
}
If you find this useful, then do give it up vote, so that others can find a good answer easily.
For each Country, you have to create a class for it, to just add State, City & Area. So that it doesn't become a mesh at a single at single page.
Have fun.
Regards,
Haps.
Here is a sample code which depicts the usage of spinner and action performed when spinner item is selected
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;
public class MainActivity extends Activity {
Spinner spin;
String spin_val;
String[] gender = { "Male", "Female" };//array of strings used to populate the spinner
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);//setting layout
spin = (Spinner) findViewById(R.id.spinner_id);//fetching view's id
//Register a callback to be invoked when an item in this AdapterView has been selected
spin.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int position, long id) {
// TODO Auto-generated method stub
spin_val = gender[position];//saving the value selected
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
//setting array adaptors to spinners
//ArrayAdapter is a BaseAdapter that is backed by an array of arbitrary objects
ArrayAdapter<String> spin_adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_item, gender);
// setting adapteers to spinners
spin.setAdapter(spin_adapter);
}
}
To add a list of values to the spinner, you then need to specify a SpinnerAdapter in your Activity, which extends Adapter class.. A spinner adapter allows to define two different views: one that shows the data in the spinner itself and one that shows the data in the drop down list when the spinner is pressed.You can also download & refer to the complete spinner_demo example project for better understanding.
Check the following examples :
https://developer.android.com/guide/tutorials/views/hello-spinner.html
http://www.designerandroid.com/?cat=4