skip to Main Content

MainActivity.java

Spinner spinner = (Spinner) findViewById(R.id.spinner);

    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
            R.array.Door_array, android.R.layout.simple_spinner_item);

    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter);

    Button button = (Button)findViewById(R.id.Button1);
    button.setText(spinner.getSelectedItem().toString());

}

}

2

Answers


  1. Changing the text of button every time you must enter your code inside of this functions :

    String spinner_text = "";
    spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parentView, View 
    selectedItemView, 
    int position, long id) {
            spinner_text = parent.getItemAtPosition(position).toString();
            yourButton.settext(spinner_text);//Set text to button
    
        }
    
        @Override
        public void onNothingSelected(AdapterView<?> parentView) {
            // Set a default text on button
        }
    });
    
    Login or Signup to reply.
  2. try this

     ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.Door_array, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
    
        final Button button = findViewById(R.id.Button1);
        spinner.setOnItemSelectedListener(new OnItemSelectedListener(){
    
                @Override
                public void onItemSelected(AdapterView<?> p1, View p2, int p3, long p4){
                    button.setText(p1.getItemAtPosition(p3).toString());
                }
    
                @Override
                public void onNothingSelected(AdapterView<?> p1){}
            });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search