Sharing Knowledge

« Android Notification | Main | Displaying dialogs... »
Saturday Jun 26, 2010

Android Font

Kadangkala kita membutuhkan font atau jenis tulisan untuk aplikasi yang kita buat. Anda bisa mengambil dari font yang disediakan oleh Android (default font) atau menggunakan font external yang kemudian dimasukkan ke dalam Android.


1. Default Font
Android hanya menyediakan 3 default font, yaitu Monoscape, Sans, dan Serif.

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">

  <TextView android:layout_height="wrap_content"
    android:typeface="monospace"
    android:id="@+id/monospaceTxt"
    android:layout_width="fill_parent"
    android:text="Monospace : Android Programming"
    android:background="#424242">
  </TextView>

  <TextView android:layout_height="wrap_content"
    android:typeface="sans"
    android:id="@+id/sansTxt"
    android:layout_width="fill_parent"
    android:text="sans : Android Programming">
  </TextView>

  <TextView android:layout_height="wrap_content"
    android:typeface="serif"
    android:layout_width="fill_parent"
    android:text="serif : Android Programming"
    android:id="@+id/serifTxt"
    android:background="#424242">
  </TextView>
</LinearLayout>





2. External Font
Selain default font, anda dapat menggunakan external font dalam aplikasi Android. Langkah pertama, anda perlu download font (file .ttf) kemudian buat folder baru di dalam package assets dan copy kan font tadi kedalamnya.
Setelah itu beri id untuk masing - masing font dengan nama text1 dan text2 dalam sebuah TextView di file xml (main.xml) :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView
    android:id="@+id/text1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"

    />
<TextView
    android:id="@+id/text2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"

    />

</LinearLayout>



Import android graphics Typeface kedalam class Main. Typeface digunakan untuk menentukan jenis huruf dan style intrinsik dari sebuah font. Kemudian buat object font1 dan font2 untuk mengambil font yang kita simpan dalam folder assets tadi dengan menggunakan method createFromAssets()

 package android.externalFont;

import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.widget.TextView;

public class Main extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Typeface font2 = Typeface.createFromAsset(getAssets(), "font/font2.ttf");
        Typeface font3 = Typeface.createFromAsset(getAssets(), "font/font3.ttf");

Buat object customText dalam TextView untuk mengambil id font yang sudah kita deklarasikan dalam main.xml

 TextView customText2 = (TextView)findViewById(R.id.text2);
 TextView customText3 = (TextView)findViewById(R.id.text3);

Anda dapat menentukan jenis font yang akan digunakan dengan fungsi setTypeface(), mengubah ukuran text default dengan method setTextSize(), dan mendefinisikan text yang akan ditampilkan melalui method setText()

        customText2.setTypeface(font2);
        customText2.setTextSize(30.f);
        customText2.setText("Android Programming");

        customText3.setTypeface(font3);
        customText3.setTextSize(55.f);
        customText3.setText("Android Programming");






netoya_meruvian

Comments:

Post a Comment:
  • HTML Syntax: Allowed