Airborne
Airborne

Reputation: 31

How to assign custom font to parsed data in a listView?

I just parsed some data with jsoup and showed them in listview but i want change the font for parsed data? shall I create a custom lv? how?

public class MainActivity extends Activity {
    adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.product_name, titleList);
}

public class NewThread extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... arg) {
        Document doc;
        try {
            doc = Jsoup.connect("http://tinyez.net//").get();
            title = doc.select("div.post.lit");
            title1 = title.select("h2");
            for (Element titles : title1) {
                titleList.add(titles.text());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(String result) {
        lv.setAdapter(adapter);
    }
}
}

Upvotes: 0

Views: 169

Answers (2)

Vibhor Chopra
Vibhor Chopra

Reputation: 657

Step 1: Create a folder with name "folder" in "assets" folder.

Step 2: Download the font which you want to apply to text(file should be in .ttf format).

Step 3: From MainActivity add the String to List and pass it to CustomAdapter and add adapter to ListView.

public class MainActivity extends Activity {
List<String> listString ;

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


    String[] stringArray = new String[]{"Android", "iPhone", "WindowsMobile",
            "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
            "Linux", "OS/2", "Ubuntu", "Windows7", "Max OS X", "Linux",
            "OS/2", "Ubuntu", "Windows7", "Max OS X", "Linux", "OS/2"};
    listString = new ArrayList<String>();

    for(int i=0; i<stringArray.length;i++){
        listString.add(stringArray[i]);
    }
    ListView listView = (ListView) findViewById(R.id.listView);

    CustomAdapter customAdapter = new CustomAdapter(this,listString);
    listView.setAdapter(customAdapter);
}

}

Step 4: Create class CustomAdapter and extends it with ArrayAdapter and do the under mention code.

public class CustomAdapter extends ArrayAdapter<String>{

List<String> listString;
Activity context;
Typeface typeFace ;
public CustomAdapter(MainActivity mainActivity, List<String> listString) {
    super(mainActivity,R.layout.list_row,listString);
    this.context=mainActivity;
    this.listString=listString;
}
static class ViewHolder{
    public TextView textView;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View vi = convertView;
    if(vi==null)
    {
        LayoutInflater inflater = context.getLayoutInflater();
        vi = inflater.inflate(R.layout.list_row, null);
        ViewHolder viewHolder = new ViewHolder();
        viewHolder.textView = (TextView) vi.findViewById(R.id.textView);
        vi.setTag(viewHolder);
    }
    ViewHolder holder = (ViewHolder) vi.getTag();
    typeFace = Typeface.createFromAsset(context.getAssets(), "fonts/verdana.ttf");
    holder.textView.setTypeface(typeFace);
    holder.textView.setText(listString.get(position));

    return vi;
}

}

Upvotes: 1

FD_
FD_

Reputation: 12919

You'll have to create a custom ListView Adapter. In getView() you'll be able to set the type face of your TextView.

See this on how to write a custom adapter: http://www.vogella.com/tutorials/AndroidListView/article.html

Upvotes: 0

Related Questions