I have a spinner in normal activity and a wants to try to use spinner in fragment but in the fragment, shows empty when run it
example
My code in onCreateView
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View vista =inflater.inflate(R.layout.fragment_pag1,container,false);
calcular= (Button)vista.findViewById(R.id.button);
etd=(EditText)vista.findViewById(R.id.editText);
resultadocp=(TextView)vista.findViewById(R.id.textView3);
lista = (Spinner)vista.findViewById(R.id.spinner);
String []opciones={"one","two","three","four","five"};
ArrayAdapter adapter = new ArrayAdapter(getActivity(),android.R.layout.simple_list_item_1, opciones);
lista.setAdapter(adapter); return vista; }
Example one:
You can use this
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this.getActivity(), android.R.layout.simple_spinner_item, opciones);
adapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
lista.setAdapter(adapter);
`Insted of
ArrayAdapter adapter = new ArrayAdapter(getActivity(),android.R.layout.simple_list_item_1, opciones);
lista.setAdapter(adapter);
Example two:
Create spinner xml
<Spinner
android:id="#+id/spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
Add your spinner string to string.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="string_array">
<item>one</item>
<item>two</item>
<item>three</item>
<item>four</item>
<item>five</item>
</string-array>
Add this code to your fragment
Spinner spinner = (Spinner)lista.findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this.getActivity(),
R.array.string_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
just try this...
I hope it works for you...
Easy if only title is missing and dropdown working
spinner.setPrompt("Title"); or xml: android:prompt="#string/title"
Btw can you elaborate problem and xml file use
spinner.setOnItemSelectedListener(this);
and override
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// On selecting a spinner item
String item = parent.getItemAtPosition(position).toString();
// Showing selected spinner item
}
Related
I have an android spinner that allows the user to select a translation. I can tap the spinner and it will reveal a list with available translations, but when I select an item in the list it will not appear in the spinner and neither does the onItemSelected method get called.
Here is the xml code for the spinner:
<Spinner
android:id="#+id/trans_list"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_toRightOf="#+id/chap_list"
android:layout_weight="1" />
Here is the relevant code for initiating the spinner. (edit) This code is ran from inside a Fragment class and not from my MainActivity class:
trans_spinner = v.findViewById(R.id.trans_list);
ArrayAdapter<String> adapter = new ArrayAdapter<>(v.getContext(), android.R.layout.simple_spinner_dropdown_item, translations);
trans_spinner.setAdapter(adapter);
trans_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String translation = trans_spinner.getItemAtPosition(trans_spinner.getSelectedItemPosition()).toString();
Log.d("trans", translation);
Toast.makeText(view.getContext(), translation, Toast.LENGTH_LONG).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
Log.d("test", "1");
}
});
trans_spinner.setSelection(1);
Neither the onItemSelected() or onNothingSelected() method gets called when I select an item. I found this page describing an issue very similar to mine:
Android Spinner will not launch OnItemSelected and current selected item is not displayed in Spinner
However the user did not present a clear solution to their problem so it doesn't help me much.
I am not sure if it is relevant, but the items in the spinner are taken from an online webpage that provides JSON data to fill the spinner. This seems to work, as the options do appear in the spinner list. The issue is that upon selecting one of them, the spinner appears empty and the listener doesn't do anything.
So I found out that the issue was actually because of how I obtained the list of strings to put inside the spinner. As mentioned in the original post, I am loading the string values from a webpage in JSON format. I'm using the Retrofit2 API for this.
The problem was caused by the fact that I was downloading this data and then initializing the spinner both in the onCreateView() method of my fragment. What I did to fix it is I created a new method that initializes the spinner, and then I would call it from the onResponse() method used by the Retrofit API. This means that it doesn't initialize the spinner until after it has finished downloading/populating the list of strings.
<Spinner
android:id="#+id/planets_spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
f the available choices for your spinner are pre-determined, you can provide them with a string array defined in a string resource file:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="planets_array">
<item>Mercury</item>
<item>Venus</item>
<item>Earth</item>
<item>Mars</item>
<item>Jupiter</item>
<item>Saturn</item>
<item>Uranus</item>
<item>Neptune</item>
</string-array>
</resources>
With an array such as this one, you can use the following code in your Activity or Fragment to supply the spinner with the array using an instance of ArrayAdapter:
Spinner spinner = (Spinner) findViewById(R.id.spinner);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.planets_array, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner.setAdapter(adapter);
When the user selects an item from the drop-down, the Spinner object receives an on-item-selected event.
To define the selection event handler for a spinner, implement the AdapterView.OnItemSelectedListener interface and the corresponding onItemSelected() callback method. For example, here's an implementation of the interface in an Activity:
public class SpinnerActivity extends Activity implements OnItemSelectedListener {
...
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
// An item was selected. You can retrieve the selected item using
// parent.getItemAtPosition(pos)
}
public void onNothingSelected(AdapterView<?> parent) {
// Another interface callback
}
}
The AdapterView.OnItemSelectedListener requires the onItemSelected() and onNothingSelected() callback methods.
Then you need to specify the interface implementation by calling setOnItemSelectedListener():
Spinner spinner = (Spinner) findViewById(R.id.spinner);
spinner.setOnItemSelectedListener(this);
If you implement the AdapterView.OnItemSelectedListener interface with your Activity or Fragment (such as in the example above), you can pass this as the interface instance.
Java Code
public class MainActivity extends AppCompatActivity {
Spinner spinner;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner = findViewById(R.id.spinner);
List<String> translations = new ArrayList<>();
translations.add("string1");
translations.add("string2");
translations.add("string3");
translations.add("string4");
translations.add("string5");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_dropdown_item, translations);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Object getPosition = parent.getItemAtPosition(position);
String value = getPosition.toString();
Toast.makeText(getActivity(), value, Toast.LENGTH_LONG).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}
XML Code
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/spinner">
</Spinner>
Add this code after init adapter
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
And to get the selected item inside onItemSelected
translations.get(position)
I want to make an app which contains a Spinner and a ListView and when I click a Sinner item it will open a new ListView, no new Activity just to update the ListView from the database.
For instance, I have Europe and Africa in the Spinner and when I click Europe it will show me a ListView with the European countries, when I click Africa it will show me a ListView with the African countries.
I searched this on the internet, but I could not find anything.
First of all, to set up a spinner you need an adapter. Then you need to use an OnItemSelectedListener. Use this code:
europeListView = (ListView) findViewById(R.id.europeListView);
africaListView = (ListView) findViewById(R.id.africaListView);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.countries, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Spinner spinner = (Spinner) findViewById(R.id.spinner);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
if(adapter.getItem(i) == "Europe"){
europeListView.setVisibility(View.VISIBLE);
africaListView.setVisibility(View.INVISIBLE);
} else if(adapter.getItem(i) == "Africa"){
africaListView.setVisibility(View.VISIBLE);
europeListView.setVisibility(View.INVISIBLE);
}
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
Finally, make a string array called "countries" containing Europe and Africa in your strings.xml file like so:
<string-array name="countries">
<item>Europe</item>
<item>Africa</item>
</string-array>
Hope this helps!
I am trying to save spinner selected value, but i am getting like below i shown when i retrieve the details. Anybody know what is the problem.
Spinner:android.widget.Spinner#43e807a0
Have you used the getSelectedItem() inside setOnSelectedListner? If not, do as shown below:
mPres_doctor.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> adapter, View view,
int position, long id) {
String pres_doctor = mPres_doctor.getSelectedItem().toString();
}
public void onNothingSelected(AdapterView<?> arg0) {
}
});
The result is displaying as an object value, usually I follow the below method to get the spinner values:
Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.spinner, android.R.layout.spinner_layout);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
strings.xml
<string-array name="spinner">
<item>Dev</item>
<item>Stieve</item>
<item>John</item>
<item>Britto</item>
</string-array>
i have 2 spinners :
FIRST SPINNER :
array_spinner=new String[3];
array_spinner[0]="Color";
array_spinner[1]="Model";
array_spinner[2]="Price";
Spinner s = (Spinner) findViewById(R.id.select);
ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_spinner_item,
array_spinner);
s.setAdapter(adapter);
SECOND SPINNER :
array_color=new String[3];
array_color[0]="Black";
array_color[1]="Pink";
array_color[2]="Green";
array_model=new String[3];
array_model[0]="Ipod";
array_model[1]="mp3";
array_price=new String[3];
array_price[0]="1000-2000";
array_price[1]="3000-4000";
Spinner val=(Spinner)findViewById(R.id.select_val);
ArrayAdapter adapter1 = new ArrayAdapter(this,
android.R.layout.simple_spinner_item, array_color);
val.setAdapter(adapter1);
When the user clicks Model in the First Spinner , immediately the Second Spinner should display the models which are in the array_model ... Please Help !!!!
use setOnItemSelectedListener for the first spinner. In the onItemSelected() set a new arrayAdapter for the second spinner with the new data set
I think you must put a listener on the spinner.
Then You create the good second spinner.
example :
s1.setOnItemSelectedListener(
new OnItemSelectedListener() {
public void onItemSelected(
AdapterView<?> parent, View view, int position, long id) {
if(array_spinner[int]=="model"){
//Do your second spinner
}
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
I was using a spinner in a TableRow and it was working perfectly well except that I didn't like spinner icon is stretching out according to the selected item. I tried to delete Spinner section in xml and create it on my code.
To create a spinner in OnCreate():
selectArea = /*(Spinner)this.findViewById(R.id.spinner);*/new Spinner(this);
String[] ss = getResources().getStringArray(R.array.countries);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, ss);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
selectArea.setOnItemSelectedListener(new MyOnItemSelectedListener());
selectArea.setSelection(prefInt);
To handle a selection event:
public class MyOnItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
Log.d(TAG, "onItemSelected() " + id);
}
public void onNothingSelected(AdapterView<?> parent) {
// Do nothing.
}
}
It worked almost perfect. TableRow show no spinner icon, on touching TableRow, it pops up items to be selected. My only problem is that, on selecting one tiem, I never get my onItemSelected() called..
What could be wrong?
please change this line
spinner.setAdapter(adapter);
into the line
selectedArea.setAdapter(adapter);
see i have created spinner here
Spinner selectArea= new Spinner(Activity.this);
String[] ss = getResources().getStringArray(R.array.countries);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, ss);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
selectArea.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
selectArea.setAdapter(adapter);
linearLayout1.addView(selectArea);//to add your spinner
selectArea.setOnItemSelectedListener(new MyOnItemSelectedListener());