Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Customize adapter to use custom font

Tags:

java

android

I have both Custom Font example and a small List View example app. But I am unable to join them !!!

 /** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    TextView tv = (TextView)findViewById(R.id.tv);
    Typeface cFont = Typeface.createFromAsset(getAssets(), "fonts/jcc.ttf");
    tv.setTypeface(cFont);




<TextView  
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:text="@string/hello"
android:textSize="18sp"
android:id="@+id/tv"
/>

and

    /** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_product_list);

    String asim02 = System.getProperty("line.separator");

    String products[] = {
            "Apple" + asim02 +"Definition1", 
            "Orange" + asim02 +"Definition2",
            "Banana"+ asim02 +"Definition3", 
            "Onion"+ asim02 +"Definition4",  };

    lv = (ListView) findViewById(R.id.list_view);

    // Adding items to listview
    adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.p_list,   products);
    lv.setAdapter(adapter);



<TextView

        android:textColor="?android:textColorPrimary"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="15sp"
        android:id="@+id/p_list"
        />

Please help me customizing adapter to use custom font

Image of ProductList app https://dl.dropbox.com/u/15065300/ProductList.png

Here is the two example Apps http://www.mediafire.com/?scb3hjplb15yly5

like image 678
Prabir Ray Avatar asked Jan 26 '26 03:01

Prabir Ray


2 Answers

I do this by creating a CustomTextView.class

This would be my list_item_layout.xml :

<LinearLayout
    [...]

    android:orientation="vertical">
        <com.example.CustomTextView
             android:layout_width="fill_parent"
             [...]
             android:id="@+id/myCustomTextView"/>

</LinearLayout>

N.B.: You have to point to your CustomTextView.class or else you will get an exception

This is the CustomTextView.class

public class CustomTextView extends TextView {

    public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    public CustomTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public CustomTextView(Context context) {
        super(context);
        init();
    }

    public void init() {
        Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/yourfont.ttf");
        setTypeface(tf);
    }

}

You can apply this to EditText, TextView, Buttons etc

like image 136
Delpes Avatar answered Jan 28 '26 16:01

Delpes


Tacking off of @FoamyGuy's response, I viewed the your mediafire file response, but you didn't quite make a modified arrayAdapter class. I don't exactly have one of these file-sharing site accounts, so here's what should work:

in your ProductList activity replace your adding items to listview code with:

    // Adding items to listview
    Typeface cFont = Typeface.createFromAsset(getAssets(), "fonts/jcc.ttf");
    adapter = new CustomArrayAdapter(this, R.layout.list_item, R.id.p_list, products, cFont);

then the custom class, which you must also insert:

public class CustomArrayAdapter extends ArrayAdapter <String> {

    private Typeface tf;
    private LayoutInflater inflater;
    private int resource;
    private int textViewResourceId;
    private String[] objects;

    public CustomArrayAdapter(Context context, int resource, int textViewResourceId,
            String[] objects, Typeface tf) {
        super(context, resource, textViewResourceId, objects);
        this.tf = tf;
        this.resource = resource;
        this.textViewResourceId = textViewResourceId;
        this.objects = objects;
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            convertView = inflater.inflate(resource, parent, false);    
        }

        TextView text = (TextView) convertView.findViewById(textViewResourceId);
        text.setTypeface(tf);
        text.setText(objects[position]);
        return convertView;
    }
}

if you need to see the full source folder, so that you're not making the same mistake, send me your email address and i'll send it to you.

like image 41
mango Avatar answered Jan 28 '26 17:01

mango



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!