Hi I have created a layout which contains two buttons add and remove, when i click on the plus button it will add a spinner pro-grammatically and i have another button (remove) with - sign as you can see in the picture , now i want when i click to the remove button it will delete the spinner from last, please guide me how can i delete the spinner? i have add the spinner pro-grammatically and it is creating spinner pro-grammatically. i'll post my code.
public class AddMenulist extends Fragment{
TextView tv1,tv2,tv3,tv4;
Button bt1 , bt2;
LinearLayout linearlayout,linearlayout1;
ArrayList<String> spinnerArray = new ArrayList<String>();
private Spinner spinner;
private Spinner spinner2;
public AddMenulist(){
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_pages, container, false);
tv1 = (TextView)rootView.findViewById(R.id.userName);
tv2 = (TextView)rootView.findViewById(R.id.userName2);
bt1 = (Button)rootView.findViewById(R.id.add);
bt2 = (Button)rootView.findViewById(R.id.delete);
linearlayout = (LinearLayout)rootView.findViewById(R.id.main_layout);
spinnerArray.add("one");
spinnerArray.add("two");
spinnerArray.add("three");
spinnerArray.add("four");
spinnerArray.add("five");
bt1.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
//Toast.makeText(getActivity(), "Test", Toast.LENGTH_LONG).show();
linearlayout1 = new LinearLayout(getActivity());
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
linearlayout1.setOrientation(LinearLayout.HORIZONTAL);
layoutParams.setMargins(10, 0, 0, 0);
linearlayout1.setLayoutParams(layoutParams );
tv3 = new TextView(getActivity());
tv3.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
tv3.setText("Label");
tv4 = new TextView(getActivity());
tv4.setText("Category");
spinner = new Spinner(getActivity());
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_dropdown_item, spinnerArray);
spinner.setAdapter(spinnerArrayAdapter);
spinner2 = new Spinner(getActivity());
ArrayAdapter<String> spinnerArrayAdapter2 = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_dropdown_item, spinnerArray);
spinner2.setAdapter(spinnerArrayAdapter2);
linearlayout1.addView(tv3);
linearlayout1.addView(spinner);
linearlayout1.addView(tv4);
linearlayout1.addView(spinner2);
linearlayout.addView(linearlayout1);
}
});
bt2.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
//Toast.makeText(getActivity(), "Test", Toast.LENGTH_LONG).show();
}
});
return rootView;
}
}
Use this yourSpinner.setVisibility(View.GONE);