The spinner has an array list of band genres when selecting any of the genres in the spinner it will display a toast, the toast isn't showing when clicking the spinner, the code isn't showing any errors ? any ideas why ?
String spinnerSelection = null;
Spinner spinner = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_main );
ArrayAdapter<String> adapter = new ArrayAdapter<>(
this, android.R.layout.simple_spinner_item, spinnerContent);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner = (Spinner) findViewById(R.id.spinner);
spinner.setAdapter(adapter);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
public void onItemSelected(AdapterView<?>parent, View v, int position, long id) {
{
Toast.makeText( getBaseContext(),"Hell Yeah", Toast.LENGTH_SHORT).show();
}
}
public void onNothingSelected(AdapterView<?> arg0) {
}
}
Don't forget to add click listener in the spinner
spinner.setOnItemClickListener(this);
You are adding setOnClickListener() on your button. Add setOnItemClickListener() to your spinner.
You need to put #Override for onItemSelected(...) and onNothingSelected(...) functions.
Make sure you set a listener on the spinner:
spinner.setOnItemClickListener(this);
Edit
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
MediaPlayer myMusic;
int paused;
public void play(View view) {
if (myMusic == null) {
myMusic = MediaPlayer.create( this, R.raw.fast_and_cold );
myMusic.start();
}
}
public void stop(View view) {
myMusic.reset();
myMusic = null;
}
String spinnerSelection = null;
Spinner spinner = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_main );
final List<String> spinnerContent = new ArrayList<>();
spinnerContent.add("Black Metal");
spinnerContent.add("Death Metal");
spinnerContent.add("Thrash Metal");
spinnerContent.add("Heavy Metal");
ArrayAdapter<String> adapter = new ArrayAdapter<>(
this, android.R.layout.simple_spinner_item, spinnerContent);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner = (Spinner) findViewById(R.id.spinner);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this); // add this line
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
}
#Override // add this line
public void onItemSelected(AdapterView<?>parent, View v, int position, long id) {
Toast.makeText( getBaseContext(),"Show your Devil Horns !!", Toast.LENGTH_SHORT).show();
}
#Override // add this line
public void onNothingSelected(AdapterView<?> arg0) {
}
}
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 have two spinners, the first one shows the transport lines and the second one shows the stations of the line selected on the first spinner, the problem is that I want to set to empty value the second spinner when I click on the first one, but I can't do it.
private ArrayAdapter<String> arrayAdapter;
private MaterialBetterSpinner spinner;
private ArrayAdapter<String> arrayAdapter2;
private MaterialBetterSpinner spinner2;
I've tried a few options on the onItemClick() method of the first spinner but it's not working propertly:
spinner.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
#Override
protected void onPreExecute() {
// I'm trying all the options in this method
}
#Override
protected String[] doInBackground(String... strings) {
}
#Override
protected void onPostExecute(String[] result) {
}
}
});
Option 1:
arrayAdapter2.clear();
arrayAdapter2.notifyDataSetChanged();
Option 2:
spinner2.setAdapter(null);
Option 3:
ArrayList<String> list = new ArrayList<>();
list.add("");
spinner2.setSelection(list.size()-1);
Option 4:
arrayAdapter2.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item);
spinner2.setAdapter(arrayAdapter2);
spinner2.setSelection(arrayAdapter2.getCount());
spinner2.setOnItemSelectedListener(this);
arrayAdapter2.notifyDataSetChanged();
To sum up, there is a way to change the value of a spinner in function of the value of other one? Or just a way to change the value of a spinner to empty value?
Any help will be apreciated!
Use setOnItemSelectedListener instead of OnItemClickListener like:
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
{
if(pos==0) // you can change condition as per your requirement
{
arrayAdapter2.clear();
arrayAdapter2.notifyDataSetChanged();
}
}
public void onNothingSelected(AdapterView<?> parent)
{
}
});
The complete code:
public class FillTransportPlaceActivity extends AppCompatActivity {
private ArrayAdapter<String> arrayAdapter;
private MaterialBetterSpinner spinner;
private ArrayAdapter<String> arrayAdapter2;
private MaterialBetterSpinner spinner2;
private ArrayList<String> list = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fill_transport_place);
arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
spinner = findViewById(R.id.listLines);
spinner.setAdapter(arrayAdapter);
arrayAdapter2 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
spinner2 = findViewById(R.id.listStations);
spinner2.setAdapter(arrayAdapter2);
SharedPreferences sp = getApplicationContext().getSharedPreferences("transportButton", 0);
boolean metro = sp.getBoolean("metro", false);
boolean bus = sp.getBoolean("bus", false);
spinner.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if(parent.getItemAtPosition(position).toString() != null) {
String choice = parent.getItemAtPosition(position).toString();
if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.HONEYCOMB)
new stationsDB().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, choice);
else
new stationsDB().execute(choice);
}
}
});
}
private class stationsDB extends AsyncTask<String, String[], String[]> {
#Override
protected String[] doInBackground(String... strings) {
return DB_transportPlace.getStations(strings[0]);
}
#Override
protected void onPostExecute(String[] result) {
updateAdapter2(result);
}
}
public void updateAdapter(String[] result) {
arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, result);
spinner.setAdapter(arrayAdapter);
}
public void updateAdapter2(String[] result) {
arrayAdapter2 = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, result);
spinner2.setAdapter(arrayAdapter2);
}
As I said I've tried with setOnItemSelectedListener instead of OnItemClickListener and it didn't work, only execute one time in the onCreate method, I just want to change the value of spinner2 to empty when I change the value of spinner1.
i'm practising how to add an string to the addapter of the spinner. If i declare the string in the java activity, works perfectly, but if i do in the string.xml:
<string-array name="tabs">
<item>tab</item>
<item>tab1</item>
<item>tab2</item>
<item>tab3</item>
<item>tab4</item>
<item>tab5</item>
</string-array>
and in the java
ArrayAdapter<String> adapterr1 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, R.array.tabs);
dont work.
what's the problem? Thanks
This is the full code, because i have problems whit the app (crash)
public class MainActivity extends AppCompatActivity {
Spinner OptionSpinner;
TextView textview;
String[] stabs= getResources().getStringArray(R.array.tabs);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
OptionSpinner = (Spinner) findViewById(R.id.OptionSpinner);
textview = (TextView) findViewById(R.id.textview);
ArrayAdapter adapter1 = new ArrayAdapter(this, android.R.layout.simple_spinner_item, stabs);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
OptionSpinner.setAdapter(adapter1);
OptionSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
textview.setText("seleccionado: " + parent.getItemAtPosition(position));
if(position==1){
Intent IntentActT1 = new Intent(MainActivity.this, ActTab1.class);
startActivity(IntentActT1);
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}
String[] stabs= getResources().getStringArray(R.array.tabs);
ArrayAdapter adapterr1 = new ArrayAdapter(this, android.R.layout.simple_spinner_item, stabs);
Edited 1
public class MainActivity extends AppCompatActivity {
Spinner OptionSpinner;
TextView textview;
String[] stabs;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
OptionSpinner = (Spinner) findViewById(R.id.OptionSpinner);
textview = (TextView) findViewById(R.id.textview);
stabs= getResources().getStringArray(R.array.tabs);
ArrayAdapter adapter1 = new ArrayAdapter(this, android.R.layout.simple_spinner_item, stabs);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
OptionSpinner.setAdapter(adapter1);
OptionSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
textview.setText("seleccionado: " + parent.getItemAtPosition(position));
if(position==1){
Intent IntentActT1 = new Intent(MainActivity.this, ActTab1.class);
startActivity(IntentActT1);
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}
The constructor ArrayAdapter<String>(Context, int, int) doesn't use the third argument as an array resource id; it uses it as the id of the TextView to use to display each item in the layout. Instead, you can pass getStringArray(R.array.tabs) as the third argument (or getContext().getStringArray(R.array.tabs), if this code is not in an Activity).
hello I try to put in my java Code a Spinner, but it shows in the else if statement error:
private EditText textName;
private EditText textContent;
private Spinner CategorySpinner;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_text);
configureButton1();
configureButton2();
textName = (EditText) findViewById(R.id.editTextName);
textContent = (EditText) findViewById(R.id.editContent);
CategorySpinner = (Spinner) findViewById(R.id.editCategorySpinner);
}
private void configureButton1() {
Button del = (Button)findViewById(R.id.btNewText);
del.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent(v.getContext(), StartActivity.class);
v.getContext().startActivity(myIntent);
}
});
}
private void configureButton2() {
Button save = (Button) findViewById(R.id.btSave);
save.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (textName.getText().length() == 0) {
textName.requestFocus();
return;
} else if (textContent.getText().length() == 0) {
textContent.requestFocus();
return;
} else if (CategorySpinner.**getText**().length() == 0) {
CategorySpinner.requestFocus();
return;
} else {
//next Step: get the mood
Intent myIntent = new Intent(v.getContext(), Activity2.class);
myIntent.putExtra("textName", textName.getText().toString());
myIntent.putExtra("textContent", textContent.getText().toString());
myIntent.putExtra("CategorySpinner", CategorySpinner.**getText()**.toString());
v.getContext().startActivity(myIntent);
}
}
}
});
}
it shows in the CategorySpinner.getText() an error. I hope you can help me.
I´ve tried a lot, but not found a solution.
private void initSpinnerList() {
spinner = (Spinner) findViewById(R.id.spinner);
spinner.setOnItemSelectedListener(listner);
categories = new ArrayList<String>();
categories.add("S");
categories.add("T");
categories.add("U");
categories.add("V");
categories.add("W");
categories.add("X");
categories.add("Y");
categories.add("Z");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, categories);
dataAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);
}
private OnItemSelectedListener listner = new OnItemSelectedListener() {
/*
* (non-Javadoc)
*
* #see
* android.widget.AdapterView.OnItemSelectedListener#onItemSelected(
* android .widget.AdapterView, android.view.View, int, long)
*/
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
String item = parent.getItemAtPosition(position).toString();
/// on item selected data
}
/*
* (non-Javadoc)
*
* #see
* android.widget.AdapterView.OnItemSelectedListener#onNothingSelected
* (android .widget.AdapterView)
*/
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
};
#Override
public void onClick(View v) {
if (spinner != null && categories != null) {
Log.v("Selected Spinner Item ",
categories.get(spinner.getSelectedItemPosition()));
}
}
You haven't provided adapter to spinner. please use below code for adapter:
Spinner spinner = new Spinner(this);
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, spinnerArray);
spinner.setAdapter(spinnerArrayAdapter);
after that if u want text of an item than use below code:
String value = spinnerArray[spinner.getSelectedItemPosition()];
which will return value of selected item in spinner. Default it is 0.
I'm very new to android development and this is basically my first app.
I followed the guide on developer.android.com and other souces on the internet to work with spinners.
On my main activity, I have a spinner and a button and what I would like to do is to Enable (setClickable) the button based on the spinner selected item.
public class MainMenu extends Activity {
private Spinner spinner1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_menu);
//Add items to spinner 1 dynamically
addItemsOnSpinner1();
addListenertoSpinner();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main_menu, menu);
return true;
}
public void addItemsOnSpinner1(){
spinner1 = (Spinner) findViewById(R.id.spinner1);
List<String> list = new ArrayList<String>();
list.add("Test 1");
list.add("Test 2");
list.add("Add new");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
spinner1.setAdapter(dataAdapter);
}
public void addListenertoSpinner(){
spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner1.setOnItemSelectedListener(new CustomOnItemSelectedListener());
}
}
I have successfully created an OnItemSelectedListener class from which I can display the spinner selected item text:
public class CustomOnItemSelectedListener extends Activity implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
// TODO Auto-generated method stub
if(parent.getItemAtPosition(pos).toString()== "Add new"){
Toast.makeText(parent.getContext(),
"OnItemSelectedListener : " + parent.getItemAtPosition(pos).toString(),
Toast.LENGTH_SHORT).show();
}
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
My question is:
How can I set the Button.setClickable?
I tried doing something like:
if(parent.getItemAtPosition(pos).toString()== "Add new"){
Button b1 = (Button)findViewById(R.id.button1);
b1.setClickable(true);
}
but the application crashes...
Cheers in advance!
You shouldn't extend from activity just for a listener. Try this
public void addListenertoSpinner(){
spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
if(parent.getItemAtPosition(pos).toString()== "Add new"){
Button b1 = (Button)findViewById(R.id.button1);
b1.setClickable(true);
}
}
public void onNothingSelected(AdapterView<?> arg0) {
}
});
}
This creates an anonymous class (within the concept of the CURRENT activity) that can safely use findViewById().