Dismiss spinner's dialog when user presses out - android

First of all sorry for my english. I know it isn't very good but i hope you can understand me. I've been looking into this for some hours. I have created a spinner in a layout file setting the spinnerMode parameter to "dialog":
<Spinner
android:id="#+id/spinner2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/llBarra_list"
android:drawSelectorOnTop = "true"
android:prompt="#string/spinner_title"
android:background="#drawable/btn_dropdown_normal"
android:spinnerMode="dialog"/>
When user presses in the spinner, a dialog populates with some options defined in one ArrayList according to:
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(getActivity().getBaseContext(),android.R.layout.simple_spinner_item, listaHashes);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(dataAdapter);
spinner2.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,int arg2, long arg3) {
String text = listaHashes.get(arg2);
lblHash.setText(text);
ArrayList<String> tweetsEsteHash= new ArrayList<String>();
tweetsEsteHash.add(text);
receiveTweets(tweetsEsteHash);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
The dialog doesn't fill parent's width and height, which is fine for my intentions. I need that when the user touches outside the dialog, the dialog dismisses itself. Should i create a custom spinner with a custom spinner dialog or are there some options which i can use to achieve this?

Related

Spinner, strings can be seen in the background

I use to change the difficulty from my game a Spinner Item and use the strings "Leicht","Mittel","Schwer" then i took as background an drawable file, but when i start the application i can see in the background the strings i used for the spinner.. can i hide the strings? that i see just the drawable background?
schwierigkeitsgradAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, new String[] {" Leicht"," Mittel"," Schwer"});
schwierigkeitsgradAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
schwierigkeitsgrad.setAdapter(schwierigkeitsgradAdapter);
and the xml code:
<Spinner
android:id="#+id/schwierigkeitsgrad"
android:layout_gravity="center_horizontal"
android:layout_marginTop="360dp"
android:background="#drawable/btnschwierigkeit"
android:textAlignment="center"
android:layout_width="220dp"
android:layout_height="90dp"
android:gravity="center_vertical|start"
android:foregroundGravity="center_vertical"
android:spinnerMode="dialog" />
try this to hide spinner text during selection:
Spinner spinner = (Spinner)findViewById(R.id.your_spinner);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// hide selection text
((TextView)view).setText(null);
// if you want you can change background here
}
public void onNothingSelected(AdapterView<?> arg0) {
}
});
To show spinner items use:
String[] test=new String[]{"easy", "medium", "heavy"};
ArrayAdapter<String> adapter= new ArrayAdapter<String>(yourActivity.this,android.R.layout.simple_spinner_item, test);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
schwierigkeitsgrad.setAdapter(adapter);

why doesn't Spinner respond to setOnItemSelectedListener?

The code below fills the spinner with data but when i select an item in the spinner no event is triggered. Any ideas ?
spinner itself is inside of toolbar
toolbar_spinner_item
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:singleLine="true"
android:layout_width="150dp"
android:layout_height="48dp"
android:ellipsize="marquee"
android:background="#color/white"
android:textColor="#color/dark_grey"/>
some activity onCreate
ArrayAdapter spinnerAdapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.date_ranges, android.R.layout.simple_spinner_dropdown_item);
spinnerAdapter.setDropDownViewResource(R.layout.toolbar_spinner_item);
Spinner spinner = (Spinner)findViewById(R.id.toolbar_spinner);
spinner.setAdapter(spinnerAdapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
return;
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
return;
}
});
Just turning my comment into an answer ;-)
Instead of adding return; try to add a Log.d call for instance and see if it writes the log statement.
If you're using Android Studio, sometimes it skips certain lines when debugging.

Android Spinner not calling onItemSelected

EDIT: I did a workaround and it SORT OF fixed my issue.
My layout for the spinner looks like
<LinearLayout>
<Spinner>
<TextView> //used as a label
</LinearLayout>
I removed the LinearLayout. Though the City spinner does not become auto-populated with items whenever a State is selected, I am able to manually input values for the City.
What could the linearlayout possibly do to mess up my spinner's behavior?
I have a registration form with multiple Edittexts and 3 spinners at the bottom namely State, City, Area.
What I want to be able to do is the following
onItemSelect a State, City and Area will show the first items in their list
onItemSelect a City, Area will show the first item in its list
onItemSelect an Area, it will show the item I selected.
Code is working fine on Kitkat. But on JellyBean and below, whenever I select a State, the other spinners do not show anything. After selecting a State, when you click on the City, it will show correct dataset, however when I click on an item the spinner remains blank. (onItemSelected does not fire.)
The weird part is that, whenever the spinners misbehave, if I click on one of the Edittexts in my view (softKeyboard will be shown) > Press back button > Spinner works. I'm not sure if it has something to do with focus? But I already tried to set the spinners to focusable and focusableInTouchMode then request focus, but to no avail.
this is how I set the adapters and content of the spinners
state.setAdapter(stateAdapter);
state.setSelection(0, false);
state.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
stateSelection = parent.getItemAtPosition(position).toString();
cityAdapter = new ArrayAdapter<String>(getActivity(), R.layout.sherlock_spinner_item, getDistinctCity(stateSelection));
cityAdapter.setDropDownViewResource(R.layout.sherlock_spinner_dropdown_item);
city.setAdapter(cityAdapter);
areaAdapter = new ArrayAdapter<String>(getActivity(), R.layout.sherlock_spinner_item, new String[]{});
areaAdapter.setDropDownViewResource(R.layout.sherlock_spinner_dropdown_item);
area.setAdapter(areaAdapter);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
city.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
citySelection = parent.getItemAtPosition(position).toString();
areaAdapter = new ArrayAdapter<String>(getActivity(), R.layout.sherlock_spinner_item, getDistinctArea(stateSelection, citySelection));
areaAdapter.setDropDownViewResource(R.layout.sherlock_spinner_dropdown_item);
area.setAdapter(areaAdapter);
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
area.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
areaSelection = parent.getItemAtPosition(position).toString();
postalSelection = getDistinctPostal(stateSelection, citySelection, areaSelection);
postal.setText(postalSelection);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
If anyone can explain to me why the spinners are behaving like this, or possibly lead me to the right direction, that would be awesome. Thank you

Android Spinner taking time, after select value from adapter

In my application there are lot's of views(spinner, buttons, editText, textView, ImageButton etc).
After selecting one value in the spinner it not reflect the value which I selected. The old value is till there on the spinner. But when I tap on other views like editTest then spinner value automatically updated.
I think no need to mention the code because normal spinner is there which populating data from ArrayAdapter and I am doing small task after selecting value.
CODE:-
spinnerWard = (Spinner) findViewById(R.id.spinnerWard);
aAdapterWard = new ArrayAdapter<WardList>(this,
android.R.layout.simple_spinner_item, listWard);
aAdapterWard.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerWard.setAdapter(aAdapterWard);
spinnerWard.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View v,
int pos, long id) {
intWardPosition = pos;
intFinalWardId = listWard.get(pos).getId();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {}
});

Hide text of Android spinner

I have a spinner with a background image. But when I add array adapter to the spinner, the text is shown on the background image. I want to hide this text. There is also a TextView. My requirement is, when I click the image, the spinner should pop up and when I selects an item, the TextView gets updated with the selected item.
Spinner spin = (Spinner) d.findViewById(R.id.searchCriteria);
spin.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// hide selection text
((TextView)view).setText(null);
// if you want you can change background here
}
public void onNothingSelected(AdapterView<?> arg0) {}
});
Create ghost_text.xml in your layouts folder with this text:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#android:color/transparent" />
Then use that file in your adapter like this:
ArrayAdapter<String> your_adapter = new ArrayAdapter<String>(this,
R.layout.ghost_text, your_list);
your_spinner.setAdapter(your_adapter);
You should also be able to use this technique with other resource-based adapters.
Probably you can define a xml-layout file, without any textview in it, and supply it to the adapter for the spinner.
e.g. empty_spinner_item.xml
<ImageView xmlns:android="http..."
android:layout_width=".."
/>
and then use any adapter:
spinner.setAdapter(new SimpleAdapter(this, data, R.layout.empty_spinner_item, ... ));

Categories

Resources