Showing posts with label TextView. Show all posts
Showing posts with label TextView. Show all posts

Thursday, June 27, 2013

How to use custom fonts in Android XML file?


Add the .ttf file in your assets/fonts folder. To use it in TextView the best way is to extend the TextView and have to set the font.  Here is the code of the extended TextView,

package naveendroid.fontstest;

import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.TextView;

public class RobotTextView extends TextView {
    public RobotTextView(Context context, AttributeSet attrs, int defStyle) {        super(context, attrs, defStyle);
        init();
    }

    public RobotTextView(Context context, AttributeSet attrs) {        super(context, attrs);
        init();
    }

    public RobotTextView(Context context) {        super(context);
        init();
    }

    private void init() {
        Typeface tf = Typeface.createFromAsset(getContext().getAssets(),
                                               "Roboto-Regular.ttf");
        setTypeface(tf);
    }

}

Then use this in your .xml file as shown below,

<naveendroid.fontstest.MyTextView
    android:id="@+id/sometext"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:layout_weight="1"
    android:text="TextView with the font that you had set in RobotTextView class "
    android:textSize="20dip"
    android:textColor="#000"
   >



Tuesday, December 4, 2012

Clickable links for textview inside Listview - Android

How to make a URL/link clickable in a textview which is inside a Listview?


Let us assume that you are binding your Listview to a custom adapter. In that case, the following piece of code goes into your getView method call:
Code:
textView.setText(Html.fromHtml(item.gettext()));
textView.setAutoLinkMask(Linkify.WEB_URLS);
Took me sometime to find this out :)


Tuesday, November 20, 2012

TextView - Bold or Italic - Android

Below is the code that makes a Textview bold or italic


strings.xml
<resources>
    <string name="username"><u><b><i>Peter</i></b></u></string>
</resources>


To set this String to your TextView, do this in your main.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/textview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="@string/username"
/>



or In your JAVA code,
   TextView textView=new TextView(this);
    textView.setText(R.string.username);


If you're making use of Dynamic Text, the below code would help
  String tempString="SomeString";
  TextView text=(TextView)findViewById(R.id.text);
  SpannableString spanString = new SpannableString(tempString);
  spanString.setSpan(new UnderlineSpan(), 0, spanString.length(), 0);
  spanString.setSpan(new StyleSpan(Typeface.BOLD), 0, spanString.length(), 0);
  spanString.setSpan(new StyleSpan(Typeface.ITALIC), 0, spanString.length(), 0);
  text.setText(spanString);