Reputation: 572
I'm pretty new in java and android, the problem that i'm facing is that i can't change the font of another layout.
okay i have a ListView
and i use the ArrayAdapter
to connect another layout to my ListView
. like image bellow:
I copy my font to assets
folder and write this inside onCreate
:
// list of titles
String[] pizza_titles = {"BBQ Bacon Cheeseburger", "Old-Fashioned Meatbrawl", "7-Alarm Fire", "Sweet Sriracha Dynamite","Cock-A-Doodle Bacon"};
// getting the ListView ID
pizza_list_view = (ListView) findViewById(R.id.listView_Pizza);
// creating ArrayAdapter and connecting the layout to ListView as well as list of string to a TextView
ArrayAdapter<String> adapter=new ArrayAdapter<String (this,R.layout.food_row,R.id.text_food_title,pizza_titles);
pizza_list_view.setAdapter(adapter);
// defining the font typeface
Typeface pizza_title_font = Typeface.createFromAsset(getAssets(),"pizza.ttf");
TextView pizza_title = (TextView) findViewById(R.id.text_food_title);
pizza_title.setTypeface(pizza_title_font );
once i run this i get error, i guess because i set the setTypeface
to layout that connected to ListView
, but if i setTypeface
to a TextView
that is in my activity_main
, it will work perfectly. like image bellow.
so what am i missing? and how can i solve this issue?
Edit_1: here the xml code of food_row
layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:src="@drawable/food_preview"
android:id="@+id/image_food"
android:layout_width="100dp"
android:layout_height="100dp"/>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="@+id/text_food_title" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/text_food_description" />
</LinearLayout>
</LinearLayout>
here the activity_main
xml code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="domain.bernard.task_02.MainActivity"
android:weightSum="1">
<ListView
android:layout_width="match_parent"
android:layout_height="317dp"
android:id="@+id/listView_Pizza"
android:layout_gravity="center_horizontal" />
<TextView
android:layout_width="302dp"
android:layout_height="48dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/textView2" />
</LinearLayout>
Update:
Upvotes: 0
Views: 5891
Reputation: 14825
You need to create your own Custom Adapter and in getView
of your Adapter you can set the fonts, here is the example
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
if (convertView == null)
vi = lif.inflate(R.layout.inflate, null);
imageView = (ImageView) vi.findViewById(R.id.imageForImageView);
TextView pizza_title = (TextView) findViewById(R.id.text_food_title);
pizza_title.setText("Your text");
final Typeface pizza_title_font = Typeface.createFromAsset(getAssets(),"pizza.ttf");
pizza_title.setTypeface(pizza_title_font);
return vi;
}
if you dunno how to create Custom Adapter then you can use this library for that, its pretty straight forward
Include the dependency in your gradle.build
dependencies {
compile 'uk.co.chrisjenx:calligraphy:2.2.0'
}
create a folder in your assets folder named fonts, e.g assets/fonts
and move your fonts in that folder and then in your TextView
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
fontPath="fonts/pizza.ttf"
android:id="@+id/text_food_title" />
Remeber: it won't give you live preview of your fonts in XML
preview but you can check it by running your app in emulate or device.
Upvotes: 2
Reputation: 16587
I recommend to fix some part of your code :
Upvotes: 1