Egek92 Egek92 -4 years ago 116
Android Question

How to create EditText fields depending on the spinner option?

Question may seem unclear, I need to create EditTexts depending on the selected spinner option. For instance:

Spinner:

<Spinner
android:id="@+id/spn_options"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:entries="@array/spinner_options"
/>


Spinner Options:

<string-array name="spinner_options">
<item>One </item>
<item>Two </item>
<item>Three </item>
<item>Four </item>
</string-array>


Activity:

spnOptions.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String chosenOption = (String) parent.getItemAtPosition(position);

}
@Override
public void onNothingSelected(AdapterView<?> parent) {}
});


I have this spinner with 4 options, if the users select 3, 3 EditText will be appear under the spinner. I tried to create 4 EditTexts and set them Invisible and make them visible depending on the choice but it seems like a horrible idea in performance wise. Anyone have another idea.

Answer Source

Create the four EditText in your xml layout with visibility="GONE". Then depending on Spinner selection change visibility to VISIBLE using setVisibility()

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download