0

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

TomNg
  • 1,897
  • 2
  • 18
  • 25
  • working with custom fonts is a pain in android I am currently doing it too you might want to try this library to make things easier https://github.com/chrisjenx/Calligraphy – Illegal Argument Jun 12 '14 at 03:21

3 Answers3

0

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

Community
  • 1
  • 1
Christopher Francisco
  • 15,672
  • 28
  • 94
  • 206
  • Thanks man! I can do both above, what I need, however, is to display a listview with custom font (and each element can have any font as I chose before) – TomNg Jun 12 '14 at 03:55
0

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;
}
}
Blake Mumford
  • 17,201
  • 12
  • 49
  • 67
  • Thanks, that's exactly what I want to do. Can you show me a working example of using this Adapter? I kinda new to this. – TomNg Jun 12 '14 at 07:00
  • @coderkisser I have already included an example of a custom cursor adapter. What else is unclear to you? – Blake Mumford Jun 12 '14 at 07:33
  • Your Adapter is nice, but MessageCursorAdapter(Context context, Cursor cursor) is deprecated, should be changed to MessageCursorAdapter(Context context, Cursor cursor, false) – TomNg Jun 16 '14 at 07:29
0

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;
  }
Quamber Ali
  • 2,170
  • 25
  • 46
  • Thanks for your comment. But I don't have a textView id, I just have a ListView in my layout, how can I set TypeFace for the textView inside it. In other words, how can I find the texts in my listView? – TomNg Jun 12 '14 at 08:48
  • Seems like you really don't know about listviews but you have to create a Custom Separate layout XML for that and add an ID to your TextViews. you can check this tutorial http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/ – Quamber Ali Jun 12 '14 at 09:43
  • honestly I haven't figured out how to use your getView(), I have many different Textviews in the ListView and I need to find some way to identify each of them. – TomNg Jun 14 '14 at 10:18