I am trying to add a spinner dynamically to a RelativeLayout but I am having problems. This is my code.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="@+id/Relativ"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
</RelativeLayout>
And in my class:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout rl = (RelativeLayout) findViewById(R.id.Relativ);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,1);
adapter.add("1");
adapter.add("2");
adapter.add("3");
adapter.add("4");
adapter.add("5");
Spinner list = new Spinner(this);
list.setAdapter(adapter);
rl.addView(list);
}
But it does't run. I would like to know what is wrong in my code and how I can fix it. I have tryed with RelativeLayout params in the RelativeLayout.addView and it doesn't run. Thank you.