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;
}
}
Roboto is the default font on Android, and since 2013, other Google services such as Google Play, YouTube, Google Maps, and Google Images.
If you are using Android Studio 3.5+, Changing the font is super simple. Select the text widget on the Design view and check the font family on Attribute Window. The value dropdown contains all the available fonts from which you can select one. If you are looking for Google Fonts, Click the More Fonts option.
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;
}
}
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 :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With