Android Custom Spinner not Populating - android

Having trouble getting my spinner to populate. Well, I was getting it to populate just fine with a string array from strings.xml but now I want to add a custom font to the array. I'm using a Custom ArrayAdapter for the first time and it is overwriting my "android:entries=" and "android:prompt=" inside its layout xml.
So, I need to populate this spinner with an array from my strings.xml
(Inside onCreate)
spinner1 = (Spinner) findViewById(R.id.distancespinner);
MyArrayAdapter ma = new MyArrayAdapter(this, R.layout.my_spinner_style);
spinner1.setAdapter(ma);
Then:
private class MyArrayAdapter extends ArrayAdapter {
Typeface font = Typeface.createFromAsset(getAssets(), "fonts/m01.TTF");
public MyArrayAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
}
public TextView getView(int position, View convertView, ViewGroup parent) {
TextView v = (TextView) super.getView(position, convertView, parent);
v.setTypeface(font);
return v;
}
public TextView getDropDownView(int position, View convertView, ViewGroup parent) {
TextView v = (TextView) super.getView(position, convertView, parent);
v.setTypeface(font);
return v;
}
}
I tried adding the entries and prompt again inside the my_spinner_style.xml...
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/spinnerTarget"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:entries="#array/location_arrays"
android:prompt="#string/location_prompt"
android:singleLine="True" />
But, that is not working either.

Maybe this is a little late. After I implement your code and some additional test, I think I finally get the answer. The KEY problem the spinner not populating is:
You set the entries of the spinner within the xml file but replace it programmatically with an EMPTY adapter.
I have tried the following ways to populate the spinner with different results:
Set the entries in xml without any adapter.
<Spinner
android:id="#+id/simple_spinner2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="#array/simpleStringArray"
android:prompt="#string/simplePrompt"/>
It populates the spinner with right content without any additional style.
Set the entries in xml with another adapter set to the spinner (as you did in the question)
in .xml
<Spinner
android:id="#+id/simple_spinner2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="#array/simpleStringArray"
android:prompt="#string/simplePrompt"/>
in .java:
Spinner mySpinner2 = (Spinner) findViewById(R.id.simple_spinner2);
MySpinnerAdapter2 spinnerAdapter2 = new MySpinnerAdapter2(this, R.layout.simple_list_item);
It displays an empty spinner.
set the data programmatically to adapter
in .xml
in .java
setContentView(R.layout.activity_spinner);
Spinner mySpinner2 = (Spinner) findViewById(R.id.simple_spinner2);
MySpinnerAdapter2 spinnerAdapter2 = new MySpinnerAdapter2(this, R.layout.simple_list_item);
spinnerAdapter2.addAll(items2);
private class MySpinnerAdapter2 extends ArrayAdapter {
public MySpinnerAdapter2(Context context, int textViewResourceId) {
super(context, textViewResourceId);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView v = (TextView) super.getView(position, convertView, parent);
v.setTextColor(Color.BLUE);
return v;
}
#Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
Typeface myFont = Typeface.createFromAsset(getAssets(), "fonts/Igualb.ttf");
TextView v = (TextView) super.getDropDownView(position, convertView, parent);
v.setTextColor(Color.RED);
v.setTypeface(myFont);
return v;
}
It displays the content correctly with cumtom-set font.
You can check for the complete project in this github repository if you like to.
Wish this helps.

That's the code I rewrote and it's working fine:
Spinner spinner = (Spinner) findViewById(R.id.distancespinner);
spinner.setPromptId(R.string.location_prompt);
MyArrayAdapter ma = new MyArrayAdapter(this, R.layout.my_spinner_style);
ma.addAll(Arrays.asList(getResources().getStringArray(
R.array.location_arrays)));
spinner.setAdapter(ma);
And the XML
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/spinnerTarget"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:singleLine="True" />
Adapter is the same.

May be the below line is incorrect
Typeface font = Typeface.createFromAsset(getAssets(), "fonts/m01.TTF");
m01.TTF is wrong change to m01.ttf
Typeface font = Typeface.createFromAsset(getAssets(), "fonts/m01.ttf");

Related

Using same custom adapter for AutoCompleteTextView and Spinner

So I use one adapter for my Spinner and AutoCompleteTextView, which extends ArrayAdapter<Object>. For View-related method, I only override getView.
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
TextView tv = (TextView) view;
//I do something with TextView here, but won't affect the problem
return view;
}
Now, both declared like this:
adapterSpinner = new CustomAdapter(
this, R.layout.custom_layout_item, objects);
adapterService.setDropDownViewResource(
R.layout.custom_dropdown_layout);
spinner.setAdapter(adapterSpinner);
adapterAutoComplete = new CustomAdapter(
this, R.layout.custom_layout_item, objects);
adapterAutoComplete.setDropDownViewResource(
R.layout.custom_dropdown_layout);
autoComplete.setAdapter(adapterAutoComplete);
The result confusing me:
AutoCompleteTextView
Applies custom_layout_item to its dropdown View instead, and instead applies a style I gave in its element in xml to its TextView/EditText layout. It ignores custom_dropdown_layout I've set.
Spinner
Applies correctly all of them, its TextView view using custom_layout_item, and its dropdown view using custom_dropdown_layout. Though it ignores the style I gave to it in xml.
Functional, both works fine. But from UI, quite a mess... Why did it happen?

How to add Image in Spinner?

I made this app called UnitConverter...
unitarray=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item);
unitarray.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
SpinnerUnit.setAdapter(unitarray);
unitarray.add(getResources().getString(R.string.unit1));
unitarray.add(getResources().getString(R.string.unit2));
unitarray.add(getResources().getString(R.string.unit3));
unitarray.add(getResources().getString(R.string.unit4));
unitarray.add(getResources().getString(R.string.unit5));
unitarray.add(getResources().getString(R.string.unit6));
unitarray.add(getResources().getString(R.string.unit7));
unitarray.add(getResources().getString(R.string.unit8));
unitarray.setNotifyOnChange(true);
everything's awesome but I want to put images next to each Unit!
is there anyone who can help me with this please? thanks!
Probably the simplest way would be to use android's R.layout.simple_spinner_item as the spinner elem layout and internally to add an image to the TextView using setCompoundDrawablesWithIntrinsicBounds
ArrayAdapter adapter = new ArrayAdapter(context, android.R.layout.simple_spinner_item, countries);
countrySpinner.setAdapter(adapter);
And the adapter:
class ImageSpinnerAdapter extends ArrayAdapter {
public View getView(int position, View convertView, ViewGroup parent) {
TextView textView = (TextView) super.getView(position, convertView, parent);
Elem elem = elemArray.get(position);
textView.setText(elem.toString());
textView.setCompoundDrawablesWithIntrinsicBounds(elem.getImage(), null, null, null);
return textView;
}
}
See detailed example here.

custom font for textview in ArrayAdapter

I'm trying to change the font of a TextView in my ArrayAdapter. The font chantelli_antiqua.ttf is in the assets folder.
Here is my Java code:
listItemAdapter = new ArrayAdapter<MenuItem>(this, R.layout.listitem, menuItems);
Typeface font = Typeface.createFromAsset(getAssets(), "chantelli_antiqua.ttf");
TextView v = (TextView)listItemAdapter.getView(0, null, null);
v.setTypeface(font);
xml for the listitem layout:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="30sp"
/>
I'm quite sure the problem lies with the Adapter.getView(int, View, ViewGroup) method. I just don't really understand what to pass as variables and tried null. But this doesn't do what I would like it to.
How to change the font of the TextView in the Adapter to the custom font?
Update
According to Pixie's suggestion I created a MenuItemAdapter which extends ArrayAdapter<MenuItem>:
public class MenuItemAdapter extends ArrayAdapter<MenuItem>
{
private Typeface font;
public MenuItemAdapter(Context context, int textViewResourceId, List<MenuItem> objects)
{
super(context, textViewResourceId, objects);
font = Typeface.createFromAsset(context.getAssets(), "chantelli_antiqua.ttf");
}
#Override
public View getView(int position, View view, ViewGroup viewGroup)
{
((TextView)view).setTypeface(font);
return super.getView(position, view, viewGroup);
}
}
And changed my java code to:
listItemAdapter = new MenuItemAdapter(this, R.layout.listitem, menuItems);
But now my app crashes after the onCreate of the ListActivity, but before hitting the breakpoint in getView(...), I haven't been able to figure out yet why. Any suggestion?
Update2
Changed the code for getView(...) to:
#Override
public View getView(int position, View view, ViewGroup viewGroup)
{
View v = super.getView(position, view, viewGroup);
((TextView)v).setTypeface(font);
return v;
}
and this works. :)
You shouldn't call the getView() method of your adapter. The ListView does this for you. You have to extend the ArrayAdapter class and override the getView() method instead. In this method you have to inflate a new view or re-use convertView and set the typeface for this view.
I think the problem is in return super.getView(position, view, viewGroup); at the end of getView() method.
I think it should be like this
#Override
public View getView(int position, View view, ViewGroup viewGroup)
{
TextView tv = ((TextView)view).setTypeface(font);
tv.setText(<String> getItem());
return tv;
}
please note this code is example I didn't try it now but I made custom arrayAdapter before and it was something like that.
Here is a tutorial describing how to create custom arrayAdapter.

Can I make one ListView item have a different Text Color?

I have the layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ListView
android:id="#+id/ListView01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1.0"
android:textColor="#android:color/white"
android:dividerHeight="1px"
android:listSelector="#drawable/highlight_sel"
/>
</LinearLayout>
And the code:
private ListView lv1;
private String lv_arr[]={"Item 1","Item 2","Item 3","Item 4"};
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.newsmenu);
lv1=(ListView)findViewById(R.id.ListView01);
// By using setAdpater method in listview we an add string array in list.
lv1.setAdapter(
new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
lv_arr));
}
I want the text color of Item 2 (or 1 or 3 or 4) to appear dynamically as red (denoting a new item) or white (default). Is there a way to do this?
I already have a selector present, which is why I used ListView. I've search the Internet and this site, and I have not seen this question broached.
So is it possible?
Yes everything is possible. you need to write your own adapter implementation basically overriding the getView Method in the adapter. search google and stack you will find many tutorials on how to write an adapter.
Writing a special adapter to override getView in simple adapter is the way to change the text color alternating on the lines of your choice in a listview. I took the example which has been repeated many times on this website and added a way to change the text color. position mod length to select the color position can be replaced with any scheme you like. The text view "business" can be the first line of your layout like mine--or use the android.R.id.text1.
public class SpecialAdapter extends SimpleAdapter {
private int[] colors = new int[] { 0x30FF0000, 0x300000FF };
public SpecialAdapter(Context context, List<HashMap<String, String>> items, int resource, String[] from, int[] to) {
super(context, items, resource, from, to);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
int colorPos = position % colors.length;
//view.setBackgroundColor(colors[colorPos]); //old example
TextView tv1 = (TextView)view.findViewById(R.id.business); //new
tv1.setTextColor(colors[colorPos]); //new
return view;
}
}
Just use SpecialAdapter instead of SimpleAdapter in your app.
Here's an example of a getView method. Note that it's using a viewholder for efficiency. If you want to know more about that, let me know.
public View getView(int position, View convertView, ViewGroup parent) {
tempDeal = exampleBoxArrayList.get(position);
ViewHolder holder;
if (convertView == null) {
convertView = inflator.inflate(R.layout.list_item_example_box, null);
holder = new ViewHolder();
holder.divider = (RelativeLayout) convertView.findViewById(R.id.example_box_divider);
holder.merchantName = (TextView) convertView.findViewById(R.id.example_box_merchant_name);
holder.expireDate = (TextView) convertView.findViewById(R.id.example_box_expire_date);
holder.description = (TextView) convertView.findViewById(R.id.example_box_description);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
if (tempDeal.isDivider()) {
holder.divider.setVisibility(View.VISIBLE);
} else {
holder.divider.setVisibility(View.GONE);
}
holder.merchantName.setText(tempDeal.getMerchantName());
holder.expireDate.setText(tempDeal.getExpiryDateString());
holder.description.setText(tempDeal.getPriceOption().getDescription());
return convertView;
}
As you can see, I call the isDivider() method on my custom object (this method looks at a boolean set on data load). This method is used to turn the visibility of part of the layout on or off.
Alternatively, you could load a completely new layout based on this same concept.

How to set the size of an AutoCompleteTextView Result?

Does anybody know How to set the size of an AutoCompleteTextView Result ?
I try android:textSize="12sp" But it only modify the size of the text in the TextView, not in the result.
The best way is to create your own xml file with a customized design, such as "dropdown.xml"
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/textAutoComplete"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#F3F5E1"
android:gravity="center_vertical"
android:padding="5dp" />
and in your code use this
AutoCompleteTextView etChoose= (AutoCompleteTextView) findViewById(R.id.etChoose);
ArrayAdapter<String> adapterChoose = new ArrayAdapter<String>(this,R.layout.dropdown, ChooseList);
Just add android:dropDownHeight="size" for AutoCompleteTextView in xml file.
private ArrayAdapter<String> autoCompleteAdapter = new ArrayAdapter<String>
(this,android.R.layout.simple_list_item_1, yourList) {
public View getView(int position, View convertView, ViewGroup parent) {
View v = super.getView(position, convertView, parent);
((TextView) v).setTextSize(14);
Typeface Type = getFont () ; // custom method to get a font from "assets" folder
((TextView) v).setTypeface(Type);
((TextView) v).setTextColor(YourColor);
((TextView) v) .setGravity(Gravity.LEFT|Gravity.CENTER_VERTICAL);
return v;
}
};
YourAutoCompleteView.setAdapter(autoCompleteAdapter);
Assuming someone is facing the same issue now
Instead of using simple_spinner_item you can also use simple_spinner_dropdown_item to get better results as it will format the data as it would appear in a normal dropdown
ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_dropdown_item, subjects1);

Categories

Resources