Replace text in costomized TextView - android

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.

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

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" />

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.

Android + Inflate layout within Custom Button

I need every instance of a custom Button to use a FrameLayout as it's layout. How do I do this from my custom Button class, as to avoid having to wrap every xml defined CustomButton in a FrameLayout?
public CustomButton(Context context) {
super(context);
init(context);
}
public CustomButton(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public CustomButton(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context);
}
private void init(Context context) {
//this doesn't work...
FrameLayout layout = new FrameLayout(context);
inflate(context, R.layout.frame_layout, null);
}
just put your FrameLayout arount your Custom button in frame_layout and later in other xml you can put it in as and this view will be as it is in frame_layout

Change the font of all the textviews in my application?

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.

Categories

Resources