I am trying to make my Spinner1 change visibility on spinner2.My code compiles ok, but app crashes on opening.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner spinner = (Spinner)findViewById(R.id.spinner2);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
public void onNothingSelected(AdapterView<?> arg0) {
}
public void onItemSelected(AdapterView<?> arg0, View arg1,int arg2, long arg3) {
Spinner spinner = (Spinner)findViewById(R.id.spinner2);
Spinner spinner1 = (Spinner)findViewById(R.id.spinner1);
TextView textview1 = (TextView)findViewById(R.id.textView1);
if (spinner.getSelectedItemPosition() == 1) {
textview1.setVisibility(View.VISIBLE);
spinner1.setVisibility(View.VISIBLE);
}
}
});
}
Please dont set ID's during an event; Its simply not good; do like this:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner spinner = (Spinner)findViewById(R.id.spinner2);
Spinner spinner1 = (Spinner)findViewById(R.id.spinner1);
TextView textview1 = (TextView)findViewById(R.id.textView1);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
public void onNothingSelected(AdapterView<?> arg0) {
}
public void onItemSelected(AdapterView<?> arg0, View arg1,int arg2, long arg3) {
if (spinner.getSelectedItemPosition() == 1) {
textview1.setVisibility(View.VISIBLE);
spinner1.setVisibility(View.VISIBLE);
}
}
});}
Ok, so i fixed the initial app crash using an class to check the seleced item
main code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner spinner = (Spinner)findViewById(R.id.spinner2);
spinner.setOnItemSelectedListener(new checkSelectedItem());
}
My class:
public class checkSelectedItem implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
Spinner spinner1 = (Spinner)parent.findViewById(R.id.spinner1);
TextView textview1 = (TextView)parent.findViewById(R.id.textView1);
if (pos == 1) {
textview1.setVisibility(View.VISIBLE);
spinner1.setVisibility(View.VISIBLE);
}
else {
if ( textview1.getVisibility() == View.VISIBLE) {
textview1.setVisibility(View.INVISIBLE);
}
if ( spinner1.getVisibility() == View.VISIBLE) {
spinner1.setVisibility(View.INVISIBLE);
}
}
}
public void onNothingSelected(AdapterView parent) {
}
So the initial crash is fixed, but now when i select the option 1 in my spinner2 the app crashes..
Related
I want to make an app which collect data based on some Edittexts and two spinners. Then, by clicking a button show all String data in a TextView. I know it may be simple but I wonder how to get the selected Item in spinners and show them near the rest of values.
'''
Spinner shift, permit;
EditText TextHere, TrainNumber, SignalPerson, NavganPerson, DriverPerson;
TextView BigText;
Button SaveButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
shift=(Spinner)findViewById(R.id.shift_spinner);
permit=(Spinner)findViewById(R.id.permission_spinner);
TextHere=(EditText)findViewById(R.id.text_here);
TrainNumber=(EditText)findViewById(R.id.train_number);
SignalPerson=(EditText)findViewById(R.id.signal_person);
NavganPerson=(EditText)findViewById(R.id.navgan_person);
DriverPerson=(EditText)findViewById(R.id.driver_person);
SaveButton=(Button) findViewById(R.id.save_btn);
BigText=(TextView)findViewById(R.id.text_text);
ArrayAdapter ShiftSelect = ArrayAdapter.createFromResource(
this,
R.array.shifts,
R.layout.support_simple_spinner_dropdown_item
);
shift.setAdapter(ShiftSelect);
shift.setOnItemSelectedListener(this);
ArrayAdapter PermissionSelect = ArrayAdapter.createFromResource(
this,
R.array.permissions,
R.layout.support_simple_spinner_dropdown_item
);
permit.setAdapter(PermissionSelect);
permit.setOnItemSelectedListener(this);
final String TN = TrainNumber.getText().toString();
final String TH = TextHere.getText().toString();
final String SP = SignalPerson.getText().toString();
final String NP = NavganPerson.getText().toString();
final String DP = DriverPerson.getText().toString();
SaveButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Spinner spinner = (Spinner) parent;
if (spinner.getId()==R.id.shift_spinner){
String SHIFT = parent.getSelectedItem().toString();
}
if (spinner.getId()==R.id.permission_spinner){
String PER = parent.getSelectedItem().toString();
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
'''
I want to pass Item that is selected from public void onItemSelected to onCreate method section to show it in a txetview.
Thanks you.
Try this.
TextView textView;
Spinner spinner;
String SEL_VALUE;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner=(Spinner)findViewById(R.id.shift_spinner);
textView=(TextView)findViewById(R.id.textView);
SEL_VALUE="";
// Create adapter here
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String sel_val = spinner.getSelectedItem().toString();
// Setting selected value to textView.
SEL_VALUE = sel_val;
textView.setText(sel_val);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
I hope this is what you asked for
Spinner shift, permit;
EditText TextHere, TrainNumber, SignalPerson, NavganPerson, DriverPerson;
TextView BigText;
Button SaveButton;
String SHIFT,PER;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
shift=(Spinner)findViewById(R.id.shift_spinner);
permit=(Spinner)findViewById(R.id.permission_spinner);
TextHere=(EditText)findViewById(R.id.text_here);
TrainNumber=(EditText)findViewById(R.id.train_number);
SignalPerson=(EditText)findViewById(R.id.signal_person);
NavganPerson=(EditText)findViewById(R.id.navgan_person);
DriverPerson=(EditText)findViewById(R.id.driver_person);
SaveButton=(Button) findViewById(R.id.save_btn);
BigText=(TextView)findViewById(R.id.text_text);
ArrayAdapter ShiftSelect = ArrayAdapter.createFromResource(
this,
R.array.shifts,
R.layout.support_simple_spinner_dropdown_item
);
shift.setAdapter(ShiftSelect);
shift.setOnItemSelectedListener(this);
ArrayAdapter PermissionSelect = ArrayAdapter.createFromResource(
this,
R.array.permissions,
R.layout.support_simple_spinner_dropdown_item
);
permit.setAdapter(PermissionSelect);
permit.setOnItemSelectedListener(this);
final String TN = TrainNumber.getText().toString();
final String TH = TextHere.getText().toString();
final String SP = SignalPerson.getText().toString();
final String NP = NavganPerson.getText().toString();
final String DP = DriverPerson.getText().toString();
SaveButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
BigText.setText(SHIFT+" "+PER);
}
});
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Spinner spinner = (Spinner) parent;
if (spinner.getId()==R.id.shift_spinner){
SHIFT = parent.getSelectedItem().toString();
}
if (spinner.getId()==R.id.permission_spinner){
PER = parent.getSelectedItem().toString();
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
Try this Out.
TextView textView;
Spinner spinner;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner=(Spinner)findViewById(R.id.shift_spinner);
textView=(TextView)findViewById(R.id.textView);
// Create adapter here
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String sel_val = spinner.getSelectedItem().toString();
// Setting selected value to textView.
textView.setText(sel_val);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
I am trying to display image in image view, based on spinner selection. If anyone know how to solve this please help.
public class activity_tables extends AppCompatActivity {
Spinner s1;
String[] tbls;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tables);
s1 = (Spinner)findViewById(R.id.spinner);
tbls = getResources().getStringArray(R.array.tables);
final ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_spinner_item,tbls);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s1.setAdapter(adapter);
}
}
Set onItemSelectedListener for your spinner like this
s1.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
// your code here
if(position==0)
{
qImageView.setImageResource(R.drawable.whatever);}
else if(position ==1)
{
qImageView.setImageResource(R.drawable.whatever);}
}
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});
I'm trying to build an app that takes 2 inputted redox pairs from 2 spinners to display information about the cell that could be made from these chemicals such as what would be used as the anode/cathode, the reduction potential of each half-reaction and the cell etc.
My issue is I haven't been able to find any way to take the input of both the spinners to be able to make the calculations, only have both spinners do the same thing.
My current code is this:
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener{
Spinner spinner1, spinner2;
ArrayAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
adapter = ArrayAdapter.createFromResource(this, R.array.pairs, android.R.layout.simple_spinner_item);
spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner1.setAdapter(adapter);
spinner1.setOnItemSelectedListener(MainActivity.this);
spinner2 = (Spinner) findViewById(R.id.spinner2);
spinner2.setAdapter(adapter);
spinner2.setOnItemSelectedListener(MainActivity.this);
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String choice1 = parent.getItemAtPosition(position).toString();
double first = 0;
double second = 0;
double anode;
double cathode;
if (choice1 == getResources().getString(R.string.MnO4)){
first = 0.60;
Toast.makeText(parent.getContext(), choice1, Toast.LENGTH_SHORT).show();
} else if (choice1 == getResources().getString(R.string.Cl)) {
first = 1.36;
Toast.makeText(parent.getContext(), choice1, Toast.LENGTH_SHORT).show();
}
if (first > second) {
cathode = first;
anode = second;
} else {
cathode = second;
anode = first;
}
double cellValue = anode - cathode;
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
I'm very new to android studio so any help is greatly appreciated.
Thanks
I suggest that you use anonymous classes to handle the onItemSelected. That way you can handle the selection of each Spinner independently.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
adapter = ArrayAdapter.createFromResource(this, R.array.pairs, android.R.layout.simple_spinner_item);
spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner1.setAdapter(adapter);
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// Do what you need to do with spinner1 info
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
spinner2 = (Spinner) findViewById(R.id.spinner2);
spinner2.setAdapter(adapter);
spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// Do what you need to do with spinner2 info
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
Now you can remove the Override method:
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
and you can remove implements :
implements AdapterView.OnItemSelectedListener
I'm trying to get the position (number) of the spinner when selected to use it in another Activity that will display a different map each time depending on the item selected. when I run the application it crashes. this is the first Activity code:
public class TestProjectActivity extends Activity {
public Spinner spinner1;
public Integer number;
private Button valideButton;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MySpinner();
valide_button();
}
public void MySpinner() {
final Spinner spinner1 = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.num, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(adapter);
spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parentView,
View selectedItemView, int position, long id) {
// Object item = parentView.getItemAtPosition(position);
TestProjectActivity.this.number = spinner1
.getSelectedItemPosition() + 1;
}
public void onNothingSelected(AdapterView<?> arg0) {// do nothing
}
});
}
public void valide_button() {
valideButton = (Button) findViewById(R.id.valide_button);
valideButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(TestProjectActivity.this,
MetroMapActivity.class);
startActivity(intent);
}
});
}
}
The way to get the selection of the spinner is:
spinner1.getSelectedItemPosition();
Documentation reference:
http://developer.android.com/reference/android/widget/AdapterView.html#getSelectedItemPosition()
However, in your code, the one place you are referencing it is within your setOnItemSelectedListener(). It is not necessary to poll the spinner, because the onItemSelected method gets passed the position as the "position" variable.
So you could change that line to:
TestProjectActivity.this.number = position + 1;
If that does not fix the problem, please post the error message generated when your app crashes.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt = findViewById(R.id.button);
spinner = findViewById(R.id.sp_item);
setInfo();
spinnerAdapter = new SpinnerAdapter(this, arrayList);
spinner.setAdapter(spinnerAdapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//first, we have to retrieve the item position as a string
// then, we can change string value into integer
String item_position = String.valueOf(position);
int positonInt = Integer.valueOf(item_position);
Toast.makeText(MainActivity.this, "value is "+ positonInt, Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
note: the position of items is counted from 0.
final int[] positions=new int[2];
Spinner sp=findViewByID(R.id.spinner);
sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
Toast.makeText( arg2....);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
if (position ==0) {
if (rYes.isChecked()) {
Toast.makeText(SportActivity.this, "yes ur answer is right", Toast.LENGTH_LONG).show();
} else if (rNo.isChecked()) {
Toast.makeText(SportActivity.this, "no.ur answer is wrong", Toast.LENGTH_LONG).show();
}
}
This code is supposed to select both check boxes.
Is there a problem with it?
I'm trying to get the position (number) of the spinner when selected to use it in another Activity that will display a different map each time depending on the item selected. when I run the application it crashes. this is the first Activity code:
public class TestProjectActivity extends Activity {
public Spinner spinner1;
public Integer number;
private Button valideButton;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MySpinner();
valide_button();
}
public void MySpinner() {
final Spinner spinner1 = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.num, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(adapter);
spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parentView,
View selectedItemView, int position, long id) {
// Object item = parentView.getItemAtPosition(position);
TestProjectActivity.this.number = spinner1
.getSelectedItemPosition() + 1;
}
public void onNothingSelected(AdapterView<?> arg0) {// do nothing
}
});
}
public void valide_button() {
valideButton = (Button) findViewById(R.id.valide_button);
valideButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(TestProjectActivity.this,
MetroMapActivity.class);
startActivity(intent);
}
});
}
}
The way to get the selection of the spinner is:
spinner1.getSelectedItemPosition();
Documentation reference:
http://developer.android.com/reference/android/widget/AdapterView.html#getSelectedItemPosition()
However, in your code, the one place you are referencing it is within your setOnItemSelectedListener(). It is not necessary to poll the spinner, because the onItemSelected method gets passed the position as the "position" variable.
So you could change that line to:
TestProjectActivity.this.number = position + 1;
If that does not fix the problem, please post the error message generated when your app crashes.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt = findViewById(R.id.button);
spinner = findViewById(R.id.sp_item);
setInfo();
spinnerAdapter = new SpinnerAdapter(this, arrayList);
spinner.setAdapter(spinnerAdapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//first, we have to retrieve the item position as a string
// then, we can change string value into integer
String item_position = String.valueOf(position);
int positonInt = Integer.valueOf(item_position);
Toast.makeText(MainActivity.this, "value is "+ positonInt, Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
note: the position of items is counted from 0.
final int[] positions=new int[2];
Spinner sp=findViewByID(R.id.spinner);
sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
Toast.makeText( arg2....);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
if (position ==0) {
if (rYes.isChecked()) {
Toast.makeText(SportActivity.this, "yes ur answer is right", Toast.LENGTH_LONG).show();
} else if (rNo.isChecked()) {
Toast.makeText(SportActivity.this, "no.ur answer is wrong", Toast.LENGTH_LONG).show();
}
}
This code is supposed to select both check boxes.
Is there a problem with it?