Change the font of all the textviews in my application? - android

I wonder if there is a way to change the font of all the text views in an android application in single time ? By all the text views i mean programmatically or dynamically created text views and the text views created separately using XML layout files (Drag n drop)?
I know i can create a new theme with different required fonts and use it . But i can only see the theme applies to dynamically created text views within the program but not the one's in XML layout .
Can you please let me know if there is any solution for this or the only option is to change the font of each text view manually.

The easiest way is to extend the TextView widget:
public class FontTextView extends TextView {
private String mTypefacePath;
public FontTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setAttrs(context, attrs, defStyle);
init(context);
}
public FontTextView(Context context, AttributeSet attrs) {
super(context, attrs);
setAttrs(context, attrs, 0);
init(context);
}
public FontTextView(Context context) {
super(context);
init(context);
}
private void setAttrs(Context context, AttributeSet attrs, int defStyle) {
if (isInEditMode())
return;
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.FontTextView, defStyle, 0);
mTypefacePath = a.getString(R.styleable.FontTextView_typeface);
a.recycle();
}
private void init(Context context) {
if (!TextUtils.isEmpty(mTypefacePath)) {
try {
setTypeface(Typeface.createFromAsset(context.getAssets(),
mTypefacePath));
} catch (Exception ex) {
// could not create the typeface from path
}
}
}}
You also need to define your typeface attribute.
Take a look on this to see a useful explanation about.

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);
}
}

Replace text in costomized TextView

I am trying to replace the text in a customized text view. the purpose is to force the text to be LTR; In order to do that, I am adding the "\u200E" prefix to my text.
Right now the setText() does not take any affect on the customized textView.
public class LocalizedTextView extends TextView {
public LocalizedTextView(Context context) {
super(context);
init(context);
}
public LocalizedTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public LocalizedTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context);
}
private void init(Context context) {
this.setText("\u200E" + getText().toString());
}
}
And this is the customized element:
<com.tempProject.customviews.LocalizedTextView
android:id="#+id/item_text"
style="#style/DrawerSectionText"
android:textDirection="ltr" />
Any Idea to force textView with RTL content to align to the left for devices older than 4.2.0, will be more than welcome.
The android:supportsRtl flag has to be set to true in the manifest.

Define a Custom EditText

I'm a beginner. I want to define the xml for a custom EditText and then programmatically add those custom edittexts at runtime without using a bunch of code to customize the editTexts. This could be similar to implementing custom buttons, textviews, etc. from libraries...although it would be my own. What is the best way to approach this?
Thanks!
Alternative code apart from code shared in above comment link is below :
basically this code makes easy for user to customize the fonts etc.
public class MyEditText extends EditText {
public MyEditText(Context context) {
super(context);
}
public MyEditText(Context context, AttributeSet attrs) {
super(context, attrs);
parseAttributes(context, attrs);
}
public MyEditText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
parseAttributes(context, attrs);
}
private void parseAttributes(Context context, AttributeSet attrs) {
TypedArray values = context.obtainStyledAttributes(attrs, R.styleable.Simplified);
int typefaceValue = values.getInt(R.styleable.Simplified_typeface, 0);
values.recycle();
setTypeface(MyFontUtil.obtaintTypeface(context, typefaceValue));
}
}
XML
<com.my.mtetno.widget.MyEditText
android:id="#+id/uname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/lh_edit_field_without_border"
android:inputType="textEmailAddress"
android:maxLines="1"
android:overScrollMode="always"
android:textSize="#dimen/login_page_edit_text_size"
app:typeface="simplified_regular" />

Custom Views not showing in Graphical Layout

I created a custom Button, TextView, and ImageView. None of these appear properly in the Graphical Layout of any XML. Instead of showing a button or text with a custom font, it instead shows a huge grey box with the name of the custom class I'm calling. How do I get these to show in the preview?
public class FontTextView extends TextView {
public static Typeface FONT_NAME;
public FontTextView(Context context) {
super(context);
if(FONT_NAME == null) FONT_NAME = Typeface.createFromAsset(context.getAssets(), "font.ttf");
this.setTypeface(FONT_NAME);
}
public FontTextView(Context context, AttributeSet attrs) {
super(context, attrs);
if(FONT_NAME == null) FONT_NAME = Typeface.createFromAsset(context.getAssets(), "font.ttf");
this.setTypeface(FONT_NAME);
}
public FontTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
if(FONT_NAME == null) FONT_NAME = Typeface.createFromAsset(context.getAssets(), "font.ttf");
this.setTypeface(FONT_NAME);
}
and
<com.example.gesturetest.FontTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text placeholder" />
You can do this in your Custom View:
if(!isInEditMode()){
// Your custom code that is not letting the Visual Editor draw properly
// i.e. thread spawning or other things in the constructor
}
Reference
do this in the constructor of your customView
public CustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);
if (!isInEditMode()) {
createTypeface(context, attrs); //whatever added functionality you are trying to add to Widget, call that inside this condition.
}
}

Android - Create Custom View

I want to create a custom view a thing like a power switch ( a switch that switches between ON and OFF). When I have started to implement it I faced 3 constructors for View class:
public CusatomView(Context context) {
super(context);
}
public CustomView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
Now my question is: Which one of these constructors I should complete it to retrieve my own XML attribute (for instance: textOn and textOff)?
And what is the role of each?
Ideally, you should do your stuff in a separate method and call this from all three constructors, because you never know which of the constructor will be called. Here are the roles:
CusatomView(Context context) creates a new view with no attributes initialized.
CustomView(Context context, AttributeSet attrs) is invoked when you set attributes like layout_height or layout_width in your layout.xml
CustomView(Context context, AttributeSet attrs, int defStyle) is used when you set styles to your view.
You should create another funciton init and call it in all.
public CusatomView(Context context) {
super(context);
init();
}
public CustomView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public CustomView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
The thing is any of this constructors can be used to instantiate your custom view. As in when you create a view in java code you just provide context and when it is created from xml attrs is also supplied.

Categories

Resources