Spinner dropdown list doesn't show - android

http://i.stack.imgur.com/HGsTw.jpg
This is the error I get and the app just crashes and shows nothing!.
PS:When I moved those last 4 lines of code from the OnCreate method to the beginning of the OnItemSelected method I get no errors and this layout (http://i.stack.imgur.com/1DiLI.jpg) shows but no dropdown lists (I expect a list of colors when I click the arrow button next to background).
Here is my code:
public class SetLayout extends Activity implements OnItemSelectedListener {
TextView o_markertv, x_markertv, bcktv, gridtv;
String[] colors ={"Red", "Green", "Blue", "Black", "White"};
Spinner spinner1, spinner2, spinner3, spinner4;
LinearLayout bck;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.setlayout);
bck= (LinearLayout) findViewById(R.id.bck);
o_markertv= (TextView) findViewById(R.id.o_markertv);
x_markertv= (TextView) findViewById(R.id.x_markertv);
bcktv= (TextView) findViewById(R.id.bcktv);
gridtv= (TextView) findViewById(R.id.gridtv);
spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner2 = (Spinner) findViewById(R.id.spinner2);
spinner3 = (Spinner) findViewById(R.id.spinner3);
spinner4 = (Spinner) findViewById(R.id.spinner4);
//Button BtnSubmit = (Button) findViewById(R.id.btnSubmit);
ArrayAdapter<String> adapter=new ArrayAdapter<String> (SetLayout.this,android.R.layout.simple_spinner_item,colors);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(adapter);
spinner1.setOnItemSelectedListener(this);
}
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
int position= spinner1.getSelectedItemPosition();
switch(position){ //red, green blue black white
case 0:
bck.setBackgroundColor(color.red);
break;
case 1:
bck.setBackgroundColor(color.green);
break;
case 2:
bck.setBackgroundColor(color.blue);
break;
case 3:
bck.setBackgroundColor(color.black);
break;
case 4:
bck.setBackgroundColor(color.white);
break;
}
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}

Related

Opening different layouts based on values selected in two spinners

I am new to android,and in this code i have two spinners and based on the values selected,different layouts should open accordingly. Im stuck with what logic should i use to open different layouts, inside the OnItemSelected for the spinner.Say i have 5 items in spinner and i have 5 different layouts for each item.So how should i do??
Any help or ideas would be much appreciated. Thanks in advance.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout);
// Spinner element
Spinner spinner1 = (Spinner) findViewById(R.id.spinner1);
Spinner spinner2 = (Spinner) findViewById(R.id.spinner2);
Button button = (Button) findViewById(R.id.button);
// Spinner click listener
// Spinner Drop down elements
List<String> categories = new ArrayList<String>();
categories.add("Maruti");
categories.add("Ford");
categories.add("Nissan");
categories.add("TATA");
List<String> types = new ArrayList<String>();
types.add("1Lakh-5Lakh");
types.add("5Lakh-10Lakh");
types.add("10Lakh-20Lakh");
types.add("20Lakh-50Lakh");
types.add("Above 50Lakhs");
// Creating adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories);
// Drop down layout style - list view with radio button
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
ArrayAdapter<String> spinner2Adapter=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,types);
spinner2Adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// attaching data adapter to spinner
spinner1.setAdapter(dataAdapter);
spinner2.setAdapter(spinner2Adapter);
spinner1.setOnItemSelectedListener(this);
spinner2.setOnItemSelectedListener(this);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i=new Intent(SecondActivity.this,ThirdActivity.class);
startActivity(i);
}
});
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Spinner spinner1 = (Spinner)parent;
Spinner spinner2 = (Spinner)parent;
if(spinner1.equals(position))
{
}
if(spinner2.getId() == R.id.spinner2)
{
}
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}}
Try below.
check the onClickListener of button which will decide which activity to open.
Spinner spinner1;
Spinner spinner2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout);
// Spinner element
spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner2 = (Spinner) findViewById(R.id.spinner2);
Button button = (Button) findViewById(R.id.button);
// Spinner click listener
// Spinner Drop down elements
List<String> categories = new ArrayList<String>();
categories.add("Maruti");
categories.add("Ford");
categories.add("Nissan");
categories.add("TATA");
List<String> types = new ArrayList<String>();
types.add("1Lakh-5Lakh");
types.add("5Lakh-10Lakh");
types.add("10Lakh-20Lakh");
types.add("20Lakh-50Lakh");
types.add("Above 50Lakhs");
// Creating adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories);
// Drop down layout style - list view with radio button
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
ArrayAdapter<String> spinner2Adapter=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,types);
spinner2Adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// attaching data adapter to spinner
spinner1.setAdapter(dataAdapter);
spinner2.setAdapter(spinner2Adapter);
spinner1.setOnItemSelectedListener(this);
spinner2.setOnItemSelectedListener(this);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i;
if(spinner1_selected_item.equals("Maruti"))
{
if(spinner2_selected_item.equals("1Lakh-5Lakh"))
i=new Intent(SecondActivity.this,ThirdActivity.class);
if(spinner2_selected_item.equals("5Lakh-10Lakh"))
i=new Intent(SecondActivity.this,FourthActivity.class);
if(spinner2_selected_item.equals("10Lakh-20Lakh"))
i=new Intent(SecondActivity.this,FifthActivity.class);
}
startActivity(i);
}
});
}
String spinner1_selected_item;
String spinner2_selected_item;
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if(spinner1.getId() == R.id.spinner1)
{
spinner1_selected_item = parent.getItemAtPosition(position).toString();
}
if(spinner2.getId() == R.id.spinner2)
{
spinner2_selected_item = parent.getItemAtPosition(position).toString();
}
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}}

Changing textview using 2 spinners

I have 2 spinners and 1 textview.When i selected two different elements from these spinners textview must be changed everytime.For 1 spinner everything is OK but it doesnt work on 2 spinners.
Here's what i tried:
public class Chords extends Activity implements AdapterView.OnItemSelectedListener {
String[] notes = {"A", "B", "C", "D", "E", "F", "G"};
String[] mode = {"m","m7","5","M","7M","dim","aug","sus2"};
Spinner sp;
Spinner spinner2;
TextView textView;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.chords_screen);
System.out.println(notes.length);
textView = (TextView) findViewById(R.id.textView);
sp= (Spinner) findViewById(R.id.spinner);
spinner2=(Spinner) findViewById(R.id.spinner2);
ArrayAdapter<String> adapter_state = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item,notes);
ArrayAdapter<String> adapter_state2 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item,mode);
adapter_state.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
adapter_state2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp.setAdapter(adapter_state);
sp.setOnItemSelectedListener(this);
spinner2.setAdapter(adapter_state2);
sp.setOnItemSelectedListener(this);
}
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
switch(position)
{
case 0:
textView.setText("Am");
break;
}
}
#Override
public void onNothingSelected (AdapterView < ? > parent){
}
}
You have 2 spinners. You set onItemSelected listener. but your implementation is wrong i think. In switch case you have to identify which spinner value is changing. you can do like this ..
I edited all code for you
public class MainActivity extends ActionBarActivity implements OnItemSelectedListener {
String[] notes = {"A", "B", "C", "D", "E", "F", "G"};
String[] mode = {"m","m7","5","M","7M","dim","aug","sus2"};
Spinner sp;
Spinner spinner2;
TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//initialization
textView = (TextView) findViewById(R.id.textView);
sp = (Spinner) findViewById(R.id.spinner);
spinner2=(Spinner) findViewById(R.id.spinner2);
//creating adapter
ArrayAdapter<String> adapter_state = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,notes);
ArrayAdapter<String> adapter_state2 = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,mode);
//setting drop down list type
adapter_state.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
adapter_state2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//setting adapter to spinner
sp.setAdapter(adapter_state);
spinner2.setAdapter(adapter_state2);
//setting listener to spinner
sp.setOnItemSelectedListener(this);
spinner2.setOnItemSelectedListener(this);
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position,long id) {
// TODO Auto-generated method stub
switch(parent.getId())
{
case R.id.spinner:
textView.setText(""+notes[position]);
break;
case R.id.spinner2:
textView.setText(""+mode[position]);
break;
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
To do so edit like this
case R.id.spinner:
textView.setText(sp.getSelectedItem().toString()+spinner2.getSelectedItem().toString());
break;
case R.id.spinner2:
textView.setText(sp.getSelectedItem().toString()+spinner2.getSelectedItem().toString());
break;

Change content of spinner dynamically to another spinner's item

i have too spinners , spinner and spinner2, first one has a standard menu, the second must change relative to items from spinner.
am trying to get the position of item selected in spinner and then put it as a parametre into my method item2sp to change spinner's 2 items.
but i doesn't change, i keep getting only the first item menu in spinner2, how can i fix it help me please.
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
Spinner spinner2;
List<String> categories;
List<String> ndp;
List<String> ps;
ArrayAdapter<String> dataAdapter;
ArrayAdapter<String> ndpAdapter;
ArrayAdapter<String> psAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Spinner element
Spinner spinner = (Spinner) findViewById(R.id.spinner1);
Spinner spinner2 = (Spinner) findViewById(R.id.spinner2);
// Spinner click listener
spinner.setOnItemSelectedListener(this);
spinner2.setOnItemSelectedListener(this);
// Spinner Drop down elements
categories = new ArrayList<String>();
categories.add("NDP");
categories.add("PS");
categories.add("PA");
categories.add("PC");
categories.add("PH");
ps = new ArrayList<String>();
ps.add("AA");
ps.add("BB");
ps.add("CC");
ndp = new ArrayList<String>();
ndp.add("RP");
ndp.add("AFF");
ndp.add("RPA");
// Creating adapter for spinner
dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories);
psAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, ps);
ndpAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, ndp);
// Drop down layout style - list view with radio button
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
psAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
ndpAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// attaching data adapter to spinner
spinner.setAdapter(dataAdapter);
}
public void itemsp2 (int position){
switch (position){
case 0:
spinner2.setAdapter(ndpAdapter);
Toast.makeText(getBaseContext(), ""+position, Toast.LENGTH_LONG).show();
break;
case 1:
spinner2.setAdapter(psAdapter);
Toast.makeText(getBaseContext(), ""+position, Toast.LENGTH_LONG).show();
break;
}
}
// int position = spinner.getSelectedItemPosition();
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// On selecting a spinner item
String item = parent.getItemAtPosition(position).toString();
itemsp2(position);
// Showing selected spinner item
// Toast.makeText(parent.getContext(), "Selected: " + item, Toast.LENGTH_LONG).show();
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
iller is my First List and I set this list my firs spinner then I implements onItemSelected method for this spinner. In this method you can add your second spinner with difrent List.
Spinner spinner = (Spinner) findViewById(R.id.spinner);
spinner.setOnItemSelectedListener(this);
List<String> iller = new ArrayList<String>();
iller.add("İl Seçiniz..");
iller.add("Antalya");
iller.add("Burdur");
iller.add("Bursa");
iller.add("Balıkesir");
iller.add("Giresun");
iller.add("İstanbul");
ArrayAdapter<String> illerAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, iller);
illerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(illerAdapter);
spinner2 = (Spinner) findViewById(R.id.spinner2);
spinner2.setVisibility(View.INVISIBLE);
editText = (EditText) findViewById(R.id.editText);
editText.setVisibility(View.INVISIBLE);
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
int itemId = (int) id;
if (itemId == 1) {
iller.remove("İl Seçiniz..");
antalyaİlceler = new ArrayList<String>();
antalyaİlceler.add("İlçe Seçiniz..");
antalyaİlceler.add("Konyaaltı");
antalyaİlceler.add("Muratpaşa");
antalyaİlceler.add("Alanya");
antalyaİlceler.add("Manavgat");
ArrayAdapter<String> antalyaİlcelerAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, antalyaİlceler);
antalyaİlcelerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(antalyaİlcelerAdapter);
spinner2.setVisibility(View.VISIBLE);
editText.setVisibility(View.VISIBLE);
} else if (itemId == 2) {
burdurİlceler = new ArrayList<String>();
burdurİlceler.add("İlçe Seçiniz..");
burdurİlceler.add("Bucak");
burdurİlceler.add("Tefenni");
burdurİlceler.add("Gölhisar");
burdurİlceler.add("Ağlasun");
ArrayAdapter<String> burdurİlcelerAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, burdurİlceler);
burdurİlcelerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(burdurİlcelerAdapter);
spinner2.setVisibility(View.VISIBLE);
editText.setVisibility(View.VISIBLE);
}

have spinner update without button click

So i have made a stat calculator app for a game where the user selects a combination through various spinners and then clicks a button to calculate the resulting stats. So now i want to remove the button entirely and have the resulting stats update automatically when the item in the spinner changes. Here is my spinner listener code:
public class MySpinnerListener implements OnItemSelectedListener {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
int identifier = arg0.getId();
String id = arg0.getItemAtPosition(arg2).toString();
id=id.replaceAll("\\s+","");
Data data= Data.valueOf(id);
switch (data) {
//big case switch to find what is clicked an update the variables accordingly..
And here is my calculate function
public void Calculate() {
calculate = (Button) findViewById(R.id.calculate);
calculate.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
//creates object with all the stats (there are two because the user can compare different combinations) ChangeColor just makes it look nice.
TotalStats stats1 = new TotalStats(player, car, wheel, glide);
TotalStats stats2 = new TotalStats(player2, car2, wheel2,
glide2);
ChangeColor leftStats= new ChangeColor(stats1);
ChangeColor rightStats= new ChangeColor(stats2);
//outputs the stats
statsView1.setText(leftStats.builder, BufferType.SPANNABLE);
statsView2.setText(rightStats.builder,BufferType.SPANNABLE);
}
});
EDIT: Here is my onCreate method and my initial setup.
Spinner character, character1;
Spinner vehicle, vehicle1;
Spinner wheels, wheels1;
Spinner glider, glider1;
Button calculate;
TextView statsView1, statsView2, statsView, title;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.fragment_main);
statsView = (TextView) findViewById(R.id.statsView);
statsView1 = (TextView) findViewById(R.id.stats1);
statsView2 = (TextView) findViewById(R.id.stats2);
title=(TextView)findViewById(R.id.title);
Typeface font = Typeface.createFromAsset(getAssets(),"New Super Mario Font U.ttf");
statsView.setTypeface(font);
statsView1.setTypeface(font);
statsView2.setTypeface(font);
title.setTypeface(font);
InitialSetupUI();
Calculate();
}
private void InitialSetupUI() {
// TODO Auto-generated method stub
character = (Spinner) findViewById(R.id.character);
vehicle = (Spinner) findViewById(R.id.vehicle);
wheels = (Spinner) findViewById(R.id.wheels);
glider = (Spinner) findViewById(R.id.glider);
character1 = (Spinner) findViewById(R.id.character2);
vehicle1 = (Spinner) findViewById(R.id.vehicle2);
wheels1 = (Spinner) findViewById(R.id.wheels2);
glider1 = (Spinner) findViewById(R.id.glider2);
calculate = (Button) findViewById(R.id.calculate);
ArrayAdapter cadapter = ArrayAdapter.createFromResource(this,
R.array.character_array, R.layout.spinner_item);
ArrayAdapter vadapter = ArrayAdapter.createFromResource(this,
R.array.vehicle_array, R.layout.spinner_item);
ArrayAdapter wadapter = ArrayAdapter.createFromResource(this,
R.array.wheels_array, R.layout.spinner_item);
ArrayAdapter gadapter = ArrayAdapter.createFromResource(this,
R.array.glider_array, R.layout.spinner_item);
character.setAdapter(cadapter);
vehicle.setAdapter(vadapter);
wheels.setAdapter(wadapter);
glider.setAdapter(gadapter);
character1.setAdapter(cadapter);
vehicle1.setAdapter(vadapter);
wheels1.setAdapter(wadapter);
glider1.setAdapter(gadapter);
vehicle.setOnItemSelectedListener(new MySpinnerListener());
character.setOnItemSelectedListener(new MySpinnerListener());
wheels.setOnItemSelectedListener(new MySpinnerListener());
glider.setOnItemSelectedListener(new MySpinnerListener());
vehicle1.setOnItemSelectedListener(new MySpinnerListener());
character1.setOnItemSelectedListener(new MySpinnerListener());
wheels1.setOnItemSelectedListener(new MySpinnerListener());
glider1.setOnItemSelectedListener(new MySpinnerListener());
}

how to add items in edit text when i click on spinner?

In my application i have used spinner and edit text.i have created an XML file and set it as background to the spinner which looks like a drop down arrow.When i click the spinner the items selected from spinner should be set in edit text but it displays in edit text as well spinner as the below image. can any one help me with this??
public class newcard extends Activity {
Spinner spinner;
Button btn;
EditText ed,ed1,ed2;
List<String> list;
private String[] countries_list={"01/2014","02/2014","03/2014","04/2014"};
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.addnewcard);
btn= (Button) findViewById(R.id.button1);
spinner = (Spinner) findViewById(R.id.spinner2);
ed = (EditText) findViewById(R.id.editText3);
spinner.setFocusable(true);
spinner.setFocusableInTouchMode(true);
list = new ArrayList<String>();
list.add(" ");
list.add("select");
list.add("01/2014");
list.add("02/2014");
list.add("03/2014");
list.add("04/2014");
ArrayAdapter<String> adp = new ArrayAdapter<String>
(this, android.R.layout.simple_dropdown_item_1line, list);
adp.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
spinner.setAdapter(adp);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
//#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,int arg2, long arg3) {
// TODO Auto-generated method stub
switch(arg2) {
case 0 :
ed.setText("Select");
break;
case 1 :
ed.setText("01/2014");
break;
case 2 :
ed.setText("02/2014");
break;
case 3 :
ed.setText("04/2014");
break;
default :
ed.setText("Nothing");
break;
}
}
//#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
For setting spinner value to ""
public void onItemSelected(AdapterView<?> parent, View arg1, int arg2, long arg3)
{
item = (String) parent.getItemAtPosition(arg2);
((TextView) parent.getChildAt(0)).setText("");
}

Categories

Resources