How to delete item from spinner added via entries in layout xml? - android

I have this array in my strings.xml :
<string-array name="lst_addressTypes">
<item>FISCAL</item>
<item>NAP</item>
<item>SUCURSAL</item>
<item>ALMACEN</item>
<item>OFICINA</item>
<item>OTRO</item>
</string-array>
And I have this Spinner in my layout XML:
<Spinner
android:id="#+id/cboTipoDireccion"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="#array/lst_addressTypes" />
And I want to delete some items programmatically.
I tried to do this like:
spinnerObject.removeViewAt(0)
but this threw an `InvalidOperationException

You can add String[] or ArrayList<String> for entries in your activity.
Adding:
List<String> entriesList = new ArrayList<>();
// add items into spinner dynamically
public void addItemsOnSpinner() {
String[] entries = getResources().getStringArray(R.array.lst_addressTypes);
entriesList = new ArrayList<String>(Arrays.asList(entries));
Spinner spinner = (Spinner) findViewById(R.id.cboTipoDireccion);
ArrayAdapter spinnerAdapter = new ArrayAdapter(this,android.R.layout.simple_spinner_item, entriesList);
spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(spinnerAdapter);
}
Removing:
entriesList.remove(0);
spinnerArrayAdapter.notifyDataSetChanged();

Related

Android: Align items and title/prompt in "Dialog" mode Spinner

I have a spinner that's populated like this:
Spinner spinner = (Spinner) findViewById(R.id.m_spinner);
final ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(mContext, android.R.layout.simple_spinner_item, spinnerValuesList);
spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(spinnerAdapter);
spinner.setPrompt("CPU Frequency Governor");
It's created like this:
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:spinnerMode="dialog"
android:id="#+id/my_spinner"/>
Everything works fine, but I'd like to align the text of the dialog title and the spinner items. I attached a picture to show what it currently looks like and what I'm looking to achieve.
Thanks
Instead of
final ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(mContext, android.R.layout.simple_spinner_item, spinnerValuesList);
Try this:
final ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(mContext, android.R.layout.simple_list_item_1
, spinnerValuesList);
Let me know if it works for you.

Android : How can i Customize Spinner items?

This is my Spinner with Package names of Installed apps as ArrayList. It works fine but the text on spinner items appear white and the text is not visible untill focused. I need to set the item color to black, how can i?
public class AppList extends Activity
{
private Spinner spinner;
private ArrayList results = new ArrayList();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate( savedInstanceState);
setContentView(R.layout.main);
spinner = (Spinner)findViewById(R.id.Button);
PackageManager pm = AppList.this.getPackageManager();
Intent intent = new Intent( Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
List list = pm.queryIntentActivities(intent,
PackageManager.PERMISSION_GRANTED);
for (ResolveInfo rInfo : list)
{ results.add(rInfo.activityInfo.packageName.toString());
}
spinner.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, results));
}}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, array);
adaptersetDropDownViewResource(R.layout.your_layout);
spinner.setAdapter(adapter);
Make a layout xml with only textview:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textcolor="#android:color/black"/>
Extend the adapter, and customize the view in getView and getDropdownView.
Edit your simple_list_item_1.xml file
Add this your textview;
android:textColor="#000000"
ArrayAdapter<String> adap = new ArrayAdapter<String> (this,
android.R.layout.simple_
spinner_item, your_array );
adap.setDropDownViewResource(R.layout.row);
spinner.setAdapter(adap);
Now Customize the text in row.xml

where to declare array of items for the spinner

I have been going through spinners for my application but I don't know where to declare the items that are to be shown in the spinner i.e either I declare them in the string.xml or in my main_activity like this ..
String[] data = { "Hello", "There", "I", "Am", "Taking", "Values" };
Which method is best to use and why?
After declaring values in resources tag in strings.xml like below:
<string-array name="spinner_values">
<item>a</item>
<item>b</item>
</string-array>
you can use entries attribute in spinner tag in your xml layout, instead of use it pro-grammatically in java file. Like below:
<Spinner
android:id="#+id/my_spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="#array/spinner_values" />
Declaring the array in string.xml is better because:
If you want to translate your app into another language, you just
need this one file which has all the arrays and strings that your app uses.
If you have to make any changes to any string/array, you know by default where to look for(All at one place).
Re-use is possible using this way. In any other activity you can just use from there, instead of declaring again.
you can try in this way ....
public void addItemsOnSpinner1() {
spinner1 = (Spinner) findViewById(R.id.spinner1);
List<String> list = new ArrayList<String>();
list.add("Small");
list.add("Medium");
list.add("Large");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(dataAdapter);
}
public void addListenerOnSpinnerItemSelection() {
spinner1 = (Spinner) findViewById(R.id.spinner1);
apply = (Button) findViewById(R.id.apply);
spinner1.setOnItemSelectedListener(new CustomOnItemSelectedListener());
apply.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(String.valueOf(spinner1.getSelectedItem()).equals("Small"))
{
// your code
}
if(String.valueOf(spinner1.getSelectedItem()).equals("Medium"))
{
// your code
}
else if(String.valueOf(spinner1.getSelectedItem()).equals("Large"))
{
// your code
}
}
});
If content of your spinner is fixed then declare it in String.xml . its best practice
You should declare array in strings.xml
<string-array name="data">
<item>Hello</item>
<item>There</item>
<item>I am</item>
<item>Taking</item>
</string-array>
and obtain values in activity
String [] array =getResources().getStringArray(R.array.data);

How to Implements multiple spinner with different item list and different action on click in the same Activity

I want to implement two different spinner in Android, the spinner have different data set
This is the spinner with the age, that uses a defined String array with all age ranges (es 18-20, 19-21 etc.)
<Spinner
android:id="#+id/spAge"
android:layout_width="match_parent"
android:layout_height="35dp"
android:entries="#array/age_array"
tools:listitem="#android:layout/simple_spinner_item/>
And this is the spinner with the sex, that show only the two items Male and Female
<Spinner
android:id="#+id/spSex"
android:layout_width="match_parent"
android:layout_height="35dp"
android:entries="#array/sex_array"
tools:listitem="#android:layout/simple_spinner_item />
For each selected item the my activity should set the associated selected items values to the two Objects:
String selectedAge;
String selectedItem;
The sample that I have seen doesn't contains multiple spinner with different items set and different actions on item selected, and I don't know how to solve the problem.
write your Code as below to do different actions on item selected.
spinner1.setOnItemSelectedListener(this);
spinner2.setOnItemSelectedListener(this);
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
switch(arg0.getId()){
case R.id.spAge :
//Your Action Here.
break;
case R.id.spSex :
//Your Another Action Here.
break;
}
}
Try this
ArrayAdapter<CharSequence> adapterAge;
ArrayAdapter<CharSequence> adapterSex;
String[] AgeArr = {"18-20", "19-21"};
String[] sexArr = {"male", "female"};
Spinner ageDrp =(Spinner)findViewById(R.id.spAge);
Spinner sex1Drp =(Spinner)findViewById(R.id.spSex);
adapterAge = new ArrayAdapter<CharSequence>(this,android.R.layout.simple_spinner_item,AgeArr);
adapterAge.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
ageDrp.setAdapter(adapterAge);
adapterSex= new ArrayAdapter<CharSequence>(this,android.R.layout.simple_spinner_item,sexArr);
adapterSex.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sexDrp.setAdapter(adapterSex);
String selectedAge = ageDrp.getSelectedItem().toString();
String selectedSex = sexDrp.getSelectedItem().toString();
System.out.println(selectedAge+" "+selectedSex);// check the output in logcat
Try This method
spinner1 = (Spinner) findViewById(R.id.spinner);
spinner2 = (Spinner) findViewById(R.id.highschoolspinner);
List<String> categories = new ArrayList<String>();
categories.add("Qualification");
categories.add("High School");
categories.add("Higher Secondary/PUC");
categories.add("Diploma");
categories.add("Degree");
categories.add("Master Degree");
List<String> list = new ArrayList<String>();
list.add("Plus One");
list.add("Plus Two");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.spinnertext, categories);
adapter.setDropDownViewResource(android.R.layout.select_dialog_item);
spinner.setAdapter(adapter);
ArrayAdapter<String> dataAdapter12 = new ArrayAdapter<String>(this,
R.layout.spinnertext, list);
dataAdapter12.setDropDownViewResource(android.R.layout.
simple_spinner_dropdown_item);
dataAdapter12.notifyDataSetChanged();
highschool.setAdapter(dataAdapter12)
Am used custom adapter to load data you can use built in adapter like
ArrayAdapter dataAdapter11 = new ArrayAdapter(this,
android.R.layout.simple_list_item_1, list);

How to fill a ListView with a string-array?

I want to show these itens at my ListView
<string-array name="bookmark_titles">
<item>Google</item>
<item>Bing</item>
<item>Gmail</item>
</string-array>
I have a method that gets these values.
public static Collection getBookmarks(Context context) {
Collection bookmarks = new Collection();
String[] titles = context.getResources().getStringArray(R.array.bookmark_titles);
for (int i = 0; i < titles.length; i ++) {
bookmarks.add(titles[i]);
}
return bookmarks;
}
How can I call the method getBookmarks in my main.java to fill the ListView?
I have already created a ListView. It is:
<ListView
android:id="#+id/my_listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="#ECECEC"
android:dividerHeight="1sp" />
main.java
I am trying to do something like this:
ListView lv = (ListView) findViewById(R.id.my_listView);
ArrayList<Bookmark> my_array = BookmarkCollection.getTestBookmarks(context);
adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, my_array);
lv.setAdapter(adapter);
You can create your adapter with a one liner, check out the static method ArrayAdapter createFromResource(Context context, int textArrayResId, int textViewResId)
The first argument will probably be your Activity, the second is R.array.bookmark_titles, and the third is a layout to use.
To clarify based on the comments, the method accepts int, which is exactly what the constants in your generated R class are stored as.
Here's a complete example, assuming this is being called from an Activity:
ArrayAdapter<CharSequence> aa = ArrayAdapter.createFromResource(this, R.array.bookmark_titles, android.R.layout.simple_list_item_1);
myListView.setAdapter(aa);
In this case, android.R.layout.simple_list_item_1 refers to an XML layout that is provided by the Android SDK. You can change this if needed.
You can do this with an ArrayAdapter and have it use either an Array or a List to give it data
lv.setAdatper(new ArrayAdapter<String>(context, R.layout.some_layout_to_use, R.id.some_textview_in_layout, listData);

Categories

Resources