retrieve custom char with charcode android - android

I'm using a custom font with characters like star, sun, etc.
I know their charcode into this font.
I set the TextView font with the following code:
public class MyTextView extends TextView {
public MyTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public MyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public MyTextView(Context context) {
super(context);
init();
}
public void init() {
Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "font/myFont.ttf");
setTypeface(tf ,1);
}}
is there a way, since I know their charcode, to use those specific characters directly in strings.xml and programmatically?

You can reference characters by their 4 digit hexadecimal code in java :
String someString = "\u2776"
Or in resources :
<string name="some_string_name">\u2776</string>
(2776 is the code for ❶ in the default font)

Related

Font awesome text is not solid in Android

I am trying to show hand pointer icon which is https://fontawesome.com/icons/hand-pointer?style=regular I want to show it solidly(white background) like this one https://fontawesome.com/icons/hand-pointer?style=solid .
But here I have a problem that both of them has the same unicode which is f25a.
//This is the FontAwesomeTextView I Created
FontAwesomeTextView fingerPointer = new FontAwesomeTextView(MyApplication.getStaticContext());
fingerPointer.setText(MyApplication.getStaticContext().getString(R.string.finger_pointer));
fingerPointer.setTextColor(ContextCompat.getColor(MyApplication.getStaticContext(), R.color.colorWhite));
fingerPointer.setShadowLayer(2,2,2,R.color.colorOrange);
fingerPointer.setTextSize(30f);
fingerPointer.setVisibility(View.GONE);
this.fingerPointer = fingerPointer;
//This is the hand pointer text that I am using
<string name="finger_pointer" translatable="false"></string>
As you can see icon i send in link has the same unicode in string.xml.
This is my FontAwesomeTextView which is pretty simple class that loads fontawesome typeface and set it.
public class FontAwesomeTextView extends AppCompatTextView {
public FontAwesomeTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public FontAwesomeTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public FontAwesomeTextView(Context context) {
super(context);
init();
}
private void init() {
Typeface typeface = FontCache.get(FontCache.FONT_AWESOME,getContext());
setTypeface(typeface);
}
}

How do I change a Button font after it's added to a ViewGroup?

Given a Button created at runtime:
Button button = Button(context)
The way to set a custom typeface is:
button.setTypeface(myTypeface)
However I find it only works before I add it to a ViewGroup and not after.
I've also tried:
button.setTypeface(myTypeface, myStyle)
but it didn't work either. I need to change my Button font dynamically. I've tried invalidate() and requestLayout() but the font never changes.
Solution:- you can subclass the Button class with your custom font and use it instead of button.
public class MyButton extends AppCompatButton {
public MyButton(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public MyButton(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public MyButton(Context context) {
super(context);
init();
}
private void init() {
if (!isInEditMode()) {
Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "font.ttf");
setTypeface(tf);
}
}
}

how to specify the font file in the settings instead of code?

I'm trying to decorate my application fonts. I get to do it from code. but do not know how to do it in the settings. in the code I do so
public class Fonts {
public static Typeface getHeaderFont(Context context){
return Typeface.createFromAsset(context.getAssets(), "header_levelI.ttf");
}
public static Typeface getSubHeaderFont(Context context){
return Typeface.createFromAsset(context.getAssets(), "header_levelII.ttf");
}
}
and
Typeface type= Fonts.getHeaderFont(getActivity());
TextView header = (TextView) v.findViewById(R.id.cassaName);
header.setTypeface(type);
settings android studio only standard fonts. How do I specify fonts in the file not to write extra code?
Create Custom TextView with your custom font.
Note: It's important that you place required font files in the assets/fonts directory.
public class CustomTextView extends TextView {
public CustomTextView(Context context) {
super(context);
setFont();
}
public CustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);
setFont();
}
public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setFont();
}
private void setFont() {
Typeface font = Typeface.createFromAsset(getContext().getAssets(),"fonts/ss-symbolicons-line.ttf");
setTypeface(font, Typeface.NORMAL);
}
}

Change font of entire application [duplicate]

This question already has answers here:
How to set default font family for entire Android app
(16 answers)
Custom Fonts and Custom Textview on Android
(3 answers)
Closed 8 years ago.
I want to use custom font in my application. Which is the best way to give font to entire application. I know how to assign custom font to a single TextView or Button.
Is it possible to mention the custom font in one place, for eg. in styles.xml, and the font will be applied to whole application (every TextView, Button, EditText and soon).
use this type of textview in your app
public class MyTextView extends TextView {
public MyTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public MyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public MyTextView(Context context) {
super(context);
init();
}
public void init() {
Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "font/chiller.ttf");
setTypeface(tf ,1);
}
}
also edit text
public class CEditText extends EditText {
private Context context;
private AttributeSet attrs;
private int defStyle;
public CEditText(Context context) {
super(context);
this.context=context;
init();
}
public CEditText(Context context, AttributeSet attrs) {
super(context, attrs);
this.context=context;
this.attrs=attrs;
init();
}
public CEditText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
this.context=context;
this.attrs=attrs;
this.defStyle=defStyle;
init();
}
private void init() {
Typeface font=Typeface.createFromAsset(getContext().getAssets(), "fonts/myfont.ttf");
this.setTypeface(font);
}
#Override
public void setTypeface(Typeface tf, int style) {
tf=Typeface.createFromAsset(getContext().getAssets(), "fonts/myfont.ttf");
super.setTypeface(tf, style);
}
#Override
public void setTypeface(Typeface tf) {
tf=Typeface.createFromAsset(getContext().getAssets(), "fonts/myfont.ttf");
super.setTypeface(tf);
}
Unfortunatly, android is not providing any method to apply custom font for the complete app in one place.
However, you can create your CustomTextView extends TextView and CustomButton extends Button. In that you can set the font by creating a FontInstance.

Set textcolor and other attribute in textview extends

How can i set text color and text size in the class below
public class MyTextView extends TextView {
public MyTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public MyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public MyTextView(Context context) {
super(context);
init();
}
public void init() {
Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "font/chiller.ttf");
setTypeface(tf ,1);
}
}
In your init use setTextColor and setTextSize
public void init() {
Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "font/chiller.ttf");
setTypeface(tf ,1);
this.setTextColor(Color.RED);
this.setTextSize(20f);
}
If you are looking for custom attributes check this
Setting color of a Paint object in custom view

Categories

Resources