Use Typeface with EditText in widget - android

Is it possible to use somehow Typeface font definition loaded from assets with EditText in widget ?

The font you want to use needs to reside in the assets/fonts directory, and you access it like so:
Typeface myFont = Typeface.createFromAsset(getAssets(), "fonts/myfont.ttf");
edittext.setTypeface(myFont);

editText.setTypeface(Typeface.createFromAsset(context.getAssets(),"fonts/myfont.ttf"));
Assuming you have this structure of files:
/assets/fonts/myfont.ttf

Please See below code for that, it will solve your problem.
// text view label
TextView mTextView1 = (TextView) findViewById(R.id.TextView1);
// Loading Font Face
Typeface tf = Typeface.createFromAsset(getAssets(), "DroidSansFallback.ttf");
// Applying font
mTextView1.setTypeface(tf);
And see below link for more information.
Android Development – Customize Android Fonts

Typeface tf = Typeface.createFromAsset(getAssets(),"fonts/Comic.ttf");
youredittext.setTypeface(tf);
I have tried this now. It worked for me. Good luck

Other betther form to implement this and avoid to add the font to all the textviews is extends a TextView (or EditText or ... ) and apply the font on the setTypeface method. With this method you can control bold, italic and other styles.
Here is a code for a class that extends TextView and apply a Roboto font. Moreover it controls some bugs that Android 4.0 has with the html codes when set an Spannable from an HTML
public class TextViewRoboto extends TextView {
public static final String TAG = "TextViewRoboto";
public TextViewRoboto(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public TextViewRoboto(Context context, AttributeSet attrs) {
super(context, attrs);
}
public TextViewRoboto(Context context) {
super(context);
}
#Override
public void setTypeface(Typeface tf, int style) {
//This is to override eclipse error messages
if (!super.isInEditMode()) {
if (style == Typeface.BOLD)
super.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/Roboto-Bold.ttf"));
else if (style == Typeface.ITALIC)
super.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/Roboto-Italic.ttf"));
else
super.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/Roboto-Regular.ttf"));
}
}
//
// With this code aboid the <b> and <strong> problem on Jelly Bean
//
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
try{
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}catch (ArrayIndexOutOfBoundsException e){
//Logger.w(TAG, "Problem onMeasure. Set normal text");
setText(getText().toString());
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
#Override
public void setGravity(int gravity){
try{
super.setGravity(gravity);
}catch (ArrayIndexOutOfBoundsException e){
//Logger.w(TAG, "Problem setGravity. Set normal text");
setText(getText().toString());
super.setGravity(gravity);
}
}
#Override
public void setText(CharSequence text, BufferType type) {
try{
super.setText(text, type);
}catch (ArrayIndexOutOfBoundsException e){
//Logger.w(TAG, "Problem on setText. Set normal text");
setText(text.toString());
}
}
public void setHTMLText(CharSequence text, BufferType type) {
String tmpText = text.toString();
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
tmpText = tmpText.replace("<strong>", "<b>");
tmpText = tmpText.replace("</strong>", "</b>");
tmpText = tmpText.replace("<em>", "<i>");
tmpText = tmpText.replace("</em>", "</i>");
text = tmpText;
}
try{
super.setText(Html.fromHtml(tmpText), type);
}catch (ArrayIndexOutOfBoundsException e){
//Logger.w(TAG, "Problem on setText. Set normal text");
setText(text.toString());
}
}
}

You can use this
editText.setTypeface(Typeface.SERIF);

Related

multiline edittext where parts are not editable, like fill in the blanks

I need to have a view which contains textview and edittext.
Example:
Yay! you made it to ______ We should hang out! feel ____ to follow me.
Above "_____" could be of any length and it should feel like a paragraph in the end. Rest of the text given above is not changeable. Just like fill in the blanks.
From my perspective, a fill-in-the-blank widget should do the following:
Allow only certain identified portions of the text to be changed. The rest of the text is locked.
Not allow cursor movement into the locked text.
Flow from line to line like EditText.
Be generalized with variable placement of blanks.
Here is an implementation of such a widget based upon EditText. Editable spans are set up using a span (BlanksSpan) extended from StyleSpan. A blank span is identified by five underscores ("_____") in the text. Cursor movement is controlled in OnSelectionChanged() and various EditText callbacks. Changes to the text is monitor by a TextWatcher and adjustments to the displayed text are made there.
Here is the video of the widget in use:
FillInBlanksEditText.java
public class FillInBlanksEditText extends android.support.v7.widget.AppCompatEditText
implements View.OnFocusChangeListener, TextWatcher {
private int mLastSelStart;
private int mLastSelEnd;
private BlanksSpan mSpans[];
private Editable mUndoChange;
private BlanksSpan mWatcherSpan;
public FillInBlanksEditText(Context context) {
super(context);
init();
}
public FillInBlanksEditText(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public FillInBlanksEditText(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
mSpans = setSpans();
setOnFocusChangeListener(this);
}
#Override
public void onRestoreInstanceState(Parcelable state) {
mSpans = null;
super.onRestoreInstanceState(state);
Editable e = getEditableText();
mSpans = e.getSpans(0, e.length(), BlanksSpan.class);
}
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
addTextChangedListener(this);
if (findInSpan(getSelectionStart(), getSelectionEnd()) != null) {
mLastSelStart = getSelectionStart();
mLastSelEnd = getSelectionEnd();
} else if (findInSpan(mLastSelStart, mLastSelEnd) == null) {
setSelection(getEditableText().getSpanStart(mSpans[0]));
}
} else {
removeTextChangedListener(this);
}
}
#Override
protected void onSelectionChanged(int selStart, int selEnd) {
if (!isFocused() || mSpans == null ||
(getSelectionStart() == mLastSelStart && getSelectionEnd() == mLastSelEnd)) {
return;
}
// The selection must be completely within a Blankspan.
final BlanksSpan span = findInSpan(selStart, selEnd);
if (span == null) {
// Current selection is not within a Blankspan. Restore selection to prior location.
moveCursor(mLastSelStart);
} else if (selStart > getEditableText().getSpanStart(span) + span.getDataLength()) {
// Acceptable location for selection (within a Blankspan).
// Make sure that the cursor is at the end of the entered data. mLastSelStart = getEditableText().getSpanStart(span) + span.getDataLength();
mLastSelEnd = mLastSelStart;
moveCursor(mLastSelStart);
} else {
// Just capture the placement.
mLastSelStart = selStart;
mLastSelEnd = selEnd;
}
super.onSelectionChanged(mLastSelStart, mLastSelEnd);
}
// Safely move the cursor without directly invoking setSelection from onSelectionChange.
private void moveCursor(final int selStart) {
post(new Runnable() {
#Override
public void run() {
setSelection(selStart);
}
});
// Stop cursor form jumping on move.
getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
#Override
public boolean onPreDraw() {
getViewTreeObserver().removeOnPreDrawListener(this);
return false;
}
});
}
#Nullable
private BlanksSpan findInSpan(int selStart, int selEnd) {
for (BlanksSpan span : mSpans) {
if (selStart >= getEditableText().getSpanStart(span) &&
selEnd <= getEditableText().getSpanEnd(span)) {
return span;
}
}
return null;
}
// Set up a Blankspan to cover each occurrence of BLANKS_TOKEN.
private BlanksSpan[] setSpans() {
Editable e = getEditableText();
String s = e.toString();
int offset = 0;
int blanksOffset;
while ((blanksOffset = s.substring(offset).indexOf(BLANKS_TOKEN)) != -1) {
offset += blanksOffset;
e.setSpan(new BlanksSpan(Typeface.BOLD), offset, offset + BLANKS_TOKEN.length(),
Spanned.SPAN_INCLUSIVE_INCLUSIVE);
offset += BLANKS_TOKEN.length();
}
return e.getSpans(0, e.length(), BlanksSpan.class);
}
// Check change to make sure that it is acceptable to us.
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
mWatcherSpan = findInSpan(start, start + count);
if (mWatcherSpan == null) {
// Change outside of a Blankspan. Just put things back the way they were.
// Do this in afterTextChaanged. mUndoChange = Editable.Factory.getInstance().newEditable(s);
} else {
// Change is OK. Track data length.
mWatcherSpan.adjustDataLength(count, after);
}
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// Do nothing...
}
#Override
public void afterTextChanged(Editable s) {
if (mUndoChange == null) {
// The change is legal. Modify the contents of the span to the format we want.
CharSequence newContents = mWatcherSpan.getFormattedContent(s);
if (newContents != null) {
removeTextChangedListener(this);
int selection = getSelectionStart();
s.replace(s.getSpanStart(mWatcherSpan), s.getSpanEnd(mWatcherSpan), newContents);
setSelection(selection);
addTextChangedListener(this);
}
} else {
// Illegal change - put things back the way they were.
removeTextChangedListener(this);
setText(mUndoChange);
mUndoChange = null;
addTextChangedListener(this);
}
}
#SuppressWarnings("WeakerAccess")
public static class BlanksSpan extends StyleSpan {
private int mDataLength;
public BlanksSpan(int style) {
super(style);
}
#SuppressWarnings("unused")
public BlanksSpan(#NonNull Parcel src) {
super(src);
}
public void adjustDataLength(int count, int after) {
mDataLength += after - count;
}
#Nullable
public CharSequence getFormattedContent(Editable e) {
if (mDataLength == 0) {
return BLANKS_TOKEN;
}
int spanStart = e.getSpanStart(this);
return (e.getSpanEnd(this) - spanStart > mDataLength)
? e.subSequence(spanStart, spanStart + mDataLength)
: null;
}
public int getDataLength() {
return mDataLength;
}
}
#SuppressWarnings({"FieldCanBeLocal", "unused"})
private static final String TAG = "FillInBlanksEditText";
private static final String BLANKS_TOKEN = "_____";
}
activity_main.java
A sample layout.
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.example.fillintheblanks.FillInBlanksEditText
android:id="#+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:background="#android:color/transparent"
android:inputType="textMultiLine"
android:padding="16dp"
android:text="Yay! You made it to _____. We should hang out! Feel _____ to follow me."
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.example.fillintheblanks.FillInBlanksEditText
android:id="#+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:background="#android:color/transparent"
android:inputType="textMultiLine"
android:padding="16dp"
android:text="_____ says that it is time to _____. Are you _____?"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="#id/editText" />
</android.support.constraint.ConstraintLayout>
A few things to note:
In extracted mode, cursor placement jumps around if a touch is made outside of a BlanksSpan. Things still work but misbehave a little.
The length of the blanks fields is fixed, but it can be made variable in length with some additional work.
The action mode in the control needs some work based upon requirements.
multiline edittext where parts are not editable, like fill in the blanks
You can use a TextWatcher() for this requirement
Try this he is the little work around for this
MainActivity
public class MainActivity extends AppCompatActivity {
EditText myEditText;
String startText = "I'm The First Part";
String lastText = "I'm The Last Part";
SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final SpannableStringBuilder firstStringBuilder = new SpannableStringBuilder(startText);
final SpannableStringBuilder lastStringBuilder = new SpannableStringBuilder(lastText);
StyleSpan firstStyleSpan = new StyleSpan(android.graphics.Typeface.BOLD);
firstStringBuilder.setSpan(firstStyleSpan, 0, firstStringBuilder.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE); // make first 4 characters Bold
lastStringBuilder.setSpan(firstStyleSpan, 0, lastStringBuilder.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE); // make first 4 characters Bold
myEditText = findViewById(R.id.myEditText);
spannableStringBuilder.append(firstStringBuilder);
spannableStringBuilder.append(" ");
spannableStringBuilder.append(lastStringBuilder);
myEditText.setText(spannableStringBuilder);
Selection.setSelection(myEditText.getText(), startText.length() + 1);
myEditText.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable s) {
if (!s.toString().startsWith(firstStringBuilder.toString())
|| !s.toString().contains(lastText)) {
Log.e("StringBuilder_TAG", spannableStringBuilder.toString());
myEditText.setText(spannableStringBuilder);
Selection.setSelection(myEditText.getText(), myEditText.getText().length() - lastStringBuilder.length() - 1);
} else {
spannableStringBuilder.clear();
spannableStringBuilder.append(s.toString());
Log.e("My_TAG", spannableStringBuilder.toString());
}
}
});
}
}
layout.activity_main
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="#+id/myEditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:padding="5dp" />
</LinearLayout>
Here is the output video of above code https://www.youtube.com/watch?v=pfhUzLiFD6U
using above code you able to make not editble first and last parts of editext
Note
You can also use a TextDrawable
here are some links for that
How to put text in a drawable?
https://github.com/amulyakhare/TextDrawable
Set unchangeable some part of editText android
You can also create a custom EditText for this
Adding a prefix to an EditText
You can also use InputFilter
Solution one
Try using flexbox-layout - https://github.com/google/flexbox-layout.
Solution Two
Use textWatcher
Solution Three
Use html, css and javascript to design a simple webpage.
Use webview to load the html file.
Follow #Cheticamp 's answer, it works when you set a SpannableString in a setText() method.
Also you should override setText() method and set Spans to mSpans:
#Override
public void setText(CharSequence text, BufferType type) {
mSpans = Editable.Factory.getInstance().newEditable(text).getSpans(0, text.length(), BlanksSpan.class);
Log.d(TAG, "setText: " + mSpans.length);
super.setText(text, type);
}
if you don't set a SpannableString, Editable interface will get a simple String without Spans.
So if you call methods like:
Editable.getSpanStart(tag) returns -1;
Editable.getSpanEnd(tag) returns -1;
Editable.getSpans(start, end, Class) returns empty array

Change TextInputLayout error font?

Is it possible to change the TextInputLayout error text font for an EditText?
I could only change the color or text size, via app:errorTextAppearance.
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);
}
}
Another method if you like it, you can set Error color or Font both
public static void setErrorTextColor(TextInputLayout textInputLayout, int color, Typeface font) {
try {
Field fErrorView = TextInputLayout.class.getDeclaredField("mErrorView");
fErrorView.setAccessible(true);
TextView mErrorView = (TextView) fErrorView.get(textInputLayout);
Field fCurTextColor = TextView.class.getDeclaredField("mCurTextColor");
fCurTextColor.setAccessible(true);
fCurTextColor.set(mErrorView, color);
mErrorView.setTypeface(font);
} catch (Exception e) {
e.printStackTrace();
}
}

How to make EditText read and copy only but not editable

I want to make my edit box read only but not editable.
User should able to copy from my Edit box but it should not be editable ny user.
please let me know how to do this.
The command text.setTextIsSelectable(true) requires API 11. For those using lower API's use the following XML:
android:inputType="none"
android:textIsSelectable="true"
This will make your editText selectable but not editable.
The easiest way to do this is to add this code:
textInput.setInputType(InputType.TYPE_NULL);
textInput.setTextIsSelectable(true);
textInput.setKeyListener(null);
Create a TextView as has been indicated by the other answer, instead of an EditText. Then override the Activity's context menu in your Activity class as below:
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
menu.add(0, v.getId(), 0, "Copy");
//cast the received View to TextView so that you can get its text
TextView yourTextView = (TextView) v;
//place your TextView's text in the clipboard
ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
clipboard.setText(yourTextView.getText());
}
Then simply call registerForContextMenu(yourTextView); in onCreate().
You can overwrite the key listener, so you can do anything except editing
final EditText edittext = (EditText) findViewById(R.id.edittext);
edittext.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
return true;
}
});
Use android:editable="false" property for the EditText in your layout view file.
i'm using this class
import android.content.Context;
import android.text.InputFilter;
import android.text.Spanned;
import android.util.AttributeSet;
import android.widget.EditText;
/*
*
* To make EditText read and copy only but not editable
* using
* sendReadOnlyCallback(callback);
*
*/
public class MyEditText extends EditText {
private InputFilter[] originalFilters = null;
private boolean internalChange = false;
private InputFilter[] myInputFilters = null;
private static ReadonlyCallback sDummyCallback = new ReadonlyCallback() {
#Override
public boolean isReadOnly() {
return false;
}
};
private ReadonlyCallback callback = sDummyCallback;
public MyEditText(Context context) {
super(context);
}
public MyEditText(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyEditText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public interface ReadonlyCallback {
public boolean isReadOnly();
}
public void setReadonlyCallback(ReadonlyCallback cb) {
if (cb == null)
callback = sDummyCallback;
else
callback = cb;
}
public void setFilters(InputFilter[] filters) {
// duplicated from TexView
originalFilters = new InputFilter[filters.length];
System.arraycopy(filters, 0, originalFilters, 0, filters.length);
// funny No. 1 : have to re instantiate `callback`
// otherwise got `NullPointerExcection` when called from `filter`
callback = sDummyCallback;
myInputFilters = new InputFilter[] { new InputFilter() {
// funny No. 2:
// have to make refs to `originalfilters`
// otherwise got `NullPointerExcection` when called from `filter`
InputFilter[] flts = originalFilters;
#Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
if (!internalChange && callback.isReadOnly())
return dest.subSequence(dstart, dend);
int filtercount = flts.length;
if (filtercount == 0)
return null;
// duplicated from InputFilter.AllCaps
for (int i = 0; i < filtercount; i++) {
CharSequence repl = flts[i].filter(source, start, end, dest, start, end);
if (repl != null) {
source = repl;
start = 0;
end = repl.length();
}
if (i == filtercount)
return repl;
}
return null;
}
} };
super.setFilters(myInputFilters);
}
#Override
public InputFilter[] getFilters() {
if (myInputFilters == null)
return super.getFilters();
return originalFilters;
}
#Override
public synchronized void setText(CharSequence text, BufferType type) {
internalChange = true;
super.setText(text, type);
internalChange = false;
}
}
Why not this?
final EditText edittext = (EditText) findViewById(R.id.edittext);
edittext.setEnabled(false);

Android custom fonts - for system components like Actionbar, Toast, Dialogs

I am working on application translated to languages that are not supported - missing fonts on devices.
So I added fonts to application and changed typeface for all TextViews, EditTexts, Buttons.
Also I was able to change fonts in WebView.
But now I need to change typeface for all system components:
ActionBar:
Title Dropdown
Menu
ShareActionProvider
ActionItems
ActionItems in overflow
Dialog
Toast
Settings Screen
Is there a way to change typeface for this components.
Probably all have some TextView in them - just to know their id to find them.
Please do not post answers about changing Typeface on TextView!
Create Views with custom fonts like this
FontManager.java
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import android.content.Context;
import android.content.res.XmlResourceParser;
import android.graphics.Typeface;
import android.view.InflateException;
public class FontManager
{
//Making FontManager a singleton class
private static class InstanceHolder
{
private static final FontManager INSTANCE = new FontManager();
}
public static FontManager getInstance()
{
return FontManager.InstanceHolder.INSTANCE;
}
private FontManager()
{
}
// Different tags used in XML file.
private static final String TAG_FAMILY = "family";
private static final String TAG_NAMESET = "nameset";
private static final String TAG_NAME = "name";
private static final String TAG_FILESET = "fileset";
private static final String TAG_FILE = "file";
// Different styles supported.
private static final String STYLE_BOLD = "-Bold.ttf";
private static final String STYLE_ITALIC = "-Italic.ttf";
private static final String STYLE_BOLDITALIC = "-BoldItalic.ttf";
private class FontStyle
{
int style;
Typeface font;
}
private class Font
{
// different font-family names that this Font will respond to.
List<String> families;
// different styles for this font.
List<FontStyle> styles;
}
private List<Font> mFonts;
//private boolean isFamilySet = false;
private boolean isName = false;
private boolean isFile = false;
// Parse the resId and initialize the parser.
public void initialize(Context context, int resId)
{
XmlResourceParser parser = null;
try
{
parser = context.getResources().getXml(resId);
mFonts = new ArrayList<Font>();
String tag;
int eventType = parser.getEventType();
Font font = null;
do
{
tag = parser.getName();
switch ( eventType )
{
case XmlPullParser.START_TAG:
if ( tag.equals(TAG_FAMILY) )
{
// one of the font-families.
font = new Font();
}
else if ( tag.equals(TAG_NAMESET) )
{
// a list of font-family names supported.
font.families = new ArrayList<String>();
}
else if ( tag.equals(TAG_NAME) )
{
isName = true;
}
else if ( tag.equals(TAG_FILESET) )
{
// a list of files specifying the different styles.
font.styles = new ArrayList<FontStyle>();
}
else if ( tag.equals(TAG_FILE) )
{
isFile = true;
}
break;
case XmlPullParser.END_TAG:
if ( tag.equals(TAG_FAMILY) )
{
// add it to the list.
if ( font != null )
{
mFonts.add(font);
font = null;
}
}
else if ( tag.equals(TAG_NAME) )
{
isName = false;
}
else if ( tag.equals(TAG_FILE) )
{
isFile = false;
}
break;
case XmlPullParser.TEXT:
String text = parser.getText();
if ( isName )
{
// value is a name, add it to list of family-names.
if ( font.families != null )
font.families.add(text);
}
else if ( isFile )
{
// value is a file, add it to the proper kind.
FontStyle fontStyle = new FontStyle();
fontStyle.font = Typeface.createFromAsset(context.getAssets(), text);
if ( text.endsWith(STYLE_BOLD) )
fontStyle.style = Typeface.BOLD;
else if ( text.endsWith(STYLE_ITALIC) )
fontStyle.style = Typeface.ITALIC;
else if ( text.endsWith(STYLE_BOLDITALIC) )
fontStyle.style = Typeface.BOLD_ITALIC;
else
fontStyle.style = Typeface.NORMAL;
font.styles.add(fontStyle);
}
}
eventType = parser.next();
}
while ( eventType != XmlPullParser.END_DOCUMENT );
}
catch ( XmlPullParserException e )
{
throw new InflateException("Error inflating font XML", e);
}
catch ( IOException e )
{
throw new InflateException("Error inflating font XML", e);
}
finally
{
if ( parser != null )
parser.close();
}
}
public Typeface get(String family, int style)
{
for ( Font font : mFonts )
{
for ( String familyName : font.families )
{
if ( familyName.equals(family) )
{
// if no style in specified, return normal style.
if ( style == -1 )
style = Typeface.NORMAL;
for ( FontStyle fontStyle : font.styles )
{
if ( fontStyle.style == style )
return fontStyle.font;
}
}
}
}
return null;
}
}
res/values/Fonts.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="Fonts">
<!-- using android's -->
<attr name="android:textStyle" />
<!-- our custom attribute -->
<attr name="font" format="string" />
</declare-styleable>
</resources>
res/xml/fonts.xml
<?xml version="1.0" encoding="utf-8"?>
<familyset>
<!-- Arial -->
<family>
<nameset>
<name>Arial</name>
</nameset>
<fileset>
<file>fonts/Arial-Regular.ttf</file>
<file>fonts/Arial-Bold.ttf</file>
<file>fonts/Arial-Italic.ttf</file>
<file>fonts/Arial-BoldItalic.ttf</file>
</fileset>
</family>
</familyset>
Add the fonts inside assets/fonts
Arial-Bold.ttf
Arial-BoldItalic.ttf
Arial-Italic.ttf
Arial-Regular.ttf
Create your custom view Ex: FontableTextView.java
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.widget.TextView;
public class FontableTextView extends TextView
{
private static final String TAG = "FontableTextView";
public FontableTextView(Context context)
{
super(context);
}
public FontableTextView(Context context, AttributeSet attrs)
{
super(context, attrs);
setCustomFont(context, attrs);
}
public FontableTextView(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
setCustomFont(context, attrs);
}
/*
* #see android.widget.CompoundButton#onDraw(android.graphics.Canvas)
*/
#Override
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
}
private void setCustomFont(Context ctx, AttributeSet attrs)
{
// Fonts work as a combination of particular family and the style.
TypedArray a = ctx.obtainStyledAttributes(attrs, R.styleable.Fonts);
String family = a.getString(R.styleable.Fonts_font);
int style = a.getInt(R.styleable.Fonts_android_textStyle, -1);
a.recycle();
// Set the typeface based on the family and the style combination.
if ( family != null )
{
setTypeface(FontManager.getInstance().get(family, style));
}
}
}
Initiate FontManager to set custom fonts at the start of the MainActivity
FontManager.getInstance().initialize(getApplicationContext(), R.xml.fonts);
Use the custom view in Layout XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.package.FontableTextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dip"
android:text="FontableTextView"
android:textStyle="bold"
custom:font="Arial" />
</RelativeLayout>
for actionbar:
use actionbar sherlock (http://actionbarsherlock.com/) and customize its code:
in com.actionbarsherlock.app.Actionbar add these two abstract methods:
public abstract void setTitleTypeface(Typeface TF);
public abstract void setSubtitleTypeface(Typeface TF);
and override these methods in com.actionbarsherlock.internal.app.ActionBarImpl
#Override
public void setTitleTypeface(Typeface TF) {
mActionView.setTitleTypeface(TF); }
#Override
public void setSubtitleTypeface(Typeface TF) {
mActionView.setSubtitleTypeface(TF); }
and also in com.actionbarsherlock.internal.app.ActionBarWrapper like this
#Override
public void setTitleTypeface(Typeface TF) {}
#Override
public void setSubtitleTypeface(Typeface TF) {}
and finally in com.actionbarsherlock.internal.widget.ActionBarView add these methods:
public void setTitleTypeface(Typeface TF){
mTitleView.setTypeface(TF);
}
public void setSubtitleTypeface(Typeface TF){
mSubtitleView.setTypeface(TF);
}
and now use it in your SherlockActivity like this:
Typeface TF = Typeface.createFromAsset(getApplication().getAssets(),
"Arial.ttf");
getSupportActionBar().setTitleTypeface(TF);
Make Sure there is no better way!!!
2. for dialogs, system settings,... u should change code like this!

Ellipsize only a section in a TextView

I was wondering if it is possible to abbreviate only a portion of a string in a TextView. What I would like to do is something like this:
Element with short title (X)
Element with a very lo...(X)
The title should be ellipsized, but the X must be always visible. In my case, is not possible to use more than one TextView. Do you think there is a simple way of doing this?
Thanks!
I really needed a clean solution for a project so after searching around and not finding any solutions I felt I liked, I took some time to write this up.
Here is an implementation of a TextView with enhanced ellipsis control. The way it works is by using Android's Spanned interface. It defines an enum you can use to tag the specific section of text you'd like to be ellipsized if needed.
Limitations:
Does not support ellipsis at MIDDLE. This should be easy to add if it's really needed (I didn't).
This class will always render the text onto one line, as it only supports a single line of text. Others are welcome to extend it if that's needed (but it's a far harder problem).
Here's a sample of the usage:
FooActivity.java
class FooActivity extends Activity {
/**
* You can do this however you'd like, this example uses this simple
* helper function to create a text span tagged for ellipsizing
*/
CharSequence ellipsizeText(String text) {
SpannableString s = new SpannableString(text);
s.setSpan(TrimmedTextView.EllipsizeRange.ELLIPSIS_AT_END, 0, s.length(),
Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
return s;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.foo_layout);
TextView textView = (TextView) findViewById(R.id.textView4);
SpannableStringBuilder text = new SpannableStringBuilder();
text.append(ellipsizeText("This is a long string of text which has important information "));
text.append("AT THE END");
textView.setText(text);
}
}
res/layouts/foo_layout.xml
<com.example.text.TrimmedTextView
android:id="#+id/textView4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"/>
That's it
Here's an example of the result:
The Implementation
package com.example.text;
import android.content.Context;
import android.text.Editable;
import android.text.Layout;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.TextUtils.TruncateAt;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.TextView;
public class TrimmedTextView extends TextView {
public static enum EllipsizeRange {
ELLIPSIS_AT_START, ELLIPSIS_AT_END;
}
private CharSequence originalText;
private SpannableStringBuilder builder = new SpannableStringBuilder();
/**
* This allows the cached value of the original unmodified text to be
* invalidated whenever set externally.
*/
private final TextWatcher textCacheInvalidator = new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
originalText = null;
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void afterTextChanged(Editable s) {
}
};
public TrimmedTextView(Context context) {
this(context, null, 0);
}
public TrimmedTextView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public TrimmedTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
addTextChangedListener(textCacheInvalidator);
Log.v("TEXT", "Set!");
}
/**
* Make sure we return the original unmodified text value if it's been
* custom-ellipsized by us.
*/
public CharSequence getText() {
if (originalText == null) {
return super.getText();
}
return originalText;
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
Layout layout = getLayout();
CharSequence text = layout.getText();
if (text instanceof Spanned) {
Spanned spanned = (Spanned) text;
int ellipsisStart;
int ellipsisEnd;
TruncateAt where = null;
ellipsisStart = spanned.getSpanStart(EllipsizeRange.ELLIPSIS_AT_START);
if (ellipsisStart >= 0) {
where = TruncateAt.START;
ellipsisEnd = spanned.getSpanEnd(EllipsizeRange.ELLIPSIS_AT_START);
} else {
ellipsisStart = spanned.getSpanStart(EllipsizeRange.ELLIPSIS_AT_END);
if (ellipsisStart >= 0) {
where = TruncateAt.END;
ellipsisEnd = spanned.getSpanEnd(EllipsizeRange.ELLIPSIS_AT_END);
} else {
// No EllipsisRange spans in this text
return;
}
}
Log.v("TEXT", "ellipsisStart: " + ellipsisStart);
Log.v("TEXT", "ellipsisEnd: " + ellipsisEnd);
Log.v("TEXT", "where: " + where);
builder.clear();
builder.append(text, 0, ellipsisStart).append(text, ellipsisEnd, text.length());
float consumed = Layout.getDesiredWidth(builder, layout.getPaint());
CharSequence ellipsisText = text.subSequence(ellipsisStart, ellipsisEnd);
CharSequence ellipsizedText = TextUtils.ellipsize(ellipsisText, layout.getPaint(),
layout.getWidth() - consumed, where);
if (ellipsizedText.length() < ellipsisText.length()) {
builder.clear();
builder.append(text, 0, ellipsisStart).append(ellipsizedText)
.append(text, ellipsisEnd, text.length());
setText(builder);
originalText = text;
requestLayout();
invalidate();
}
}
}
}
You can try using something like this:
myTextView.setEllipsize(TextUtils.TruncateAt.MIDDLE);
It might not give you exactly what you want though, it may do something like this:
Element wi...title (X)
Reference Info
TruncateAt
setEllipsize

Categories

Resources