Add spinner with click of a button dynamically in Android
Solution 1
This is what You have done
- Added a new LinearLayout.
- Added a spinner to the above layout.
- And you set the content view.
This is what I think will work.
- You get the root layout using
findViewById(R.id.root_layout)
. - Now create a spinner.Add items to it as you did correctly above in your code.
- Now add the spinner to your
root_layout
.
Your Mistake
You made a new layout and set it as the main content and you never added it to the root_layout.
Debugging
If you want to see what layout hierarchy your layout has.Open ddms view and click on the heirarchy Viewer option there.Its located near the camera option.Click it and get the screen and you will see your layout hierarchy which will display a linearLayout containing a spinner.
Here's a code to demonstrate it.
package com.example.testproject;
import android.app.Activity; import android.os.Bundle; import android.widget.ArrayAdapter; import android.widget.LinearLayout; import android.widget.RelativeLayout; import android.widget.Spinner;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/**
* This is what you have done!!!
*/
LinearLayout layout=new LinearLayout(this);
String[] strings={"1","2","3"};
Spinner spinner=new Spinner(this);
spinner.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item,strings));
layout.addView(spinner);
/**
* Comment this line when you use the below mentioned solution.
*/
setContentView(layout);
/**
* This is what you should have done.
*/
RelativeLayout root_layout=(RelativeLayout)findViewById(R.id.root_layout);
root_layout.addView(spinner);
}
}
Hope it helps you.
Solution 2
Mistake: You created new linear layout(which dose not contain any view) and added spinner to it then repaced your activity_main layout with this layout which only contains spinner.
Easy way is to make Spinner invisible initially and make it visible when button is clicked
So declare spinner spinner visibility="gone" in XML
<Spinner
android:id="@+id/my_spin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
/>
In Oncreate
spinner = (Spinner) findViewById(R.id.my_spin);
Now make spinner visible on Click of button
timer_spin.setVisibility(View.VISIBLE);
Note:
GONE:This view is invisible, and it doesn't take any space for layout purposes.
INVISIBLE:This view is invisible, but it still takes up space for layout purposes
VISIBLE:This view is visible.
Solution 3
Could you post your activity_main.xml
file located in the res/layout/
folder of your project.
In any case, you should be adding your spinner to your main_layout, not to some new linearlayout not connected to anything.
And below is what the solution should look like
(although, this will only work depending on what's actually written in your activity_main.xml
file):
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startTime = (EditText)findViewById(R.id.EditTextST);
startDate = (EditText)findViewById(R.id.editTextSD);
// The line below assumes that the layout called main_layout
// found in the res/layout/activity_main.xml is a LinearLayout
LinearLayout mainLayout = (LinearLayout)findViewById(R.id.main_layout);
ArrayList<String> spinnerArray = new ArrayList<String>();
spinnerArray.add("one");
spinnerArray.add("two");
spinnerArray.add("three");
spinnerArray.add("four");
spinnerArray.add("five");
Spinner spinner = new Spinner(this);
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item, spinnerArray);
spinner.setAdapter(spinnerArrayAdapter);
mainLayout.addView(spinner);
}
After you correct this mistake, I'd suggest you study a little bit Java and do some Java exercises for at least twenty minutes a day during three or four weeks. Once you know a little bit more of Java, you'll be able to correct mistakes such as these very easily.
![Gantavya](https://i.stack.imgur.com/815Hj.png?s=256&g=1)
Gantavya
Updated on June 28, 2022Comments
-
Gantavya about 2 years
I was trying to add spinner to my app dynamically by using the following code:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); startTime = (EditText)findViewById(R.id.EditTextST); startDate = (EditText)findViewById(R.id.editTextSD); LinearLayout linearlayout = new LinearLayout(this); //RelativeLayout linearlayout = (RelativeLayout)findViewById(R.id.main_layout); ArrayList<String> spinnerArray = new ArrayList<String>(); spinnerArray.add("one"); spinnerArray.add("two"); spinnerArray.add("three"); spinnerArray.add("four"); spinnerArray.add("five"); Spinner spinner = new Spinner(this); ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item, spinnerArray); spinner.setAdapter(spinnerArrayAdapter); linearlayout.addView(spinner); setContentView(linearlayout); }
This adds the spinner but replaces everything I have in my XML with the dynamically-created spinner. I want to add the spinner to the layout (which I have created by using XML) by clicking a button, not replacing the layout. Please help.
EDIT: Here is my XML file
<?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" > <RelativeLayout android:id="@+id/main_layout" android:layout_width="match_parent" android:layout_height="900dp" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:text="@string/select_week" android:textAppearance="?android:attr/textAppearanceMedium" /> <CheckBox android:id="@+id/checkBox1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/textView1" android:text="@string/sun" /> <CheckBox android:id="@+id/checkBox2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/checkBox1" android:layout_alignBottom="@+id/checkBox1" android:layout_toRightOf="@+id/checkBox1" android:text="@string/mon" /> <CheckBox android:id="@+id/checkBox3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/checkBox2" android:layout_toRightOf="@+id/checkBox2" android:text="@string/tue" /> <CheckBox android:id="@+id/checkBox4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/checkBox3" android:layout_alignBottom="@+id/checkBox3" android:layout_toRightOf="@+id/checkBox3" android:text="@string/wed" /> <CheckBox android:id="@+id/checkBox5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/checkBox1" android:layout_toLeftOf="@+id/checkBox2" android:text="@string/thu" /> <CheckBox android:id="@+id/checkBox6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/checkBox2" android:layout_below="@+id/checkBox2" android:text="@string/fri" /> <CheckBox android:id="@+id/checkBox7" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/checkBox2" android:layout_toRightOf="@+id/checkBox2" android:text="@string/sat" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/checkBox5" android:layout_marginTop="18dp" android:text="@string/select_week_number" android:textAppearance="?android:attr/textAppearanceMedium" /> <CheckBox android:id="@+id/CheckBox01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/checkBox02" android:layout_alignBottom="@+id/checkBox02" android:layout_alignLeft="@+id/checkBox5" android:text="@string/one" /> <CheckBox android:id="@+id/CheckBox03" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/checkBox02" android:layout_alignBottom="@+id/checkBox02" android:layout_toRightOf="@+id/checkBox6" android:text="@string/three" /> <CheckBox android:id="@+id/checkBox02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/textView2" android:layout_toRightOf="@+id/checkBox5" android:text="@string/two" /> <CheckBox android:id="@+id/CheckBox04" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/CheckBox03" android:layout_alignBottom="@+id/CheckBox03" android:layout_toRightOf="@+id/textView2" android:text="@string/four" /> <TextView android:id="@+id/TextView01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/CheckBox01" android:layout_marginTop="16dp" android:text="@string/select_start_date" android:textAppearance="?android:attr/textAppearanceMedium" /> <EditText android:id="@+id/editTextSD" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/CheckBox01" android:layout_below="@+id/TextView01" android:layout_toLeftOf="@+id/CheckBox04" android:ems="10" android:inputType="text|date" android:onClick="showDatePickerDialog" > <requestFocus /> </EditText> <EditText android:id="@+id/EditTextST" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/editTextSD" android:layout_alignBottom="@+id/editTextSD" android:layout_alignLeft="@+id/CheckBox04" android:ems="10" android:inputType="time" android:onClick="showTimePickerDialog" /> <TextView android:id="@+id/TextView02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/editTextSD" android:layout_below="@+id/editTextSD" android:layout_marginTop="17dp" android:text="@string/select_end_date" android:textAppearance="?android:attr/textAppearanceMedium" /> <EditText android:id="@+id/EditText01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/TextView02" android:layout_alignRight="@+id/editTextSD" android:layout_below="@+id/TextView02" android:ems="10" android:inputType="text|date" android:onClick="showDatePickerDialog" /> <EditText android:id="@+id/EditText02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/EditText01" android:layout_alignBottom="@+id/EditText01" android:layout_alignLeft="@+id/EditTextST" android:ems="10" android:inputType="time" android:onClick="showTimePickerDialog" /> <TextView android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/EditText01" android:layout_below="@+id/EditText01" android:layout_marginTop="17dp" android:text="@string/reminder" android:textAppearance="?android:attr/textAppearanceMedium" /> <Spinner android:id="@+id/spinner1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/textView3" android:entries="@array/reminder_times" android:paddingLeft="-10dp" android:gravity="left" tools:listitem="@android:layout/activity_list_item" /> <Button android:id="@+id/button1" style="?android:attr/buttonStyleSmall" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/textView3" android:layout_below="@+id/spinner1" android:layout_marginTop="119dp" android:text="@string/add_reminder" /> </RelativeLayout> </ScrollView>
-
Gantavya over 10 yearsit works but the new spinner is added to the top of the layout. How can I add it to the bottom of the layout. I will edit my post to include my XML file.
-
Gantavya over 10 yearsit adds the spinner to the top of the layout. How can I add the spinner right before "button1" button of my XML?
-
cafebabe1991 over 10 yearsuse your container layout as RelativeLayout and set the following property to the spinner : android:layout_alignLeft="@+id/your_button"
-
Gantavya over 10 yearsI was asking about the dynamically created spinner. When I add the code
android:layout_alignLeft="@+id/button1;
, I get the errorlayout_alignleft cannot be resolved to a variable
. -
cafebabe1991 over 10 yearsNo the above thing goes in the xml,but you are doing that dynamically so you should get the layout params for the root_layout and set the rule to it.The setRule method takes the align_left (verb) and second argument is anchor(to left of which you want something).. RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)button.getLayoutParams(); params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); params.addRule(RelativeLayout.LEFT_OF, R.id.id_to_be_left_of); button.setLayoutParams(params); //causes layout update