In my project, I need to toggle TextView style to Normal and Bold. Here is the code:
mTitleTextView.setTypeface(mTitleTextView.getTypeface(), letterItem.isRead() ? Typeface.NORMAL : Typeface.BOLD);
This works good when TextView is not bold. But when current state is bold, it doesn't return to normal state.
mTitleTextView.setTypeface(null, letterItem.isRead() ? Typeface.NORMAL : Typeface.BOLD);
Above line fixes the problem, but I have used custom font and passing null for current typeface removes the font.
After tying for a while this works for me :
textView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
textView.invalidate();
if(isCliked){
isCliked = false;
Typeface face=Typeface.createFromAsset(getAssets(), "test.ttf");
textView.setTypeface(face,Typeface.NORMAL);
}
else{
Typeface face=Typeface.createFromAsset(getAssets(), "test.ttf");
textView.setTypeface(face, Typeface.BOLD);
isCliked = true;
}
Log.i("MainActivity", "onClick: "+isCliked);
}
});
the typeface will remain the same, and change only bold and normal
Related
I need to use text view as CheckBox background and set different color for each state. Some how I managed to get it with Java. But I don't think it is the
proper way. Is there any other method to achieve this?
Implement onClickListener to your textview, something like:
int ispressed = 0;
TextView txtview = (TextView) findViewbyId(R.id.textview1);
txtview.setOnClickListener(new new View.OnClickListener() {
#Override
public void onClick(View v) {
if(ispressed=0){
//CHANGE BACKGROUND COLOR
ispressed=1;
}else{
//RESTORE BACKGROUND COLOR
}
}
});
A TextView is displayed in one activity and the user goes to another activity to edit the properties of the text such as color, size, bold, italics, and underlined. When the user chooses to make the text bold, italic, or bold_italic it works. If the user unselects the checkbox the TextView does not return to normal. It can shift between the styles (ie if I check bold, then uncheck bold and check italic the TextView will be italic, not bold) but it cannot have a normal TypeFace.
I've searched around and everything I've found says that using the setTypeface method with Typeface.NORMAL should work but it is not working.
I would appreciate some help in solving this. The relevant code is below. Thank you!
TextView mDisplayMessage = (TextView) findViewById(R.id.message);
mDisplayMessage.setTypeface(mDisplayMessage.getTypeface(), Typeface.NORMAL);
//TODO: Cannot return all the way back to normal. Remains bold/italic/bold_italic
if (mIsBold && mIsItalic) mDisplayMessage.setTypeface(mDisplayMessage.getTypeface(), Typeface.BOLD_ITALIC);
else if (mIsBold) mDisplayMessage.setTypeface(mDisplayMessage.getTypeface(), Typeface.BOLD);
else if (mIsItalic) mDisplayMessage.setTypeface(mDisplayMessage.getTypeface(), Typeface.ITALIC);
else mDisplayMessage.setTypeface(mDisplayMessage.getTypeface(), Typeface.NORMAL);
if (mIsUnderlined) {
mDisplayMessage.setPaintFlags(mDisplayMessage.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
} else {
mDisplayMessage.setPaintFlags(0);
}
If you didn't care about font family you can use
mDisplayMessage.setTypeface(Typeface.DEFAULT);
Because Typeface.DEFAULT change the font family(serif, sans-serif, monospace), but the below can keep the same font family with style changed.
Typeface typeface = mDisplayMessage.getTypeface();
int style = Typeface.NORMAL;
if(mIsBold && mIsItalic) {
style = Typeface.BOLD_ITALIC;
} else if(mIsBold) {
style = Typeface.BOLD;
} else if(mIsItalic) {
style = Typeface.ITALIC;
}
Typeface newTypeface = Typeface.create(typeface, style);
mDisplayMessage.setTypeface(newTypeface);
Try this,
mDisplayMessage.setTypeface(Typeface.DEFAULT);
OR
mDisplayMessage.setTypeface(null, Typeface.NORMAL);
I know we can change edit text font by using Typeface. But what about errors we set for edit text?
Look at codes below:
Typeface font = Typeface.createFromAsset(getAssets(), "fonts/ATaha.ttf");
private EditText mPasswordView;
mPasswordView = (EditText) findViewById(R.id.password);
mPasswordView.setTypeface(font);
With this code I could only change edit text font but when I set error like this:
mPasswordView.setError(getString(R.string.error_field_required));
The error notification font is android default font and didn't change by using type face. How can I change that?
You can use a SpannableString to set the font:
SpannableString s = new SpannableString(errorString);
s.setSpan(new TypefaceSpan(font), 0, s.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
mPasswordView.setError(s);
A custom Span class that has a specific Typeface set:
public class TypefaceSpan extends MetricAffectingSpan {
private Typeface mTypeface;
public TypefaceSpan(Typeface typeface) {
mTypeface = typeface;
}
#Override
public void updateMeasureState(TextPaint p) {
p.setTypeface(mTypeface);
p.setFlags(p.getFlags() | Paint.SUBPIXEL_TEXT_FLAG);
}
#Override
public void updateDrawState(TextPaint tp) {
tp.setTypeface(mTypeface);
tp.setFlags(tp.getFlags() | Paint.SUBPIXEL_TEXT_FLAG);
}
}
Since you can't directly set a Typeface for error text, you can achieve it by setting an HTML string as a text inside it.
You can see HTML Tags supported by a TextView in The CommonsBlog
We have face attribute for font, which means you can change the font-family.
mPasswordView.setError(Html.fromHtml("<font face='MONOSPACE'>Error font is MONOSPACE</font>"));
By setting spannable string in error message or extend EditText and overrite your own error draw mechanism.
I am trying to assign a different font to my project.
I want the new font is valid for the entire project, but all I find is to change the font to a textview
Typeface font1 = Typeface.createFromAsset(getAssets(), "fonts/RockFont.ttf");
TextView customText1 = (TextView)findViewById(R.id.text1);
customText1.setTypeface(font1);
customText1.setTextSize(40.f);
customText1.setText("Hello! This is a custom font...");
There any way to default to the entire project a custom font?
Best regard
Not exactly what you asked for, but building on the comment above there are ways to make using a custom font with default controls easier.
This shows how to extend TextView and use a custom attribute so the TextView supports a custom font.
Custom fonts and XML layouts (Android)
What I do is create a support class and instantiate it from my activity and pass through all the views I wish to style.
public class textfactory{
private TextView tv;
private Button b;
private RadioButton rb;
private TypeFace font;
/**
* fetch font resource
*/
public textfactory(Context context){
this.font = Typeface.createFromAsset(context.getAssets(), 'customfont.ttf');
}
/**
* pass in all the views you wish to apply font to
*/
public void style(View... views){
for(View v : views){
if(v instance of TextView)
{
tv = (TextView)v;
tv.setTypeface(this.font);
}
else if(v instance of Button)
{
b = (Button)v;
b.setTypeface(this.font);
}
else if(v instance of RadioButton)
{
rb = (RadioButton)v;
rb.setTypeface(this.font);
}
//add as many view conditionals as required
}
}
}
Is there such a method call "getBackgroundColor" in TextView?
if I got 2 textViews: tv1 and tv2 in one LinearLayout. What I did:tv1.setBackgroundColor(Color.BLUE)
Now if I wanna setBackgroundColor of tv2 as the same as tv1, how can I get the backgroundColor in tv1 first and then setBackgroundColor of tv2?
There is a better solution than bourbons:
((ColorDrawable)view.getBackground()).getColor();
The advantage is we get an integer which is comparable to color enums given by Color class.
Setting a background color sets a Drawable with that specified color as the background, i.e. the following example will work just fine:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.some_layout_name);
TextView t1 = (TextView) findViewById(R.id.text1);
TextView t2 = (TextView) findViewById(R.id.text2);
t1.setBackgroundColor(Color.GREEN);
t2.setBackgroundDrawable(t1.getBackground());
}
You will find the solution here :
http://groups.google.com/group/android-developers/browse_thread/thread/4910bae94510ef77/59d4bb35e811e396?pli=1
It will be something like that :
((PaintDrawable) tv.getBackground()).getPaint()
There is no such method, because in common there is now "background color" - there can be any Drawable object(for example picture). So, you should remember what color do you set for text.
If you can't save it - use View.setTag() and View.getTag() methods to store any value, associated with view.
It works for me.
public static int getColor(View v) {
if(Build.VERSION.SDK_INT>=11)
{
return ((ColorDrawable)v.getBackground()).getColor();
}
else
{
try
{
Field f=View.class.getDeclaredField("mState");
f.setAccessible(true);
Object mState=f.get(v);
Field f2=mState.getClass().getDeclaredField("mUseColor");
f2.setAccessible(true);
return (int) f2.get(mState);
}
catch (Exception e)
{
}
}
return 0;
}