I want to create a Spinner with multiple choice, and every one that chooses, must add to the spinner box.my spinner has a dropDown List.pictures below will describe what I want to create:
how can I create this Spinner???
I want to create a Spinner with multiple choice, and every one that chooses, must add to the spinner box.my spinner has a dropDown List.pictures below will describe what I want to create:
how can I create this Spinner???
try this you can use TextView.append() method.
Convenience method to append the specified text slice to the TextView's display buffer, upgrading it to EDITABLE if it was not already editable.
sample code
textView = (TextView) findViewById(R.id.text);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
String s=destspin1.getSelectedItem().toString();
textView.append(s);
/* or try this
String str=textView.getText().toString().trim();
textView.setText(str+s);*/
}
@Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});
I have create a full code for you just apply
XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_test_code"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
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="com.ncrypted.thumbpin.TestCode">
<Spinner
android:layout_marginTop="20dp"
android:id="@+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="dsasas"
android:textColor="@color/black"
android:textSize="20dp" />
</LinearLayout>
JAVA
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import java.util.ArrayList;
public class TestCode extends AppCompatActivity {
Spinner spinner;
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_code);
textView = (TextView) findViewById(R.id.text);
spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, new String[]{"String 1", "String 2", "String 3", "String 4", "String 5"});
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
String s = spinner.getSelectedItem().toString();
textView.append(s);
}
@Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});
}
}
NOTE: if you want to add particular item at only once than you should have to use ArrayList to store selected items in to array list and loop the array and check the item is already added or not