I want a Spinner which contains last Item as "Add more items"
and when i click on it, then i can add next item. The item i have added should get displayed in spinner list and will have same last item as "Add more items"..
I tried using Adapter but how can i keep last element as "Add more items"
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (position == spinner.getItemIdAtPosition(spinner.getCount()))
// my code for adding item to list using Adapter
else
// spinner.setSelection();
}
did i wrote anything wrong??
any help??..Thanks ...
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Spinner
android:id="#+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:dropDownVerticalOffset="2dp"
android:dropDownWidth="500sp"
android:spinnerMode="dropdown" />
</RelativeLayout>
MainActivity.java :
public class MainActivity extends Activity {
private Spinner spinner;
ArrayAdapter<String> adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ArrayList<String> data = new ArrayList<String>();
for (int i = 0; i < 5; i++) {
data.add("item " + i);
}
data.add("Add New Item");
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, data);
spinner = (Spinner) findViewById(R.id.spinner1);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
tv.setText(spinner.getSelectedItem().toString());
if (spinner.getSelectedItem().toString().equals("Add New Item")) {
data.remove(position);
data.add(position, "item " + position);
data.add((position + 1), "Add New Item");
updateAdapter(data);
Toast.makeText(getApplicationContext(), "item " + position,
Toast.LENGTH_LONG).show();
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
}
public void updateAdapter(ArrayList<String> data) {
adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1,
data);
adapter.notifyDataSetChanged();
}
}
Related
I was trying to create a spinner which display a list of data from array list.
When I clicked on the dropdown it displays the list, but when I clicked on an item inside the dropdown list it doesn't show up the value on the spinner.
Am I missing something here?
Note: Yesterday I have tried to check using Log.d() and System.out.println, itemOnSelected() doesn't but today it works fine. Maybe I rebuilding the project or I have changed something in the code but the value on the spinner still doesn't show up after I clicked on the item inside the spinner.
Spinner spnSubjectIDInfo;
ArrayList<String> subjectList;
ArrayAdapter<String> adpSubj;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
subjectList = new ArrayList<String>();
subjectList .add("John");
subjectList .add("Maxi");
subjectList .add("Jeni");
spnSubjectIDInfo = (Spinner) v.findViewById(R.id.spnSubjectIDInfo);
adpSubj = new ArrayAdapter<String>(MyActivity.this, android.R.layout.simple_spinner_item, subjectList);
adpSubj.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spnSubjectIDInfo.setAdapter(adpSubj);
spnSubjectIDInfo.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(this, parent.getItemAtPosition(position)+ " selected", Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
XML
<Spinner
android:id="#+id/spnSubjectIDInfo"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_marginTop="54dp"
android:layout_centerHorizontal="true" />
Try this
AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(ActivityName.this, subjectList.get(position).toString() " selected", Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
You are passing the wrong context to Toast instance:
If your code is in Activity then replace this by YourActivity.this or if it's in Fragment then use getActivity() or use Application context getApplicationContext(). Because currently, this is representing Spinner's onItemSelected listener context.
Do :
Toast.makeText(/*Your activity/application context*/, parent.getItemAtPosition(position)+ " selected", Toast.LENGTH_SHORT).show();
instead of:
Toast.makeText(this, parent.getItemAtPosition(position)+ " selected", Toast.LENGTH_SHORT).show();
Change the "this" to "ActvityName.this" or use "getApplicationContext()" in the Toast, you are passing anonymous class context in the toast.
Like below:
spnSubjectIDInfo.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(ActivityName.this,parent.getItemAtPosition(position)+ " selected", Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
Hope this will help you.
public class SpinnerTest extends AppCompatActivity {
private ArrayList<String> subjectList;
private ArrayAdapter<String> adpSubj;
private Spinner spinner;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_spinner_test);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
spinner= (Spinner) findViewById(R.id.spnSubjectIDInfo);
subjectList = new ArrayList<String>();
subjectList .add("John");
subjectList .add("Maxi");
subjectList .add("Jeni");
adpSubj = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, subjectList);
adpSubj.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adpSubj);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(), spinner.getItemAtPosition(position).toString() + " selected", Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}
in Spinner setOnItemSelectedListener methord not work. if you want to get selected value show in toast use this line
String Text = mySpinner.getSelectedItem().toString();
Toast.makeText(this,Text,Toast.LENGTH_LONG).show();
it will return the selected value and will be displayed on Toast.
After few more searching I realized that I have to set the width size of the spinner to wrap_content as the length of the value doesn't support to display it on the spinner after the item being clicked at.
<Spinner
android:id="#+id/spnSubjectIDInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="54dp"
android:layout_centerHorizontal="true" />
i got a spinner with some options, and i want to change visibility of text box when the specific "other" option get selected , how should i do this ?
this.arraySpinner = new String[] {"Mobile", "Gaming", "Media Features", "Medical", "Sports And Nutrition","Security","Space","Other"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, arraySpinner);
categories.setAdapter(adapter);
Try this:
.................
........................
this.arraySpinner = new String[] {"Mobile", "Gaming", "Media Features", "Medical", "Sports And Nutrition","Security","Space","Other"};
// Views
categories= (Spinner) findViewById(R.id.spinner);
textView = (TextView) findViewById(R.id.textView);
// Adapter
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, arraySpinner);
categories.setAdapter(adapter);
// Set onItemSelected listener to spinner
categories.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
TextView spinnerText = (TextView) view;
// Checking weather selected item is "Other" or not
if (spinnerText.getText().equals("Other"))
{
// Hide TextView
textView.setVisibility(View.INVISIBLE); // or View.GONE
}
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
.............
...................
Hope this will help
you can Implement OnItemSelected in the spinner and do the things what you want.
categories.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
if(arraySpinner!=null && arraySpinner[position].equalIgnoreCase("Other")){
// your code here
}
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});
I am new in android ,I want work like, In Spinner if i select any other items form array,that item should get selected including with some text,For example,if My spinner having 10 items,if i select 6th item that item should display like "Your Item Is: Item6",This should be display in my spinner as first item,After select of items form the array,Please help me ,,Thanks in advance..
String[] categories = {"Select one","Automobile","Computers","Education"};
// Creating adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories);
// attaching data adapter to spinner
spinner.setAdapter(dataAdapter);
then onItemSelected Listerner Do :-
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// On selecting a spinner item
String item = parent.getItemAtPosition(position).toString();
categories[0] = item;
ArrayAdapter<String> newdataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories);
spinner.setAdapter(newdataAdapter);
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
ArrayAdapter arrayAdapter = null;
String[] list=null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//you can replace the below list with your list
list = {"Mercury","Venus","Earth","Mars","Jupiter","Saturn","Uranus","Neptune","Pluto"};
final Spinner spinner = (Spinner) findViewById(R.id.spinner);
arrayAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
spinner.setAdapter(arrayAdapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
spinner.setPrompt("Your selected item is:"+list[position]);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
I have add two spinners in my code. But bydefault the item of the spinner is displayed inblack color. I want to change the color of the spinner item.I have add the color in simple_spinner_item.xml file. but color is not changed. I have pasted my code here.plz help me...
public class Alert_activity extends Activity implements OnItemSelectedListener{
Spinner s1,s2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.alert_activity);
s1 = (Spinner)findViewById(R.id.spinner1);
s2 = (Spinner)findViewById(R.id.spinner2);
s1.setOnItemSelectedListener(this);
}
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
String sp1= String.valueOf(s1.getSelectedItem());
if(sp1.contentEquals("Events")) {
List<String> list = new ArrayList<String>();
list.add("Heavy Rain(mm/hr)");
list.add("Smoke/Fog");
list.add("Heat/Cold Wave");
list.add("Strong Winds");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dataAdapter.notifyDataSetChanged();
s2.setAdapter(dataAdapter);
s2.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapter, View v,
int position, long id) {
// On selecting a spinner item
String item = adapter.getItemAtPosition(position).toString();
Log.d("Item",item);
FragmentTransaction tx = getFragmentManager() .beginTransaction();
if(item=="Heavy Rain(mm/hr)")
{
tx.replace(android.R.id.content, new heavyrain());
tx.commit();
}
});
}
if(sp1.contentEquals("Forecast")) {
List<String> list = new ArrayList<String>();
list.add("Heavy Rain(mm/hr)");
list.add("Cyclone");
list.add("Cloudburst");
ArrayAdapter<String> dataAdapter2 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dataAdapter2.notifyDataSetChanged();
s2.setAdapter(dataAdapter2);
s2.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapter, View v,
int position, long id) {
String item = adapter.getItemAtPosition(position).toString();
Log.d("Inside loop","inside loop");
FragmentTransaction tx = getFragmentManager() .beginTransaction();
if(item=="Cyclone")
{
tx.replace(android.R.id.content, new Cyclone());
}
else if(item=="Heavy Rain(mm/hr)")
{
tx.replace(android.R.id.content, new forecast_heavyrain());
}
else if(item=="Cloudburst")
{
tx.replace(android.R.id.content, new forecast_cloudburst());
}
tx.commit();
}
});
}
}
simple_spinner_item.xml
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
style="?android:attr/spinnerItemStyle"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:textAlignment="inherit"
android:textColor="#FFFFFF"/>
private OnItemSelectedListener OnCatSpinnerCL = new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView parent, View view, int pos,
long id) {
((TextView) parent.getChildAt(0)).setTextColor(Color.BLUE);
((TextView) parent.getChildAt(0)).setTextSize(5);
}
public void onNothingSelected(AdapterView<?> parent) {
}
};
or refer this link:How to change spinner text size and text color?
if your simple_spinner_item.xml contains the background color for your spinner item then use
dataAdapter.setDropDownViewResource(R.layout.simple_spinner_item);
instead of dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
similarly,
dataAdapter.setDropDownViewResource(R.layout.simple_spinner_item); instead of dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
you might also have to change the layout set in the ArrayAdapter to a custom layout to solve the issue.
I have had a good search around and can't seem to find an answer. The method onItemSelected is not being called. Here's the code featured in my activity's onCreate method:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_trips);
try {
// if network present.. TODO
// Load Devices
deviceList = new ArrayList<Device>();
DataAsyncTask loadDevices = new DataAsyncTask(0);
loadDevices.execute(someUrl);
List<Device> deviceListNames = deviceList;
final ListView tripListView = (ListView)findViewById(R.id.trip_list);
Spinner deviceSpinner = (Spinner) findViewById(R.id.select_device_spinner);
ArrayAdapter<Device> deviceAdapter = new ArrayAdapter<Device>(this, android.R.layout.simple_spinner_dropdown_item, deviceListNames);
deviceAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
deviceSpinner.setAdapter(deviceAdapter);
OnItemSelectedListener spinnerListener = new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int i, long id) {
Log.d(TAG, "select is: " + parent.getSelectedItem().toString());
DataAsyncTask loadTrips = new DataAsyncTask(1);
loadTrips.execute(someUrl);
ArrayAdapter<Trip> tripAdapter = new ArrayAdapter<Trip>(getApplication(),
android.R.layout.simple_list_item_1, tripList);
tripListView.setAdapter(tripAdapter);
tripListView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int i, long id) {
Log.d(TAG, parent.getSelectedItem().toString());
Intent intent = new Intent(getApplication(), TripSummary.class);
//put value of tripId to intent TODO
startActivity(intent);
}
});
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO
Log.d(TAG, "nothing selected");
}
};
deviceSpinner.setOnItemSelectedListener(spinnerListener);
} catch (Exception e){
Log.e(TAG, e.getLocalizedMessage(), e);
}
}
Here's the activity's layout 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"
tools:context=".ViewTrips" >
<Spinner
android:id="#+id/select_device_spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:prompt="#string/select_device"
android:layout_marginTop="5dp"
android:layout_marginLeft="5dp"
android:drawSelectorOnTop = "true" />
<ListView
android:id="#+id/trip_list"
android:layout_below="#id/select_device_spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/trips"
android:clickable="true" />
</RelativeLayout>
It's values are populated and displaying correctly in the Spinner. I am just having an issue with the onItemSelected method in the OnItemSelectedListener() which is set to deviceSpinner - this method is then supposed to populate the ListView, where once clicked takes the user to a new activity. Can anyone explain what I am doing wrong?
I have also tried implementing OnItemSelectedListener for the activity, which doesn't work either.
Note: There's no errors to report in LogCat,
Log.d(TAG, "select is: " + parent.getSelectedItem().toString());
is not printing.
Thanks