Android font picker example? - android

I'm looking to implement a font picker for my Android app. I already found some nice color pickers, can anybody point me to an example of a font picker that I can pop into my app?

It should be pretty simple to make one using a ListView. Each row in the list could simply be a TextView where the text is set to the name of the font and the typeface used is the actual font.
There doesn't need to be anything special about the listview. A simple listview with an ArrayAdapter should be sufficient. Using an adapter like this should work.
NOTE this code isn't meant to compile rather to illustrate what you would need to do to solve the problem. It should be enough though to get you started.
public class FontListAdapter extends ArrayAdapter< String >
{
public FontListAdapter(Activity context, String[] title, boolean[] isHeader) {
super(context, R.layout.listitem, title);
this.context = context;
this.isHeader = isHeader;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
TextView t;
if (convertView==null)
t = new TextView(parent.getContext());
else
t = (TextView) convertView;
t.setText(getFontName());
t.setTypeface(getFont());
return convertView;
}
public Typeface getFont(int pos){
TypeFace tf = getTypeFaceForThisPosition(pos); // you need to figure this out
return tf;
}
public String getFontName(int pos){
String fontName = getFontNameForThisPosition(pos); // you need to figure this out
return FontName;
}
}
Also you can look at this:
http://www.netmite.com/android/mydroid/1.0/development/apps/FontLab/src/com/android/fontlab/FontPicker.java

This looks promissing:
Enumerating the fonts on Android platform
http://www.ulduzsoft.com/2012/01/enumerating-the-fonts-on-android-platform/
FontPreference dialog for Android
http://www.ulduzsoft.com/2012/01/fontpreference-dialog-for-android/

Related

Android: Use custom font with Custom Adapter using SQLite

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

Why is the Spinner's interface SpinnerPopup private? Why can't I add my own popup view?

I was looking at a way to answer this question where the OP is trying to limit the number of items displayed in the spinner's dropdown view. It seems that it cannot be done.
The Spinner class has its own private interface called SpinnerPopup which defines how dropdown items can be shown. This is currently based on the spinnerMode allowing for a dropdown or dialog list.
Both options are also implemented inside the Spinner class as private classes: DialogPopup and DropdownPopup. So it seems to me that the only way to customize this to add another popup option would be to copy the spinner source code and create my own version of it.
But if the SpinnerPopup interface were public, it seems like it would be easy to just:
Create my own popup implementing SpinnerPopup; and
Create my own spinner extending the original one where I override the constructor Spinner(Context context, AttributeSet attrs, int defStyle, int mode) to handle my popup.
Does anyone have any idea (or guess) why this is not the case? Or am I missing a simpler solution here?
Thanks!
I need set my own view to the dropdown, so this was what I did:
public class SpinnerAdapter_Tabela_Preco extends ArrayAdapter<Tabela_Preco>{
// Your sent context
private Context context;
private List<Tabela_Preco> list_tabela_preco;
public SpinnerAdapter_Tabela_Preco(Context context, int textViewResourceId, List<Tabela_Preco> values) {
super(context, textViewResourceId, values);
this.context = context;
this.list_tabela_preco = values;
}
public int getCount(){
return list_tabela_preco.size();
}
public Tabela_Preco getItem(int position){
return list_tabela_preco.get(position);
}
public long getItemId(int position){
return position;
}
// And the "magic" goes here
// This is for the "passive" state of the spinner
#Override
public View getView(int position, View convertView, ViewGroup parent) {
Tabela_Preco t_p = getItem(position);
// I created a dynamic TextView here, but you can reference your own custom layout for each spinner item
TextView label = new TextView(context);
label.setTextColor(Color.BLACK);
label.setTypeface(null, Typeface.BOLD);
// Then you can get the current item using the values array (Users array) and the current position
// You can NOW reference each method you has created in your bean object (User class)
label.setText(t_p.toString());
// And finally return your dynamic (or custom) view for each spinner item
return label;
}
// And here is when the "chooser" is popped up
// Normally is the same view, but you can customize it if you want
#Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
Tabela_Preco t_p = getItem(position);
TextView label = new TextView(context);
label.setTextColor(Color.BLACK);
label.setBackgroundColor(Color.WHITE);
label.setMinimumHeight(50);
label.setGravity(Gravity.CENTER_VERTICAL);
label.setTextSize(18);
label.setText(t_p.toString());
return label;
}
}
And I use with this:
final SpinnerAdapter_Tabela_Preco adapter = new SpinnerAdapter_Tabela_Preco(Consulta_Produto.this, android.R.layout.simple_spinner_item, lista_tabela_preco);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spn_tabela_preco.setAdapter(adapter);
I hope this could help you.

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

Android | ListView Using Layoutinflater

Im Writing An Application that reads XML file and Displays it as a ListView
cause each row has its own Image and Text i am using LayoutInflater, i can display the Texts but cant display Images! this is the code i use for ImageAdapter:
public class ImageAdaptertwo extends ArrayAdapter<String> {
private final Context context;
private final ArrayList<String> values;
private final ArrayList<String> values2;
public ImageAdaptertwo(Context context, ArrayList<String> values,ArrayList<String> values2) {
super(context, R.layout.trimester1_listview, values);
this.context = context;
this.values = values;
this.values2 = values2;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
String s = values.get(position);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.trimester1_main, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.label2);
ImageView imageView = (ImageView) rowView.findViewById(R.id.icon2);
textView.setText(values2.get(position));
// Change icon based on name
//String s = values.get(position);
System.out.println(s);
if (s.equals(" havingsuccessful-pregnancyS.png")) {
imageView.setImageResource(R.drawable.n1);
} else if (s.equals("urfoodguideduringpregnancyS.png")) {
imageView.setImageResource(R.drawable.n2);
} else if (s.equals("lovingurpregnantbodyS.png")) {
imageView.setImageResource(R.drawable.n3);
} else if (s.equals("gettinggoodbreastafterS.png")) {
imageView.setImageResource(R.drawable.n4);
} else if (s.equals("FoodGuidePyramidS.png")) {
imageView.setImageResource(R.drawable.n5);
} else if (s.equals("pregnancyS.png")) {
imageView.setImageResource(R.drawable.n6);
} else if (s.equals("nutritionfoS.png")) {
imageView.setImageResource(R.drawable.n7);
} else if (s.equals("Your-Growing-ChildS.png")) {
imageView.setImageResource(R.drawable.n8);
} else if (s.equals("Fatigue-in-first-trimesterS.png")) {
imageView.setImageResource(R.drawable.n9);
} else if (s.equals("T1S.png")) {
imageView.setImageResource(R.drawable.n10);
}
return rowView;
}
}
values and values2 are the ArrayList which are Coming from XML Parser! Actually no they are ok because i print the content of them and see them in LogCat!
in my Activit i use this for Adapting ImageAdapter
setListAdapter(new ImageAdaptertwo(this, imagelink,texts));
The result is a listview with texts but no Images!
can anybody tell me how i can fix this and display XML contents on ListView?
Solved the problem by changing if Statements to Switch Case . now for each row i have specific number
Well basically a problem in your if - else then. String comparisons are not happening as you expect. Make sure you check any code involving equals,equalsignorecase twice in JAVA. String comparisons are unnecessarily complicated. Also instead of if-else you can use switch and make your life just a little bit easier.
To get images from online you have to get the images on a background thread and then display on the main thread.
See this for help : http://android-developers.blogspot.in/2009/05/painless-threading.html
Similar question: unable to display url image (bitmap)
Solved the problem by changing if Statements to Switch Case . now for each row i have specific image

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