Android Listview font style - android

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);
.....
}
_ }

Related

Android: Use custom font with Custom Adapter using SQLite

How can I input a text with a custom font, put it to SQLite and then display it in a textview?
I can set font for the EditText and TextView, but I want to get the Typeface from the EditText and put it into the TextView (in my case a text in a ListView)
You can assume my app as a chat application with custom font.
Thanks
OK first, the font doesn't have anything to do with the database. So I'm gonna help you here by pointing you in the right direction:
Here's a tutorial on how to save to a database: http://www.vogella.com/tutorials/AndroidSQLite/article.html (Read it, it's a good one).
And besides, once you want to display your data into a TextView THEN you should set a font to said TextView: Android - Using Custom Font
From what you're describing I'm imagining a ListView which represents a chat thread/log. Each item in the ListView (in this case a TextView) represents a single message. Each message in the chat thread can have a custom font. You want to persist the message's font type to the database. Essentially, you want to change a TextView's font using a custom adapter.
To do this, I would create a Message object. This Message object would have fields (i.e. variables) on it like MessageContent, MessageFont etc. You could then persist this object to your database. Upon retrieval from the database, you could then use a custom adapter to assign the font to your TextView.
public class MessageCursorAdapter extends CursorAdapter {
private Cursor messageCursor;
private Context context;
private final LayoutInflater inflater;
public MessageCursorAdapter(Context context, Cursor cursor) {
super(context, cursor);
this.inflater = LayoutInflater.from(context);
this.context = context;
}
#Override
public void bindView(View view, Context context, Cursor cursor) {
TextView messageTextView = (TextView) view.findViewById(R.id.message_item_text);
String messageFont = cursor.getString(cursor.getColumnIndex("name_of_database_column"));
if (messageFont.equals("Epimodem")) {
Typeface face = Typeface.createFromAsset(getAssets(), "fonts/epimodem.ttf");
messageTextView.setTypeface(face);
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
final View view = this.inflater.inflate(R.layout.message_item, parent, false);
return view;
}
}
In any Adapter Class that you Extend to populate the data you get a Function named getView (int position, View convertView, ViewGroup parent). What you want can be done here.
What you need to do is to create a Class that extends an adapter and then inflate the view that you are going to return and want to be displayed and then on the TextView set the TypeFaces.
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.rowlayout, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.label);
ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
textView.setText(values[position]);
// Set the TypeFace Here
Typeface font = Typeface.createFromAsset(getAssets(), "Chantelli_Antiqua.ttf");
textView.setTypeface(font);
return rowView;
}

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

settype of arrayadapter android

Here is my code how to set my spinner and I want to set a custom font on its items My class is extends Activity and my partial code is like this
Spinner supportSpinner = (Spinner) findViewById(R.id.supportSpinner);
ArrayAdapter supportAdapter = new ArrayAdapter(this,
android.R.layout.simple_spinner_item,supports);
supportSpinner.setAdapter(supportAdapter);
btw supports is a string arraylist of my items for my spinner
I want to do such a thing but it doesnt allow
Typeface type = Typeface.createFromAsset(getAssets(), "comic.ttf");
supportSpinner.setTypeface(type);
how can I change my spinners textviews typeface?
Instead of using
android.R.layout.simple_spinner_item
Create your own layout, you then will use your current code
Typeface type = Typeface.createFromAsset(getAssets(), "comic.ttf");
supportSpinner.setTypeface(type);
but set it on the textview in your custom layout for each spinner item
instead of using android.R.layout.simple_spinner_item create your own layout, and inside XML layout editor there's the option for the TypeFace you want. No need to do any code.
for array adapters::
Typeface typeNormal = Typeface.createFromAsset(getAssets(), "roboto_lite.ttf");
timearray = new ArrayAdapter<String>(DetailsActivity.this,R.layout.floorrow,R.id.txt, flor){
public View getView(int pos, View convertView, android.view.ViewGroup parent) {
View v = convertView;
if(v== null){
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v=vi.inflate(R.layout.floorrow, null);
}
TextView tv = (TextView)v.findViewById(R.id.txt);
tv.setText(flor.get(pos));
tv.setTypeface(typeNormal);
return v;
};
};
lv_building.setAdapter(timearray);

Android font picker example?

I'm looking to implement a font picker for my Android app. I already found some nice color pickers, can anybody point me to an example of a font picker that I can pop into my app?
It should be pretty simple to make one using a ListView. Each row in the list could simply be a TextView where the text is set to the name of the font and the typeface used is the actual font.
There doesn't need to be anything special about the listview. A simple listview with an ArrayAdapter should be sufficient. Using an adapter like this should work.
NOTE this code isn't meant to compile rather to illustrate what you would need to do to solve the problem. It should be enough though to get you started.
public class FontListAdapter extends ArrayAdapter< String >
{
public FontListAdapter(Activity context, String[] title, boolean[] isHeader) {
super(context, R.layout.listitem, title);
this.context = context;
this.isHeader = isHeader;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
TextView t;
if (convertView==null)
t = new TextView(parent.getContext());
else
t = (TextView) convertView;
t.setText(getFontName());
t.setTypeface(getFont());
return convertView;
}
public Typeface getFont(int pos){
TypeFace tf = getTypeFaceForThisPosition(pos); // you need to figure this out
return tf;
}
public String getFontName(int pos){
String fontName = getFontNameForThisPosition(pos); // you need to figure this out
return FontName;
}
}
Also you can look at this:
http://www.netmite.com/android/mydroid/1.0/development/apps/FontLab/src/com/android/fontlab/FontPicker.java
This looks promissing:
Enumerating the fonts on Android platform
http://www.ulduzsoft.com/2012/01/enumerating-the-fonts-on-android-platform/
FontPreference dialog for Android
http://www.ulduzsoft.com/2012/01/fontpreference-dialog-for-android/

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.

Categories

Resources