Home > Mobile >  how to set spinner textview I want a separate textview to show if the text in the spinner is clicked
how to set spinner textview I want a separate textview to show if the text in the spinner is clicked

Time:11-19

There are three buttons namely spinner,spinner2,spinner3 and next button there is sumbit button. sumbit button is clicked then show should be displayed in the TEXTVIEW.

spinner:DD

Spinner:mm

spinner:yyy

I want it in calendar format

TextView textView = (TextView) findViewById(R.id.textView2);

**

    String [] values =
        {"1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31"};
String [] values2 =
        {"1","2","3","4","5","6","7","8","9","10","11","12"};
String [] values3 =
        {"2023"};
Spinner dd = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, values);
adapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
dd.setAdapter(adapter);

Spinner mm = (Spinner) findViewById(R.id.spinner2);
ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, values2);
adapter2.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
mm.setAdapter(adapter2);

Spinner yyyy = (Spinner) findViewById(R.id.spinner3);
ArrayAdapter<String> adapter3 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, values3);
adapter3.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
yyyy.setAdapter(adapter3);

Button sumbit= (Button) findViewById(R.id.sumbit);

TextView textView = (TextView) findViewById(R.id.textView2);

**

image

CodePudding user response:

A) using array

 Spinner spinner = findViewById(R.id.spinner); //day spinner.
      ArrayList<String> arrayListdays = new ArrayList<>();
      arrayListdays.add("1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31");
      ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,                         android.R.layout.simple_spinner_item, arrayListdays);
      arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
      spinner.setAdapter(arrayAdapter);

similarly for month and year,

then button click,

btnGet.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    tvw.setText(spinner.getSelectedItem().toString() "-" spinner1.getSelectedItem().toString() "-" spinner2.getSelectedItem().toString());
                }
            });

B) using spinner alternatively, try like below,

1). for spinner like date for day,month, year use below xml, in datepicker use property android:datePickerMode="spinner" like below.

<DatePicker
    android:id="@ id/datePicker1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:datePickerMode="spinner"
    android:calendarViewShown="false"/>

2). on button click you need value in textview for this,

public class MainActivity extends AppCompatActivity {
    DatePicker picker;
    Button btnGet;
    TextView tvw;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tvw=(TextView)findViewById(R.id.textView1);
        picker=(DatePicker)findViewById(R.id.datePicker1);
        btnGet=(Button)findViewById(R.id.button1);
        btnGet.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tvw.setText("Selected Date: "  picker.getDayOfMonth() "/"  (picker.getMonth()   1) "/" picker.getYear());
            }
        });
    }
}

if still have issue, please comment.

  • Related