numediaweb numediaweb - 21 days ago 4
Android Question

Tifinagh character not displayed on Android emulator

As you can see on the attached picture, Eclipse is displaying correctly the Tifinagh text from an XML in the resources file.
But When I parse this XML into a listView it doesn't show. I taught it has to do with XML parsing, so I created a new project based on the Hello world tutorial and substituted "Hello world!" with Tifinagh characters (that I copied from the XML) and again; they don't show.

I checked a similar question on stackoverflow but it has no answer yet.

So;


  1. Does Android support Tifinagh?

  2. If not, are there any workarounds to solve this problem?



Thanks for any help.

Tifinagh_not_displayed_on_android_emulator

Update



I managed to set the font in the hello world interface by using:

// Set the tifinagh font
Typeface tf = Typeface.createFromAsset(getAssets(), "t_ircam-webfont.ttf");
TextView tv = (TextView) MainActivity.this.findViewById(R.id.txt);
tv.setTypeface(tf);


But how can I set it for a ListView?

package com.theopentutorials.android.activities;

import java.io.IOException;
import java.util.List;

import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.theopentutorials.android.beans.PostObj;
import com.theopentutorials.android.xml.XMLPullParserHandler;

public class XMLPullParserActivity extends Activity {

ListView listView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

// Prepare the list view to publish item to.
listView = (ListView) findViewById(R.id.list);

List<PostObj> posts = null;
try {
XMLPullParserHandler parser = new XMLPullParserHandler();
posts = parser.parse(getAssets().open("amawal_posts.xml"));

System.out.println("============================== ");
System.out.println("Posts fresh "+ posts);
System.out.println("============================== ");

ArrayAdapter<PostObj> adapter = new ArrayAdapter<PostObj>(this, R.layout.list_item, posts);
listView.setAdapter(adapter);
} catch (IOException e) {
e.printStackTrace();
}

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.xmlpull_parser, menu);
return true;
}

}

Answer

After some more trial and error, I got it working :) Since I get the raw data from XML to a list view, I had to create a custom adapter, Check the full code:

package com.theopentutorials.android.xml;

import java.util.List;

import android.content.Context;
import android.graphics.Typeface;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import android.widget.TextView;

public class MyAdapter<T> extends BaseAdapter {

private final Typeface mTypeface;

private List<T> objects; // obviously don't use object, use whatever
                                // you really want

private final Context context;

public MyAdapter(Context context, int resource, List<T> objects) {//Context context, int resource, List<T> objects
    this.context = context;
    this.objects = (List<T>) objects;
    mTypeface = Typeface.createFromAsset(context.getAssets(), "t_ircam-webfont.ttf");
}

@Override
public int getCount() {
    return objects.size();
}

@Override
public Object getItem(int position) {
    return objects.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

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

    Object obj = objects.get(position);

    TextView tv = new TextView(context);
    tv.setTypeface(mTypeface);
    tv.setText(obj.toString()); // use whatever method you want for the
                                // label
    // set whatever typeface you want here as well
    return tv;
}
}

And in my Main activity

public class XMLPullParserActivity extends Activity {

    ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // Prepare the list view to publish item to.
        listView = (ListView) findViewById(R.id.list);

        List<PostObj> posts = null;
        try {
            // Parse the XML
            XMLPullParserHandler parser = new XMLPullParserHandler();
            posts = parser.parse(getAssets().open("amawal_posts.xml"));

            /*System.out.println("============================== ");
            System.out.println("Posts fresh " + posts);
            System.out.println("============================== ");*/

            listView.setAdapter(new MyAdapter<PostObj>(this, R.layout.list_item, posts));


        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.xmlpull_parser, menu);
        return true;
    }
}

In the layout/list_item.xml

<?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" >


</TextView>

In the layout/main.xml

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:text="@string/hello_world"
        android:textColor="#CC0033"
        android:textSize="16sp" />

    <ListView
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@id/text" />

</RelativeLayout>