I would like to have a fix value that is always displayed in the Spinner and when clicking on the Spinner this value should not be listed in the drop down selection.
Until now I have the following
Definition XML:
<Spinner
android:layout_width="76dp"
android:layout_height="40dp"
android:id="#+id/right_shift"
android:layout_row="0"
android:layout_column="0"/>
Java:
final Spinner right = (Spinner) findViewById(R.id.right_shift)
ArrayList<String> rightShift = new ArrayList<String>();
rightShift.add(" >>"); //THIS SHOULD BE THE VALUE THAT IS ALWAYS DISPLAYED
for (int i=0; i<5; i++)
...//add other values to arraylist
...//set values of arraylist to spinner
right.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
right.setSelection(0);
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
But when clicking on the Spinner the preselected Item will again be shown in the drop down and the right.setselection(0) is not executed fast enough so I still see the selected Item for about 0.5sec... Is there an other/easier way to perform this?
you can add
android:prompt=" >>"
in xml and in java set default position in spinner to be -1.
Related
I'm trying to make a very very simple spinner at least, as follows:
XML:
<Spinner
android:id="#+id/spinner_categories"
android:layout_width="0sp"
android:layout_height="wrap_content"
android:drawSelectorOnTop="true"
android:layout_weight="1"
android:textColor="#000000"
android:spinnerMode="dropdown"/>
JAVA:
spinnerCategories = findViewById(R.id.spinner_categories);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
getApplicationContext(),
android.R.layout.simple_spinner_item,
categories);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerCategories.setAdapter(adapter);
Log.d(Utilities.LOG_FLAG, "SPINNER: " + spinnerCategories.toString());
spinnerCategories.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) {
Toast.makeText(context, categories.get(position).toString(), Toast.LENGTH_SHORT).show();
Log.d(Utilities.LOG_FLAG, "SELECTED");
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
Log.d(Utilities.LOG_FLAG, " NOT SELECTED");
}
});
I can see the entire list, but once I click on an item, nothing happans, and it wont show the selection at all, even if I use setSelection(), and if I try to do spinnerCategories.getSelectedItem().toString() I get a NullPointerException.
I tried searching the web a lot, but none solution seems to help me...
Edit
For some reason when I load the page and then I go out of the page and reenter it, only then it will show the selected items of the spinner
On the first load it shows for a very brief second and then it's gone until the page is reentered the second time
The setOnItemSelectedListener gets triggered when you click on an element from the drop down menu .. to fetch the selected item you need to use .getSelectedItemPosition() something like
categoriesList.get(spinner.getSelectedItemPosition)
Please tell me how can I hide the item at index 0 from the dropdown in the spinner in Android Studio? I am using this code, it works, but when I open the list, it shows at the bottom. That is, it is focused on the elements below. what do i need to change?
SpinnerName = (Spinner) v.findViewById(R.id.spinner1);
ArrayList<String> names = new ArrayList<>();
names.add(0, "SELECT");
names.add(1, "Name1");
names.add(2, "Name2");
final int listsize = names.size()-1;
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getContext(), android.R.layout.simple_spinner_item, names){
#Override
public int getCount() {
return(listsize);
}
};
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
SpinnerName.setAdapter(adapter);
adapter.setDropDownViewResource(R.layout.spinner_list);
SpinnerName.setSelection(listsize);
SpinnerName.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int pos,
long id) {
....
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
SpinnerName.setSelection(listsize);
your problem is here! you are passing the total list size number and
that where you are doing wrong, because setSelection method use to
show default index of spinner.
you can simply do that SpinnerName.setSelection(1); that would give you the fist spinner item unless showing the names.add(0, "SELECT"); item
I have a spinner that i fill with 4 elements. During startup onItemSelected method gets executed correctly and the toast message is displayed. However when i open the spinner and try to click any item, no event is called and the spinner popup will not close unless i press the spinner arrow. In other words I can not interact with spinner items.
Below is the activity code for the spinner
<Spinner
android:id="#+id/sp_size"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
this is my java code
ArrayList<String> strItemsize = new ArrayList<String>(Arrays.asList("H 1", " H 2", " H 3", "H 4", "H 5"));
final ArrayList<Integer> Hsize = new ArrayList<>(Arrays.asList(18, 16, 14, 12, 10));
ArrayAdapter<String> adapterSize = new ArrayAdapter<String>(App.CurentActivity, android.R.layout.simple_spinner_dropdown_item, strItemsize);
adapterSize.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spSize.setAdapter(adapterSize);
spSize.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
App.ShowMessage().ShowToast(""+i, ToastEnum.TOAST_SHORT_TIME);
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
i searching but dose not solve it.this Link is my problem like but dos not my solotion.
Set in XML for spinner clickable=true
Or use Widget.AppCompat.Spinner instead. Seems to be Marshmallow bug
You should set spinner attribute android:clickable=true in your XML.
<Spinner
android:id="#+id/sp_size"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable=true />
There are 4 Spinnners:
resiko,untung1,untung2,untung3
The user picks resiko first to proceed and the app will show which Spinner will be visible (untung1/untung2/untung3)
Trying to make dynamic data Spinner, when you click an item inside the first Spinner, the others will be gone, and the desired one will be visible, working so far.
The problem is is when i pick "tinggi" from the Spinner resiko, it get an error: java.lang.IndexOutOfBoundsException
I also tried reading other posts, but still not sure what should I do.
I tried using getSelectedItem() before, but when I want to take the desired data from the visible Spinner, which is selected by the user, the app didn't pick the selected item itself, instead it picked the first data in the visible Spinner.
(let's say there's 2 value in the Spinner, A and B; the user pick B, but the program picks A)
in example:
the user picks "rendah" in the "resiko" Spinner, and then the next visible Spinner is untung2, then the user picks "sedang" in that Spinner,
but the program picks "--pilih--" instead of "sedang"
That's why I switched to getItemAtPosition(position).toString();
strings.xml
<string-array name="spinner_resiko_string">
<item>--Pilih--</item>
<item>Sangat Rendah</item>
<item>Rendah</item>
<item>Sedang</item>
<item>Tinggi</item>
</string-array>
<string-array name="spinner_return_string">
<item>--Pilih--</item>
<item>Rendah</item>
</string-array>
<string-array name="spinner_return_string2">
<item>--Pilih--</item>
<item>Rendah</item>
<item>Sedang</item>
</string-array>
<string-array name="spinner_return_string3">
<item>--Pilih--</item>
<item>Rendah</item>
<item>Sedang</item>
<item>Tinggi</item>
</string-array>
spinner declaration :
final Spinner resiko = (Spinner) mScrollView.findViewById(R.id.spinner_resiko);
final Spinner untung1 = (Spinner) mScrollView.findViewById(R.id.spinner_return1);
final Spinner untung2 = (Spinner) mScrollView.findViewById(R.id.spinner_return2);
final Spinner untung3 = (Spinner) mScrollView.findViewById(R.id.spinner_return3);
spinner in xml :
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/spinner_return1"
android:entries="#array/spinner_return_string"
android:layout_marginLeft="10dp"/>
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/spinner_return2"
android:entries="#array/spinner_return_string2"
android:layout_marginLeft="10dp"/>
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/spinner_return3"
android:entries="#array/spinner_return_string3"
android:layout_marginLeft="10dp"/>
code version 1 (error index out bound when i pick "tinggi" in resiko spinner ) [ using getItematPosition ] :
resiko.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> mRelative, View selectedItemView, int position, long id) {
String resikox = mRelative.getItemAtPosition(position).toString();
if (resikox.equals("Sangat Rendah")) {
untung1.setVisibility(View.VISIBLE);
untung2.setVisibility(View.GONE);
untung3.setVisibility(View.GONE);
untungx = untung1.getItemAtPosition(position).toString();
}
else if (resikox.equals("Rendah")){
untung1.setVisibility(View.GONE);
untung2.setVisibility(View.VISIBLE);
untung3.setVisibility(View.GONE);
untungx = untung2.getItemAtPosition(position).toString();
}
else if (resikox.equals("Sedang") || (resikox.equals("Tinggi"))) {
untung1.setVisibility(View.GONE);
untung2.setVisibility(View.GONE);
untung3.setVisibility(View.VISIBLE);
untungx = untung3.getItemAtPosition(position).toString();
} else {
untung1.setVisibility(View.GONE);
untung2.setVisibility(View.GONE);
untung3.setVisibility(View.GONE);
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// Another interface callback
}
});
version 2 ( using getItemSelected )
//set spinner
resiko.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> mRelative, View selectedItemView, int position, long id) {
String resikox = mRelative.getItemAtPosition(position).toString();
if (resikox.equals("Sangat Rendah")) {
untung1.setVisibility(View.VISIBLE);
untung2.setVisibility(View.GONE);
untung3.setVisibility(View.GONE);
untungx = untung1.getSelectedItem().toString();
}
else if (resikox.equals("Rendah")){
untung1.setVisibility(View.GONE);
untung2.setVisibility(View.VISIBLE);
untung3.setVisibility(View.GONE);
untungx = untung2.getSelectedItem().toString();
}
else if (resikox.equals("Sedang") || (resikox.equals("Tinggi"))) {
untung1.setVisibility(View.GONE);
untung2.setVisibility(View.GONE);
untung3.setVisibility(View.VISIBLE);
untungx = untung3.getSelectedItem().toString();
} else {
untung1.setVisibility(View.GONE);
untung2.setVisibility(View.GONE);
untung3.setVisibility(View.GONE);
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// Another interface callback
}
});
logcat :
12-23 19:37:23.221 30433-30433/com.example.fabio.tabdrawer E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.fabio.tabdrawer, PID: 30433
java.lang.IndexOutOfBoundsException: Invalid index 3, size is 3
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
at java.util.Arrays$ArrayList.get(Arrays.java:66)
at android.widget.ArrayAdapter.getItem(ArrayAdapter.java:337)
at android.widget.AdapterView.getItemAtPosition(AdapterView.java:831)
at com.example.fabio.tabdrawer.Menu_PIAF$1.onItemSelected(Menu_PIAF.java:183)
at android.widget.AdapterView.fireOnSelected(AdapterView.java:964)
at android.widget.AdapterView.access$200(AdapterView.java:49)
at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:928)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5487)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
Your <item>--Pilih--</item> is causing a problem, as it is taking up the position of the first item of your spinner. So by the time you select the last item, Tinggi it throws an index out of bounds error.
This will give you advice on how to create a non-selectable item at the top of your spinner: How to make an Android Spinner with initial text "Select One"
You can change the visibility to gone, that is a good idea, however you need to implement separate onItemSelectedListeners for each spinner.
resiko,untung1,untung2,untung3 as they are separate elements and have varying sized arrays.
Although it seems like more work, it's important to keep UI elements interactions separate.
Now if there is a method that is common to a user selection, this can be modularised.
Example:
So if several different item selections cause the background color to turn yellow in a method called:
turnBackgroundYellow()
then this method can be placed in as many on item selected events as you please.
However the reverse does not work.
Each unique spinner needs to have it's listeners attached to it specifically for that spinner.
Make these class variables, so you can pass them as parameters into a class method.
String resikox_;
String untung1_; // and the others
// Create an ArrayAdapter using the string array and a default spinner layout
// Create a separate one for each spinner resiko,untung1,untung2,untung3
ArrayAdapter<CharSequence> adapter = ArrayAdapter
.createFromResource(getActivity(), R.array.dataobjects_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
resiko.setAdapter(adapter);
// Create a separate one for each spinner resiko,untung1,untung2,untung3
resiko.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
resikox_ = parent.getItemAtPosition(position).toString();
SelectedItemMethod(resikox_)
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
//DO WHATEVER OR NOTHING
}
});
// Class method to do your item selection stuff.
public void SelectedItemMethod(String item){
if (item.equals("Sangat Rendah")) {
untung1.setVisibility(View.VISIBLE);
untung2.setVisibility(View.GONE);
untung3.setVisibility(View.GONE);
}
//etc, etc for all your spinners or break it up, as you please
Add separate setOnItemSelectedListener() for each spinner
I have a spinner that populated with a string array. The spinner is populated when a button is pressed. the output is always game_0 even if I use the spinner and select say game_3. Is this because it's in a function?
public void game(View view) throws IOException {
myText="";
TextView myTextView= (TextView)findViewById(R.id.textView1);
String game_list[] = {"game_0","game_2","game_3","game_3","game_4","game_5"};
for(int i=0; i<game_list.length; i++){
myText = myText + game_list[i] +"\n";
}
Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getBaseContext(), android.R.layout.simple_spinner_item, game_list);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
String text = spinner.getSelectedItem().toString();
myTextView.setText("");
myTextView.append(text);
}
When are you calling your game(View v) method? There's no space for user action between the spinner initialization spinner.setAdapter(adapter); and the line that extract the selected value which is obviously (by default) the first of the array..."game_0".
You can use an OnItemSelectedListener to listen for user action on the spinner, or use a button with a click listener to check the spinner value upon submit.
Here's an example of a selected listener applied to a spinner:
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// here get the selected value and do whatever you want
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// ignore this if you're not going to provide a "no-selection" option
}
});