I want to change font-family
of textview
in whole app
Currently I'm using below code
i have added font
inside my res/font
folder (Click here to check res/font directory)
Here is my font.xml
files
font/font_one
<?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">
<font
android:font="@font/oswald_regular"
android:fontStyle="normal"
android:fontWeight="400"
app:font="@font/oswald_regular"
app:fontStyle="normal"
app:fontWeight="400" />
<font
android:font="@font/oswald_bold"
android:fontStyle="italic"
android:fontWeight="400"
app:font="@font/oswald_bold"
app:fontStyle="italic"
app:fontWeight="400" />
</font-family>
@font/font_two
<?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">
<font
android:font="@font/opensans_regular"
android:fontStyle="normal"
android:fontWeight="400"
app:font="@font/opensans_regular"
app:fontStyle="normal"
app:fontWeight="400" />
<font
android:font="@font/opensans_bold"
android:fontStyle="italic"
android:fontWeight="400"
app:font="@font/opensans_bold"
app:fontStyle="italic"
app:fontWeight="400" />
</font-family>
here is my xml layout
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/sampleTextView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_weight="1"
android:background="?backColor"
android:fontFamily="@font/font_two"
android:padding="20dp"
android:text="@string/app_name"
android:textColor="?textColor"
android:textStyle="normal" />
<TextView
android:id="@+id/sampleTextView2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_weight="1"
android:background="?backColor"
android:fontFamily="@font/font_one"
android:padding="20dp"
android:text="@string/app_name"
android:textColor="?textColor"
android:textStyle="italic" />
</LinearLayout>
<Spinner
android:id="@+id/fontSpinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp" />
<Spinner
android:id="@+id/themeSpinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp" />
<Button
android:id="@+id/btnChaneFont"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="@drawable/test"
android:text="Change Font"
android:textColor="?textColor" />
<Button
android:id="@+id/btnChaneTheme"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="@drawable/test"
android:text="Change Theme"
android:textColor="?textColor" />
</LinearLayout>
here is my output when i'm using
android:fontFamily
its woking fine as per my requirement
OUTPUT using android:fontFamily="@font/font_one"
from XML
OUTPUT using android:fontFamily="@font/font_two"
from XML
here is the code of my MainActivity
public class MainActivity extends AppCompatActivity implements View.OnClickListener, AdapterView.OnItemSelectedListener {
Spinner fontSpinner, themeSpinner;
Button btnChaneFont, btnChaneTheme;
TextView sampleTextView, sampleTextView2;
int theme = R.style.AppTheme;
String[] fontsArray = new String[]{"Oswald Fonts", "Open Sans Fonts", "Raleway Fonts"};
String[] themeArray = new String[]{"Theme 1", "Theme 2", "Theme 3"};
Typeface typeface = null;
PrefManager prefManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
new ThemeColors(this);
prefManager = new PrefManager(this);
getTheme().applyStyle(prefManager.getTheme(), true);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init() {
fontSpinner = findViewById(R.id.fontSpinner);
themeSpinner = findViewById(R.id.themeSpinner);
btnChaneFont = findViewById(R.id.btnChaneFont);
btnChaneTheme = findViewById(R.id.btnChaneTheme);
btnChaneFont.setOnClickListener(this);
btnChaneTheme.setOnClickListener(this);
fontSpinner.setOnItemSelectedListener(this);
themeSpinner.setOnItemSelectedListener(this);
sampleTextView = findViewById(R.id.sampleTextView);
sampleTextView2 = findViewById(R.id.sampleTextView2);
ArrayAdapter<String> fontAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, fontsArray);
fontSpinner.setAdapter(fontAdapter);
ArrayAdapter<String> themeAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, themeArray);
themeSpinner.setAdapter(themeAdapter);
}
@Override
public void onClick(View view) {
if (view == btnChaneTheme) {
recreate();
}
if (view == btnChaneFont) {
sampleTextView.setTypeface(typeface);
sampleTextView2.setTypeface(typeface);
}
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int i, long l) {
switch (parent.getId()) {
case R.id.fontSpinner:
switch (i) {
case 0:
typeface = ResourcesCompat.getFont(MainActivity.this, R.font.font_one);
break;
case 1:
typeface = ResourcesCompat.getFont(MainActivity.this, R.font.font_two);
break;
case 2:
typeface = ResourcesCompat.getFont(MainActivity.this, R.font.font_three);
break;
}
break;
case R.id.themeSpinner:
switch (i) {
case 0:
prefManager.setTheme(R.style.AppTheme);
break;
case 1:
prefManager.setTheme(R.style.AppTheme2);
break;
case 2:
prefManager.setTheme(R.style.AppTheme3);
break;
}
break;
default:
break;
}
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
}
Now when i'm changing the typeface of text view progrmatically using java I'm getting below result
OUTPUT using font/font_two" from JAVA
OUTPUT using font/font_two" from JAVA
Currently i'm facing below issue using above code
- When i'm setting
font-family
usingXML
i'm getting both font effect normal and italic but when i'm settingfont-family
usingjava
the italic font doesn't setting correctly (you can check the result of both i have added the screenshot) - how can set
font-family
usingjava
- is there any way to set for whole application
- How can get the same behaviour of
font-family
asXML
usingjava
Can any body help me on this i want to change font-family
of whole app
here are some other post that i have visited but didn't help me to solve my problem
- How to change fontFamily of TextView in Android
- Valid values for android:fontFamily and what they map to?
- Android - set TextView TextStyle programmatically?
- Set TextView style (bold or italic)
- https://developer.android.com/guide/topics/ui/look-and-feel/fonts-in-xml
- set font at runtime, Textview
- Changing all typefaces of an android application
If need more information please do let me know. Thanks in advance. Your efforts will be appreciated.