Thursday, July 14, 2011

Fonts Android / Custom Fonts

In android by default the following fonts are available.
DEFAULT
DEFAUT_BOLD
SERIF
SANS-SERIF
MONO SPACE

The Typeface class is used to specify the style of a font. In this example i am creating a textview and apply the fonts to that textview.

TextView tv = new TextView(this);

//For DEFAULT
tv.setTypeface(Typeface.DEFAULT);

//For DEFAUT_BOLD
tv.setTypeface(Typeface.DEFAULT_BOLD);

//For SERIF
tv.setTypeface(Typeface.SERIF);

//For SANS-SERIF
tv.setTypeface(Typeface.SANS_SERIF);  
//For MONO SPACE
tv.setTypeface(Typeface.MONOSPACE);

//Custom Fonts
To create custom font first we have to download the font file (.ttf). Copy that file to Assest folder of your project.

//Creation of custom font
Typeface tf = Typeface.createFromAsset(getAssets(), "BaroqueScript.ttf");
tv.setTypeface(tf);

Note:
For more info about Typeface visit
http://developer.android.com/reference/android/graphics/Typeface.html

Expandable list view move group icon indicator to right

The setIndicatorBounds(int left, int right) is used to set the indicator bounds for the group view of an expandable list view.

explvList.setIndicatorBounds(width-GetDipsFromPixel(35), width-GetDipsFromPixel(5));

Here width means device width.


//Convert pixel to dip
public int GetDipsFromPixel(float pixels)
{
        // Get the screen's density scale
        final float scale = getResources().getDisplayMetrics().density;
        // Convert the dps to pixels, based on density scale
        return (int) (pixels * scale + 0.5f);

}

Note:

The imp point is indicator icon design.

The icon should be like this height of the icon is equal to parent view height with transparent background. In that we put our required image in the center.

The width is equal to width specified in the setBounds method. Here in my snippet it is 35.

Other wise the icon is disturbed.

Output :