Reputation: 12375
I want to let the user the possibility to delete the items shown in dropdown list of the spinner if clicks on the "X" button on the right side of each item, see the sample below.
If the user click on the item the Spinner should act like the classic Android default Spinner.
How should I extend and re-define the Spinner
to work in this way?
Upvotes: 2
Views: 2010
Reputation: 849
I think you are looking for this:
MainActivity:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spnView3 = (Spinner) findViewById(R.id.spnView3);
ArrayList<String> aList = new ArrayList<String>();
aList.add("--Select--");
aList.add("Md. Shahadat Sarker");
aList.add("Developer");
aList.add("ErrrorPoint");
spnView3.setAdapter(new SpinnerAdapter(this, R.layout.spinner_row, aList, aList));
Toast.makeText(this, "Selected: " + spnView3.getSelectedItem(), 500).show();
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Spinner
android:id="@+id/spnView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left" />
</RelativeLayout>
spinner_row.xml (Layout):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="9"
android:orientation="horizontal" >
<TextView
android:id="@+id/spnItemName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="9"
android:text="Item Name"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="bold" />
<TextView
android:id="@+id/spnItemDel"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="X"
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
</LinearLayout>
Adapter:
public View getCustomView(final int position, View convertView, ViewGroup parent){
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
View rowView = inflater.inflate(R.layout.spinner_row, parent, false);
spnItemName = (TextView) rowView.findViewById(R.id.spnItemName);
spnItemDel = (TextView) rowView.findViewById(R.id.spnItemDel);
spnItemName.setText(iName.get(position)+"");
spnItemDel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, "Deleted", Toast.LENGTH_SHORT).show();
//iName[position] = null;
iName.remove(position);
notifyDataSetChanged();
}
});
return rowView;
}
Full example is here... download
Edited Part:
if( /*Your condition goes here*/ ){
spnItemName.setVisibility(View.GONE);
} else{
spnItemName.setVisibility(View.VISIBLE);
}
Upvotes: 1