Android : How can i Customize Spinner items? - android

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

Related

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

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();

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.

Spinner shows empty when use fragment

I have a spinner in normal activity and a wants to try to use spinner in fragment but in the fragment, shows empty when run it
example
My code in onCreateView
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View vista =inflater.inflate(R.layout.fragment_pag1,container,false);
calcular= (Button)vista.findViewById(R.id.button);
etd=(EditText)vista.findViewById(R.id.editText);
resultadocp=(TextView)vista.findViewById(R.id.textView3);
lista = (Spinner)vista.findViewById(R.id.spinner);
String []opciones={"one","two","three","four","five"};
ArrayAdapter adapter = new ArrayAdapter(getActivity(),android.R.layout.simple_list_item_1, opciones);
lista.setAdapter(adapter); return vista; }
Example one:
You can use this
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this.getActivity(), android.R.layout.simple_spinner_item, opciones);
adapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
lista.setAdapter(adapter);
`Insted of
ArrayAdapter adapter = new ArrayAdapter(getActivity(),android.R.layout.simple_list_item_1, opciones);
lista.setAdapter(adapter);
Example two:
Create spinner xml
<Spinner
android:id="#+id/spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
Add your spinner string to string.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="string_array">
<item>one</item>
<item>two</item>
<item>three</item>
<item>four</item>
<item>five</item>
</string-array>
Add this code to your fragment
Spinner spinner = (Spinner)lista.findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this.getActivity(),
R.array.string_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
just try this...
I hope it works for you...
Easy if only title is missing and dropdown working
spinner.setPrompt("Title"); or xml: android:prompt="#string/title"
Btw can you elaborate problem and xml file use
spinner.setOnItemSelectedListener(this);
and override
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// On selecting a spinner item
String item = parent.getItemAtPosition(position).toString();
// Showing selected spinner item
}

xml string-array to listView not working

public class Menu extends Activity {
String[] categories;
ListView lv;
Cursor cursor;
Context context;
ArrayAdapter<String> adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
categories = getResources().getStringArray(R.array.Categories_Array);
lv = (ListView) findViewById(R.id.listViewCategories);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.activity_main,
getResources().getStringArray(R.array.Categories_Array));
lv.setAdapter(adapter);
}
...
}
I keep getting an "unfortuneately Bible CYB has stopped working" when I run the app
The problem you have is that you are giving your ArrayAdapter a full Activity's layout to inflate. What you want to do is pass it the row's layout it should use for each of the individual rows it should inflate:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, // or other basic row layout
getResources().getStringArray(R.array.Categories_Array));
You have set the activity_main.xml to the activity and use the same in the constructor of ArrayAdapter. To display the a single string in each row
Change this
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.activity_main,
getResources().getStringArray(R.array.Categories_Array));
to
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.simple_list_item_1,
categories);
where simple_list_item_1.xml is the layout from the android framework
http://androidxref.com/4.4.3_r1.1/xref/frameworks/base/core/res/res/layout/simple_list_item_1.xml
And the constructor
public ArrayAdapter (Context context, int resource, T[] objects)
Added in API level 1 Constructor
Parameters context The current context. resource The resource ID for a
layout file containing a TextView to use when instantiating views.
objects The objects to represent in the ListView.
Instead of using a array adapter try using a list adapter.. it wud give it a custom layout u designed ..
lv = (ListView) findViewById(R.id.listViewCategories);
ListAdapter adapter = new SimpleAdapter(this,
/*id for your custom layout for ex: R.layout.activity_main*/,
getResources().getStringArray(R.array.Categories_Array),/*Here write the id of the text view where u want to display the info.. forex: R.id.tvExample*/);
lv.setListAdapter(adapter);

How to display the imageview and textview using listview without baseadapter in android?

i want to display the imageview and textview using listview .But without using base adapter in listview.is this possible using ArrayAdapter?
final String[] menuitems = new String[] {
"Settings",
"Notifications",
"Logout"
};
int[] menuiicons = { R.drawable.logo, R.drawable.logo, R.drawable.logo };
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
mainListView = (ListView) findViewById( R.id.mainListView );
ArrayList<String> menuitemList = new ArrayList<String>();
ArrayList<Integer> menuiconsList = new ArrayList<Integer>();
listAdapter = new ArrayAdapter<String>(this, R.layout.listview_home, menuitemList);
mainListView.setAdapter( listAdapter );
I think it is not, because you need to populate the ListView through a a getView(..) method, and this is a BaseAdapter method.
Is there a reason why you don't want to use BaseAdapter?
of course you can use ArrayAdapter just override its getView method
Yeah you can add text and images in arrayAdapter .
ArrayAdapter(context, resource, textViewResourceId, objects)
like this you can have lot few more methods .. her reource id your image.
and also have a look at this..
http://developer.android.com/reference/android/widget/ArrayAdapter.html

Categories

Resources