0

I'm new to Android Studio, any help would be greatly appreciated.

This is not the mainActivity but another page called StudentReg1 and branch refers to the spinner id as well as as string array in strings.xml

public class StudentReg1 extends AppCompatActivity { @Override

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.student_reg1);}
    Spinner mySpinner = (Spinner) findViewById(R.id.branch);
    ArrayAdapter<String> myAdapter = new ArrayAdapter<String>(StudentReg1.this,android.R.layout.simple_list_item_1, getResources().getStringArray(R.array.branch));
    myAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    mySpinner.setAdapter(myAdapter);





}

setDropDownViewResource and setAdapter appear in red, I don't seem to understand the issue.

Jmjcode
  • 11
  • 2

2 Answers2

0

I think you need to use:

ArrayAdapter<String> myAdapter = ArrayAdapter.createFromResource(StudentReg1.this,R.array.branch,android.R.layout.simple_spinner_item);
mehul bisht
  • 682
  • 5
  • 15
0

hey buddy use this easy library to make spinner

implementation 'com.jaredrummler:material-spinner:1.3.1'

 <com.jaredrummler.materialspinner.MaterialSpinner
            android:id="@+id/newAnnouncementSpinner1"
            android:layout_marginHorizontal="25dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/select_type"/>

you can use the above code for your xml code and below for java code

private MaterialSpinner newAnnouncementSpinner1

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    newAnnouncementSpinner1=view.findViewById(R.id.newAnnouncementSpinner1);
    List<String> categories= new ArrayList<>();
    categories.add("string one");
    categories.add("string two");
    categories.add("string three");
    categories.add("string four");
    categories.add("string five");
    categories.add("string six");
    
    newAnnouncementSpinner1.setItems(categories);

}