I have an edit text in which I enter a number. I want according to the number entered in edit text to create spinners. For example if I enter 2 in edit text I have 2 spinners if 4 ==> 4 spinners.
Can any one post any example and please suggest which technique is better defining spinner data in Java or Xml?
used below code ...
public class MainActivity extends AppCompatActivity {
private EditText editText;
private Button mbtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
// toRun();
}
private void initView() {
editText = findViewById(R.id.amEtNumber);
mbtn = findViewById(R.id.amBtnSave);
LinearLayout main_layer = (LinearLayout) findViewById(R.id.main);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
LinearLayout layout = new LinearLayout(getApplicationContext());
LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setLayoutParams(params);
mbtn.setOnClickListener(view -> {
int times = Integer.parseInt(editText.getText().toString().trim());
for (int i = 0; i < times; i++) {
List<String> spinnerArray = new ArrayList<>();
spinnerArray.add("Select Item");
spinnerArray.add("Item1");
Spinner spinner = new Spinner(this);
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>
(this, android.R.layout.simple_spinner_item,
spinnerArray); //selected item will look like a spinner set from XML
spinnerArrayAdapter.setDropDownViewResource(android.R.layout
.simple_spinner_dropdown_item);
spinner.setAdapter(spinnerArrayAdapter);
layout.addView(spinner);
}
main_layer.addView(layout);
});
}
}