How to set a particular font for a button text in android?
If you plan to add the same font to several buttons I suggest that you go all the way and implement it as a style and subclass button:
public class ButtonPlus extends Button {
public ButtonPlus(Context context) {
super(context);
}
public ButtonPlus(Context context, AttributeSet attrs) {
super(context, attrs);
CustomFontHelper.setCustomFont(this, context, attrs);
}
public ButtonPlus(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
CustomFontHelper.setCustomFont(this, context, attrs);
}
}
This is a helper class to set a font on a TextView (remember, Button is a subclass of TextView) based on the com.my.package:font attribute:
public class CustomFontHelper {
/**
* Sets a font on a textview based on the custom com.my.package:font attribute
* If the custom font attribute isn't found in the attributes nothing happens
* @param textview
* @param context
* @param attrs
*/
public static void setCustomFont(TextView textview, Context context, AttributeSet attrs) {
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CustomFont);
String font = a.getString(R.styleable.CustomFont_font);
setCustomFont(textview, font, context);
a.recycle();
}
/**
* Sets a font on a textview
* @param textview
* @param font
* @param context
*/
public static void setCustomFont(TextView textview, String font, Context context) {
if(font == null) {
return;
}
Typeface tf = FontCache.get(font, context);
if(tf != null) {
textview.setTypeface(tf);
}
}
}
And here's the FontCache to reduce memory usage on older devices:
public class FontCache {
private static Hashtable<String, Typeface> fontCache = new Hashtable<String, Typeface>();
public static Typeface get(String name, Context context) {
Typeface tf = fontCache.get(name);
if(tf == null) {
try {
tf = Typeface.createFromAsset(context.getAssets(), name);
}
catch (Exception e) {
return null;
}
fontCache.put(name, tf);
}
return tf;
}
}
In res/values/attrs.xml we define the custom styleable attribute
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="CustomFont">
<attr name="font" format="string"/>
</declare-styleable>
</resources>
And finally an example use in a layout:
<com.my.package.buttons.ButtonPlus
style="@style/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_sometext"/>
And in res/values/style.xml
<style name="button" parent="@android:style/Widget.Button">
<item name="com.my.package:font">fonts/copperplate_gothic_light.TTF</item>
</style>
This may seem like an awful lot of work, but you'll thank me once you have couple of handfuls of buttons and textfields that you want to change font on.
Android: Custom font works for TextView, not for Button
Add a style to your button:
Create a new style under styles.xml
:
<style name="main_button">
<item name="android:fontFamily">@font/my-custom-font</item>
<item name="android:background">@drawable/btn_blue</item>
<item name="android:textColor">#FFFF</item>
<item name="android:textAllCaps">false</item>
<item name="android:foreground">?attr/selectableItemBackground</item>
</style>
enter whatever values you want. Make sure to include the fontFamily:
Then on your button, add the style:
<Button
android:id="@+id/id"
style="@style/main_button" />
The font style of the button text has changed for the AppCompatActivity
The change happens because of Google's Material Design theme.
Basically, when you change your Activity
to AppCompatActivity
, you are required to also change your app's theme to extend from Theme.AppCompat
. This change causes the design for every view to match the theme.
Below are the example between Holo
theme and AppCompat
theme:
As you can see, the button's default implementation in AppCompat
theme uses all caps, so you will need to change those.
A simple fix would be to add these in your styles xml:
<style name="MyAppTheme" parent="Theme.AppCompat">
<item name="buttonStyle">@style/MyAppTheme.Button</item>
<item name="android:buttonStyle">@style/MyAppTheme.Button</item>
</style>
<style name="MyAppTheme.Button" parent="Base.Widget.AppCompat.Button">
<item name="android:textAllCaps">false</item>
</style>
set a custom font for a button text in Xamarin Android
Instead of of creating a different style fontFamily should be used in the button style and the code should look like this.
<style name="ButtonStyle" parent="@style/Widget.AppCompat.Button.Borderless">
<item name="android:textSize">@dimen/button_text_size</item>
<item name="android:background">@drawable/button_background</item>
<item name="android:fontFamily">@font/my_custom_font</item>
<item name="android:textColor">?android:textColorPrimaryInverse</item>
</style>
Setting Button text font in android
First you have to put the ttf file in assets folder and then You can use the below code to set Custom font in TextView, same way you can do for Button:
TextView txt = (TextView) findViewById(R.id.custom_font);
Typeface font = Typeface.createFromAsset(getAssets(), "Helv Neue 67 Med Cond.ttf");
txt.setTypeface(font);
How to use two different font sizes for button text in Android?
Hopefully this will help. This off course is not the only solution but it is a pretty simple one. Add the xml part in your xml instead of the button.
XML
<LinearLayout
android:id="@+id/button_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="5"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
android:text="ABC"/>
</LinearLayout>
CODE
LinearLayout button= (LinearLayout)findViewById(R.id.button_layout);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO DO whatever you want to do here
}
});
Android Help with Changing Button Font Type, How?
Button IS-A TextView, so just do it as with a TextView:
Button txt = (Button) findViewById(R.id.custom_font);
Typeface font = Typeface.createFromAsset(getAssets(), "1543Humane_jenson_bold.TTF");
txt.setTypeface(font);
Related Topics
Programmatically Adding Tablerow to Tablelayout Not Working
How to Trigger Broadcast Receiver When Gps Is Turn On/Off
Simple Example for Intent and Bundle
Save Image to Sdcard from Drawable Resource on Android
Convert Magnetic Field X, Y, Z Values from Device into Global Reference Frame
Android.Content.Context.Getpackagename()' on a Null Object Reference
A Way to Get Unlock Event in Android
Android Broadcast Receiver for Sent Sms Messages
Checking Internet Connection on Android
How to Dynamically Remove Items from Listview on a Button Click
Connecting 2 Emulator Instances in Android
Flutter Run Error:You Have Not Accepted the License Agreements
In Androidx.Fragment.App.Fragment,Setuservisiblehint()Is Deprecated,And Not Executed,Why