1

I already how to change the style of an element with selector but I found nothing about the typeface...

Is it possible to do with ? Or is there another way?

Ant-Roid
  • 17
  • 11
vital
  • 1,308
  • 3
  • 13
  • 28

4 Answers4

1

Sadly, right now it is not possible.
There is a ticket for that in the Android code repo :
https://code.google.com/p/android/issues/detail?id=8941

Your best option is to manage it yourself in ontouch listeners (and yes this is ugly) or implement these new selectors yourself.

Teovald
  • 4,369
  • 4
  • 26
  • 45
1

When button is clicked,Use below code to change font of the text(custom typeface).

Put your font under main->assets->fonts directory.

//change font when button pressed

val typeFace = Typeface.createFromAsset(activity!!.assets,"fonts/sf_semi_bold.ttf")
buttonAbout.setTypeface(typeFace)
0

Typeface can easily be an element of your style... If you're using default android styles, then the idea would be to extend whatever style you're implementing and just change the elements you need. like the following style element, taken from the android styles and themes documentation:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="CodeFont" parent="@android:style/TextAppearance.Medium">
        <item name="android:layout_width">fill_parent</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:textColor">#00FF00</item>
        <item name="android:typeface">monospace</item>
    </style>
</resources>

Then apply this style in your selector, just like you already know how to do.

The other option is, of course, to do it in code, but the selector is much cleaner

JRaymond
  • 11,625
  • 5
  • 37
  • 40
  • Sure, this works with monospace... What if I want to use a special font? Usually, I load it from the asset in Java code but with XML, I don't really know how to do it... – vital Mar 28 '12 at 21:25
  • Ah, I see... Unfortunately it's not possible to do through XML unless you subclass TextView to handle a custom attribute in your own namespace, kind of like what they're doing [here](http://stackoverflow.com/questions/2973270/using-a-custom-typeface-in-android) (second answer), and then applying that same namespace in your style attribute. – JRaymond Mar 28 '12 at 22:06
  • How do we use style in selector? – berserk Mar 15 '17 at 13:43
-2

TextView.setTypeFace() Use Ctrl+Space to show a giant list of classes for TextView or anything in general on.

Andy
  • 49,085
  • 60
  • 166
  • 233
sdfwer
  • 1,042
  • 3
  • 10
  • 23
  • Yes, I know that... I have already set a new font BUT I would like to change the font when the user press the TextView... Is there a way to do it with selector or do I have to use an onTouchListener? – vital Mar 28 '12 at 21:22