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
Related
I have two spinners in which the first spinner uses autocomplete textview , when first spinner item is selected in normal way without using autocomplete text view the second spinner loads the corresponding value according to the first spinners selection. My issue is that when autocomplete textview is used for selecting first spinner value the second spinner is not loaded with the values according to first spinners selection. can any one please tell me a solution
public void vesselspinnerHandler(){
//Setting Class to Spinner
final ArrayAdapter<String> vesselspinnerAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, vessellist);
vesselspinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
vesselspinner.setAdapter(vesselspinnerAdapter);
vesselspinnerAdapter.notifyDataSetChanged();
final AutoCompleteTextView searchvessel=findViewById(R.id.searchvessel);
final AutoCompleteTextView selves = findViewById(R.id.selectedvessel);
ArrayAdapter adapter1 = new ArrayAdapter(selection.this,android.R.layout.simple_spinner_dropdown_item,vessellist);
selves.setAdapter(adapter1);
selves.setDropDownVerticalOffset(50);
vesselspinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
selves.setText(vesselspinner.getSelectedItem().toString());
searchvessel.setText(selves.getText().toString());
getdate();//second spinner call
return;
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
I'm developing an application for Android TV. I made a custom view where I represent list items (in RecyclerView).
How can I get a focus on my items and highlight them (a simple example would be wonderful)?
I did try to add
android:focusableInTouchMode="true"
android:focusable="true"
to .xml file, but I've not seen any changes.
EDIT: I want to get focus when navigating through items by dpad
in youf adapter do something like this
#Override
public void onBindViewHolder(ViewHolder viewHolder, int i) {
viewHolder.YOUR_LAYOUT.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// do whatever you want
viewHolder.YOUR_LAYOUT.setBackgroundResource(R.drawable.ripple_effect);
}
});
}
Why don't you use setOnitemClicklistener on your listview or recycleview . This is how you can do this:
ListView lv = getListView();
setContentView(lv);
lv.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> arg0, View view,int position, long arg3)
{
Toast.makeText(Activity.this, "" + position, Toast.LENGTH_SHORT).show();
//========== Here you can change the color of selected item ========
TextView Tv_title = (TextView)view.findViewById(R.id.title);
Tv_title.setTextColor(Color.RED);
//====== Your can make any view color change here just like i did for textview ===
}
});
If still you are not getting focusable try using this solution :
Use android:descendantFocusability="blocksDescendants" in your XML file of the first parent layout inside the list.
I have an Activity where I need to create 1 or more spinners dynamically according to an external DB.
SOme of this spinner items have to show a dialog according what value does the spinner has. For example the spinner has this options:
-Own
-Rental
-Family House
If the user selects Rental I have to show a dialog (or anything) asking him how much does he pays per month. If he selects own, or family nothing should happen.
After I create the layout with the spinners, edittexts, etc. Im using something like this:
for(int q=0;q<=parent.getChildCount();q++){
View v = parent.getChildAt(q);
if (v instanceof Spinner) {
Spinner res = (Spinner) v;
res.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
//Here its supposed to show dialog if the option is "RENT"
}
public void onNothingSelected(AdapterView<?> adapterView) {
return;
}
});
}
}
The problem is that when I do this the "setOnItemSelectedListener" only sets for the last spinner on the layout.
How can I do what Im trying? I dont know what else to do.
The easiest solution would probably be to make one Listener as a variable and use that for all of your spinners. To do this, you would not set it as you are currently (using the anonymous inner-class style) and instead would do this:
//This goes outside of the method
private AdapterView.OnItemSelectedListener listener =
new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
System.out.println("Spinner Selected ID = " + parent.getId());
/*
Put a check here for which one is being selected.
While you could use the parent to check, in your case, it will be easier
to use something from your DB table as a unique identifier (maybe a column
name would be ideal? Your pick)
*/
//Show your dialogs here
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
return;
}
};
//This is the method you have where you are iterating the parent object
private void doStuff(){
for(int q=0;q<=parent.getChildCount();q++){
View v = parent.getChildAt(q);
if (v instanceof Spinner) {
Spinner res = (Spinner) v;
res.setOnItemSelectedListener(listener);
}
}
}
Good luck to ya!
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) {}
});
I have a Layout in Android which has a ListView which then inflates another layout as its rows using an adapter, pretty standard.
The row layout which is inflated X times depending on how many rows there needs to be contains a spinner. I add a seSelectedItemListener to the Spinner yet when the Spinner is pressed, the options are displayed, then an item is pressed/selected, then the OnItemSelectedListener constructor is fired yet the overrided onItemSelected is not. If i inflate the row layout seprately not in a List View and select an item in the Spinner then the onSelectedItem fires...
Any ideas why this happens or how/if it can work this way?
OnItemSelected class:
class SelectedSizeChangeListener implements OnItemSelectedListener {
Spinner product_size;
TextView product_sell_price;
TextView product_cost_price;
Drink item;
Drink_Size drink_size;
SelectedSizeChangeListener()
{
String s = "here1";
}
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
String s = "here2";
}
public void onNothingSelected(AdapterView parent) {
String s = "here3";
}
}
Adding the onItemSelected class to the spinner:
product_size_spinner.post(new Runnable() {
public void run() {
product_size_spinner.setOnItemSelectedListener(new SelectedSizeChangeListener());
}
});
Any help would be great!
Adrian
Try
android:focusable="false"
android:focusableInTouchMode="false"
in list view
Pro-tip:
try to use PopUpMenu rather than Spinner.
Example android-popupwindow in list View