custom font for textview in ArrayAdapter - android

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.

Related

How to change AutoCompleteTextView's EditText's view?

Not its dropdown view, but the TextView view where our choice will appear, that looks like an EditText.
I'm using custom adapter, and I've tried to set it in adapter constructor, and then throw it back to parent using super, but the EditText view is still using the style in xml.
The only view-related I override was 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;
}
Then I use the adapter like this
adapterAutoComplete = new CustomAdapter(
this, R.layout.custom_layout_item, objects);
adapterAutoComplete.setDropDownViewResource(
R.layout.custom_dropdown_layout);
autoComplete.setAdapter(adapterAutoComplete);
Resulted in just how I've said, its EditText view used a style I used in xml and totally abandoned R.layout.custom_layout_item.
What did I do wrong?

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.

Android Custom Spinner not Populating

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

Android Listview font style

I am new in android. In android listview i want to change the font in my own style. Please reply. thanks in advance step by step how to change the font in list view.
in xml....
Typeface typeBold = Typeface.createFromAsset(getAssets(),"fonts/helveticabold.ttf");
Typeface typeNormal = Typeface.createFromAsset(getAssets(), "fonts/helvetica.ttf");
More information try the following links
example1
example2
In android listview i want to change the font in my own style.
By this, I suppose you want to change the font in the child view that is being displayed in the list. For that you need to set the typeface of the TextView inside the getView() as follows
First initialize the font in the constructor of the adapter maybe as follows
private Typeface typeFace;
public MyContructor(Context context)
{
super(context);
mInflater = LayoutInflater.from(mContext);
typeFace=Typeface.createFromAsset(mContext.getAssets(), "Fonts/GrinchedRegular.ttf"));
}
and then in the getView()
#Override
public View getView(final int position, View convertView, final ViewGroup parent)
{
if (convertView == null)
{
convertView = mInflater.inflate(R.layout.sample, null);
}
myText = (TextView) convertView.findViewById(R.id.my_text);
myText.setTypeface(typeFace);
return convertView;
}
First add your font file in your assets folder and use this code
Typeface arial = Typeface.createFromAsset(getAssets(), "fonts/arial.ttf");
name_txt.setTypeface(arial);
Use Customized List -
To change to a different built-in font, use android:typeface in List Item XML
or setTypeface() in getView of ArrayAdopter.
public class CustomeArrayAdopter extends ArrayAdapter<String> {
int res;
Typeface tf;
public CustomeArrayAdopter(Context ctx, int resource,
List<String> items) {
super(ctx, res,items);
res=resource;
tf=Typeface.createFromAsset(ctx.getAssets(),"font/Arial.ttf");
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
//Apply new TypeFace here
TextView item_text=(TextView)findViewById(R.id.listItemtv);
item_text.setTypeface(tf);
.....
}
_ }

Categories

Resources