Reputation: 5440
I have developed an Andriod RSS reader app.I have used custom listview to list RSS titles and its images.Now I want to change the font of RSS titles.How can I set typeface to my title textview?
Here is my adapter class in which I am setting the title Textview.
Adapter.java
public class InternationalAdapter extends BaseAdapter {
private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater=null;
public ImageLoader imageLoader;
public InternationalAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
activity = a;
data=d;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader=new ImageLoader(activity.getApplicationContext());
}
public int getCount() {
return data.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.list_row, null);
TextView title = (TextView)vi.findViewById(R.id.title); // For this Textview I want to set Typeface.
TextView date = (TextView)vi.findViewById(R.id.artist);
ImageView thumb_image=(ImageView)vi.findViewById(R.id.list_image);
HashMap<String, String> news = new HashMap<String, String>();
news = data.get(position);
// Setting all values in listview
title.setText(International.Title[position]);
date.setText(International.Date[position]);
imageLoader.DisplayImage(International.image[position], thumb_image);
return vi;
}
}
Upvotes: 8
Views: 29375
Reputation: 143
Source : https://segunfamisa.com/posts/custom-fonts-with-android-support-library
Setting typeface like the one below often does not work (Android Runtime Exception font asset not found)
Typeface font = Typeface.createFromAsset(activity.getAssets(), "fonts/montserrat_regular.ttf");
and can result into error like this:
java.lang.RuntimeException: Font asset not found fonts/montserrat_regular.ttf
You can therefore use the new way as given here
res/font
and paste your fonts thereFont family is something that was introduced in Android, and it is used to define a group of fonts and their corresponding style. So the system can determine what font resource to use for regular, bold and italic styles and weight configurations. A typical font family looks like this:
<?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<!-- regular -->
<font
android:font="@font/josefinslab_regular"
android:fontStyle="normal"
android:fontWeight="400"
app:font="@font/josefinslab_regular"
app:fontStyle="normal"
app:fontWeight="400" />
<!-- italic -->
<font
android:font="@font/josefinslab_italic"
android:fontStyle="italic"
android:fontWeight="400"
app:font="@font/josefinslab_italic"
app:fontStyle="italic"
app:fontWeight="400" />
</font-family>
A really important thing to note is that we had to define attributes using both android and app namespaces. The app namespace is what ensures that the feature is backward compatible.
Typeface typeface = ResourcesCompat.getFont(this, R.font.app_font);
myTextView.setTypeface(typeface);
Source : https://segunfamisa.com/posts/custom-fonts-with-android-support-library
Hope this helps :)
Upvotes: 3
Reputation: 11
We can also use this method:
t.setTypeface(Typeface.MONOSPACE,Typeface.BOLD);
Here monospace is a typeface.
Upvotes: 0
Reputation: 619
You can use this library to change font on any view, only need to pass font file name with the binding xml code. https://github.com/ChathuraHettiarachchi/TypeFaced
1.textview
2.edittext
3.switch
4.toggle
5.button
6.radiobutton
7.checkbox
<com.chootdev.typefaced.TypeFacedTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:textView_font="YOUR_FONT_NAME.ttf/>
Upvotes: 0
Reputation: 7605
use custome textview as given below and set font there
public class MyTextView extends TextView{
public MyTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
rotate();
}
public MyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
rotate();
}
public MyTextView(Context context) {
super(context);
init();
rotate();
}
private void rotate() {
// TODO Auto-generated method stub
setSelected(true);
}
private void init() {
if (!isInEditMode()) {
Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/TrajanPro-Regular.otf");
setTypeface(tf);
}
}
}
and in your listview row xml means in list_row layout add textview as
<YourpackageName.MyTextView
android:layout_marginTop="10dip" android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textSize="22px"
android:textColor="#34A4c5"
android:ellipsize="marquee"
android:maxWidth="220dp"
android:fadingEdge="horizontal"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="true"
android:singleLine="true"
android:layout_marginLeft="10dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_marginRight="10dp"></YourpackageName.MyTextView>
here more property are used u can remove which not required and in your getview method define it as
MyTextView title = (MyTextView)vi.findViewById(R.id.title);
Upvotes: 0
Reputation: 12134
You can use this,
public class InternationalAdapter extends BaseAdapter {
private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater=null;
public ImageLoader imageLoader;
public InternationalAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
activity = a;
data=d;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader=new ImageLoader(activity.getApplicationContext());
}
public int getCount() {
return data.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.list_row, null);
TextView title = (TextView)vi.findViewById(R.id.title); // For this Textview I want to set Typeface.
TextView date = (TextView)vi.findViewById(R.id.artist);
//Added Here
Typeface font = Typeface.createFromAsset(
activity.getAssets(),
"fonts/androidnation.ttf");
title .setTypeface(font);
ImageView thumb_image=(ImageView)vi.findViewById(R.id.list_image);
HashMap<String, String> news = new HashMap<String, String>();
news = data.get(position);
// Setting all values in listview
title.setText(International.Title[position]);
date.setText(International.Date[position]);
imageLoader.DisplayImage(International.image[position], thumb_image);
return vi;
}
}
Upvotes: 7
Reputation: 12134
Try like this
Typeface font = Typeface.createFromAsset(
getContext().getAssets(),
"fonts/androidnation.ttf");
title .setTypeface(font);
Upvotes: 3
Reputation: 2930
EditText edittext =(EditText) findViewById(R.id.ed);
String path="F:\\MTCORSVA.TTF";
Typeface tf=Typeface.createFromFile(path);
edittext.setTypeface(tf);
you can use this:)
Upvotes: 1