Android: Use custom font with Custom Adapter using SQLite - android

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;
}

Related

Inflate two layouts in getView() to implement ListView Section header

I'm trying to create section headers for my ListView in a way that some list rows have headers and some do not. So what i have is three xml layout files. fragment_listview.xml contains the <ListView>
row_listview.xml contains the content for ListView rows
header_listview.xml is for the section headers.
Feeds.java is my model class.
So in my adapter i have something like this:
public class FeedsArrayAdapter extends ArrayAdapter<Feeds> {
private Context context;
private List<Feeds> feeds;
private LayoutInflater vi;
FeedsArrayAdapter(Context c, List<Feed> ff){ //initializes all the class member fields }
public void getView(int position, View convertView, ViewGroup parent){
Feeds f = feeds.get(position);
if(f.headerNeeded()){
convertView = vi.inflate(R.layout.header_listview, null);
TextView textViewHeader = (TextView) convertView.findViewById(R.id.feed_header);
textViewHeader.setText(new SimpleDateFormat("dd/MM/yyyy").format(feed.date));
}
else {
convertView = vi.inflate(R.layout.row_listview, null);
TextView textViewfeedHeading = (TextView) convertView.findViewById(R.id.feed_heading);
TextView textViewfeedSubheading = (TextView) convertView.findViewById(R.id.feed_subheading);
TextView textViewfeedDate = (TextView) convertView.findViewById(R.id.textViewDate);
ImageView imageViewfeedIcon = (ImageView) convertView.findViewById(R.id.feed_icon);
ImageView imageViewfeedBanner = (ImageView) convertView.findViewById(R.id.feed_banner);
textViewfeedHeading.setText(feed.title);
textViewfeedSubheading.setText(feed.subtitle);
textViewfeedDate.setText(new SimpleDateFormat("dd/MM/yyyy").format(feed.date));
}
return convertView;
}
So this gives us something like this:
---header1----
---header2----
___Row 3_____
___Row 4_____
What i want is:
-----header 1------
_____Row 1_______
-----header 2------
_____Row 2_______
_____Row 3_______
_____Row 4_______
If i try to inflate row.listview in if(true) I get an exception.
Any approach how to resolve this.
you can use Sticky Header Library here
yourListView.addHeaderView(yourHeaderView);
See http://developer.android.com/reference/android/widget/ListView.html#addHeaderView%28android.view.View%29
use expandable listview with BaseExpandableAdapter

Android Listview font style

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

attractive listview with images

i want to build an application with a listview or whatever.. that looks very attractive and have some images and many more.But i cant find a good way to have that. I exactly want my application's UI like this images:
(source: coenraets.org)
(source: coenraets.org)
i want to display my app like this images please suggest me how can i do this? please if u know some tutorials then give the links.
Yes. Just place one ImageView and TextView in one xml layout. And, inflate this layout into one layout which is having the ListView And, do the process there for getting images from webservice or locally stored
Here i provide some example links that may very useful to you -
Lazy load of images in ListView
ListView with images
How to display a list of images in a ListView in Android?
You need to build the layout you want in a XML file, the same way you would do for an Activity. Then just inflate the XML layout for each row in your ListView and set its values and images.
Example of one ArrayAdapter that I've inflated with my own view (picture, text and checkbox):
private class FriendListAdapter extends ArrayAdapter<User> {
public FriendListAdapter(Activity a, int textViewResourceId, List<User> items) {
super(a, textViewResourceId, items);
}
public class ViewHolder{
public TextView username;
public ImageView image;
public CheckedTextView ctv;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View v = convertView;
ViewHolder holder;
if (v == null) {
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.invite_friend_row, null);
holder = new ViewHolder();
holder.username = (TextView) v.findViewById(R.id.username);
holder.username.setTypeface(tf);
holder.image = (ImageView) v.findViewById(R.id.image);
holder.ctv = (CheckedTextView) v.findViewById(R.id.checked);
v.setTag(holder);
}
else{
holder = (ViewHolder) v.getTag();
}
final User user = getItem(position);
if(user != null){
holder.username.setText(user.getName());
holder.ctv.setChecked(user.isChecked());
holder.image.setImageView(user.getImage());
}
return v;
}
}
You get the idea!

how can i use android ListView to display fields in an object?

I am trying to construct a ListActivity that uses a listview to display a list of friends and their associated status (ie single, in a relationship).
So far, I have an ArrayAdapter set like so:
Friend[] friendList = new Friend[] {
new Friend("john doe", "single"),
new Friend("jane doe", "married")
};
setListAdapter(new ArrayAdapter<Friend>(this, R.layout.portal_listview, friendList));
I would like each item in the listview to display as such:
1name: john doe
status: single
2name: jane doe
status: married
Create you own CustomArrayAdapter which extends ArrayAdapter and overwrite the View getView(int position, View convertView, ViewGroup parent) method
I could be something like this
public class FriendAdapter extends ArrayAdapter<Friend>{
private Context context;
private int resource;
private ArrayList<Friend> friends;
public FriendAdapter(Context context, int resource, ArrayList<Friend> friends) {
super(context, resource, objects);
this.context = context;
this.resource = resource;
this.friends = friends;
}
#Override
public View getView(int position, View convertView, ViewGroup parent){
View view = convertView;
if (view == null){
LayoutInflater vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = vi.inflate(your_list_item_resource, null);
}
Friend friend = friends.get(position);
view.setId(position);
if (friend != null){
TextView name = (TextView) view.findViewById(R.id.friendName);
name.setText(friend.getName());
TextView status = (TextView) view.findViewById(R.id.friendStatus);
status.setText(friend.getStatus());
}
return view;
}
Hope this can help you.
In the xml row file, you can add a composite object, like a LinearLayout
and inside that you could include more than one textview, or a combination etc.
I guess the linearlayout would be unnecessary if the individual items within the list are displayed vertically, but you can try.
You need to override getView in the ArrayAdapter class. In that method, inflate your R.layout.portal_listview instance and fill it with whatever you want.
That xml layout should be some layout form that includes a TextView for the two things you want to display.
Easiest would be a linear layout, set to vertical orientation, with 2 TextViews in it, stacked.

How can I add 3 buttons to a listview on Android?

I'd like to customize each line of a List View and make them show up in the following way:
List View Buttons http://raphaeu.com/img_botoes.jpg
In the picture above, I will, of course, replace the numbers for images. But I need to make each one of those places "clickable", that is, each numbered place must have a different touch event attached to it.
How would I go on doing that?
Thank you in advance!!
You need to create a custom adapter class, extending from BaseAdapter. In it, you can inflate any layout for your rows you wish. There're tons of tutorials for this, search for 'listview custom adapter'.
Create a Custom List Adapter like Andro.. states here:
public class CustomListAdapter extends BaseAdapter {
private ArrayList<SingleElementDetails> allElementDetails;
private Context con;
private LayoutInflater mInflater;
public CustomListAdapter(Context context, ArrayList<SingleElementDetails> results) {
allElementDetails = results;
mInflater = LayoutInflater.from(context);
con=context;
public View getView(int position, View convertView, ViewGroup parent)
{
convertView = mInflater.inflate(R.layout.listview1, null);
Button bt=(Button)convertView.findViewById(R.id.bt);
TextView textview1= (TextView) convertView.findViewById(R.id.dishname_entry);
TextView textview2 = (TextView) convertView.findViewById(R.id.category_entry);
TextView textview3=(TextView)convertView.findViewById(R.id.description_entry);
textview1.setText(allElementDetails.get(position).getDishName());
textview2.setText(allElementDetails.get(position).getCategory());
textview3.setText(allElementDetails.get(position).getDescription());
bt.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
Intent intent=new Intent(con,MainActivity.class);
con.startActivity(intent);
}
});
return convertView;
}
}
Just have to tweak it a little bit for what you're trying to do. Everything you need though is pretty much in that class.

Categories

Resources