Set EditText visibility based on Spinner value
-
11-06-2021 - |
Frage
I'm trying to create this dialog:
.
When Spinner is set to custom value, TextEdit should automatically appear. I'm calling View.setVisible()
on the TextView
but the visibility is not evaluated immediately but waits to another change - e.g. adding another row or setting a date.
The code:
...
customText = (EditText) v.findViewById(R.id.edit_custom_text);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s.setAdapter(adapter);
s.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
SpinnerItem si = (SpinnerItem) adapterView.getItemAtPosition(i);
evt.type = si.eventType;
if (evt.type == EventType.CUSTOM) {
customText.setVisibility(View.VISIBLE);
} else {
customText.setVisibility(View.GONE);
}
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
//do nothing
}
});
I tried View.invalidate()
(on parent view) and View.refreshDrawableState()
with no luck :/
Edit: The code above is reached (verified by debugger) and I also tried View.INVISIBLE
. The view is just not refreshed immediately but only after another change in view.
Lösung
That should work, could it be that your layout somehow doesn't allow/recognises this change perhaps?
Try changing it to INVISIBLE
instead of GONE
, including (important!) in your layout xml file.
If that works for some reason, try something like this:
customText.getParent().requestLayout(); //possibly the parent of that etc
Andere Tipps
For Example see this
s.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView,View selectedItemView, int position, long id) {
if ("YES".equals(s.getSelectedItem().toString().toUpperCase())) {
youredittxt.setVisibility(View.VISIBLE);
} else if ("NO".equals(s.getSelectedItem().toString().toUpperCase())) {
youredittxt.setVisibility(View.INVISIBLE);
}}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
As a follow up question, are you in the main UI thread? Because Android have some built in features and policies, so only the owning thread will be able to change the UI. If you are outside the same thread, try:
customText.getHandler().post(new Runnable() {
public void run() {
customText.setVisibility(View.VISIBLE);
}
});
Hope this helps! :)
Verify that you are actually reaching your code block.
customText.setVisibility(View.GONE);