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
}
});
Related
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 want to set text from selected item from spinner to textview, But when i tried to do application craches. Can any one help me .
public class MainActivity extends AppCompatActivity {
TextView txt;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt=(TextView) findViewById(R.id.txt1) ;
ArrayList<ItemData> list=new ArrayList<>();
list.add(new ItemData("Cash",R.drawable.paymentcashnew));
list.add(new ItemData("Paytm",R.drawable.paymentpaytmnew));
Spinner sp=(Spinner)findViewById(R.id.select_cash);
String result = sp.getSelectedItem().toString();
txt.setText(result);
SpinnerAdapter adapter=new SpinnerAdapter(this,
R.layout.spinner,R.id.txt,list);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp.setAdapter(adapter);
}
}
sp.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
String result = sp.getSelectedItem().toString();
txt.setText(result);
}
});
This may help Frnd.
spinner has no "getSelectedItem". https://developer.android.com/reference/android/widget/Spinner.html https://developer.android.com/guide/topics/ui/controls/spinner.html
and here's how you get the text-value of the selected row: https://stackoverflow.com/a/15545909/3022387
spin.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
String imc_met=spin.getSelectedItem().toString();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
Implement spinner.setOnItemClickListener and then find text inside that method like this.
spinner.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
String result = sp.getSelectedItem().toString();
txt.setText(result);
}
});
You can't get selected Item before setting adapter.Try this below code
public class MainActivity extends AppCompatActivity {
final TextView txt;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt=(TextView) findViewById(R.id.txt1) ;
ArrayList<ItemData> list=new ArrayList<>();
list.add(new ItemData("Cash",R.drawable.paymentcashnew));
list.add(new ItemData("Paytm",R.drawable.paymentpaytmnew));
Spinner sp=(Spinner)findViewById(R.id.select_cash);
SpinnerAdapter adapter=new SpinnerAdapter(this,
R.layout.spinner,R.id.txt,list);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp.setAdapter(adapter);
sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Object item = parent.getItemAtPosition(position);
String result = sp.getSelectedItem().toString();
txt.setText(result);
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}
I have a spinner with ten numbers 0-10 and I if someone picks a number I want him not to be able to pick the same value again. So inside ItemSelected I do the following with no result
#Override
public void onItemSelected(AdapterView<?> adapter, View v, int position, long id) {
// On selecting a spinner item
if (position > 0 && position!=tempPosition)
{
TeamSpinnerNumber = adapter.getItemAtPosition(position).toString();
tempPosition = position
}
}
Check this i worked out for you may it help you.
public class MainActivity extends AppCompatActivity {
ArrayList<String> mStrings;
ArrayAdapter<String> mStringArrayAdapter;
Spinner mSpinner;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSpinner = (Spinner) findViewById(R.id.spinner);
mStrings = new ArrayList<String>() {{
add("None");
add("one");
add("two");
add("three");
}};
mStringArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mStrings);
mSpinner.setAdapter(mStringArrayAdapter);
mSpinner.setSelection(0);
mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String string = parent.getItemAtPosition(position).toString();
if (!string.equals("None"))
removeThisFromSpinner(string);
mSpinner.setSelection(0);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
private void removeThisFromSpinner(String s) {
mStrings.remove(s);
mStringArrayAdapter.notifyDataSetChanged();
}
}
try this, hope it helps :
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Log.i(TAG, "onItemSelected: "+position);
//checking the position of the selected spinner item, if the previous position is not the same then do something here
if(position>0 && position!=myPosition){
}
myPosition = position;
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
and don't forget to add global variable to the class :
int myPosition = 999; // the non existed position make it global variable
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..
I have to create a Spinner which is not in my Layout.
so I want to create it Programmatic I write the following code in My Activity class
Spinner spinner = new Spinner(this);
String option[]={"By Date","By Transaction ID","By Customer Phone"};
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,option);
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item );
spinner.setAdapter(spinnerArrayAdapter);
spinner.performClick();
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentview, View v,int position, long id)
{
Log.d("in select","yes");
}
#Override
public void onNothingSelected(AdapterView<?> arg0)
{
Toast.makeText(MainMenu.this,"no",Toast.LENGTH_LONG).show();
Log.d("In No select","No");
}
});
so My question is how I can access the spinner selected item in the activity
Your only way to access it outside your listener is making a variable that will hold the position, so you would use it later.
Something like this:
public class YourActivity extends Activity {
private int curPos;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Spinner spinner = new Spinner(this);
String option[]={"By Date","By Transaction ID","By Customer Phone"};
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,option);
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item );
spinner.setAdapter(spinnerArrayAdapter);
spinner.performClick();
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentview, View v,int position, long id)
{
curPos = position;
Log.d("in select","yes");
}
#Override
public void onNothingSelected(AdapterView<?> arg0)
{
Toast.makeText(MainMenu.this,"no",Toast.LENGTH_LONG).show();
Log.d("In No select","No");
}
});
}
In your onItemSelected:
#Override
public void onItemSelected(AdapterView<?> parentview, View v,int position, long id)
{
Log.d("in select","yes");
}
The position helps you to retrieve your selected item. Which, in your case, it should be:
option[position]