I am trying to add new spinner below old one dynamically with click of a button. Also, I am adding button on the side of spinner dynamically. My problem is the spinners and buttons are added on top of each other. How can I add new spinner at the bottom of last one with every button click? Here is my code:

public class MainActivity extends FragmentActivity implements OnClickListener{
SampleAlarmReceiver alarm = new SampleAlarmReceiver();
static EditText startTime;
static EditText endTime;
static EditText startDate;
static EditText EndDate;
View buttonRem;
Spinner spinnerNew;

@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); 

    //Set up Click Listener for all Buttons
    View buttonAdd = findViewById(R.id.button1);
    buttonAdd.setOnClickListener(this);

}

public void onClick(View v)
{
    switch(v.getId()){
    case (R.id.button1):

        RelativeLayout rootLayout = (RelativeLayout)findViewById(R.id.main_layout);

        //use layout inflater to create 'spinnerNew' on the fly
        final LayoutInflater spnInflater=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        spinnerNew = (Spinner)spnInflater.inflate(R.layout.extra_spinner, null);
        rootLayout.addView(spinnerNew);

        //move the dynamic spinner to different position
        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)spinnerNew.getLayoutParams();
        params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
        params.addRule(RelativeLayout.BELOW, R.id.spinner1);

        spinnerNew.setLayoutParams(params); //causes layout update

        //use layout inflater to create 'remove button' on the fly
        final LayoutInflater btnInflater=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        buttonRem = (Button)btnInflater.inflate(R.layout.btn_remove, null);
        rootLayout.addView(buttonRem);

        //move the dynamic button to different position
        RelativeLayout.LayoutParams params1 = (RelativeLayout.LayoutParams)buttonRem.getLayoutParams();
        params1.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        params1.addRule(RelativeLayout.ALIGN_BOTTOM, R.id.spinnerNew);
    break;

    case(R.id.buttonRem):
        //spinnerNew.setVisibility(View.GONE);
        //buttonRem.setVisibility(View.GONE);
        ViewGroup layout = (ViewGroup) spinnerNew.getParent();
        if(null!=layout) 
           layout.removeView(spinnerNew);
    break;
    }
}

i am an android begginer but i would suggest that u might need to adjust the layout(which means spacing between the new spinner (created from a parent spinner) and the parent spinner and also u must change the distance between the newly created spinner and button)....

Why would you want to do such horrible think as on button click add another spinner? That is how websites works when you are searching for a product for example. In Android we do reuse views when need it and this is excellent example.

Let user select whatever he wants, save selection in sharedprefferences or other storage of your choice. Fetch new set of data, clean adapter and push new data to spinner.Job done!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.