Showing posts with label Eclipse. Show all posts
Showing posts with label Eclipse. Show all posts

Saturday, April 9, 2011

Using Custom fonts on Android

I had to do this for a project that I am working on, and I felt that it wasn't possible to achieve this. Well, there are several examples on the internet regarding this and nothing seemed to be working for me. So, in this post, we will see how to use custom fonts for your application on Android.

Few things before we start off:
  • Not all fonts are compatible with Android
  • You need to package the ttf files with your apk
  • It's obviously a little bit of extra work
So for this example, we have a TextView and a Button with different fonts. To be able to use your custom font everywhere, ie, on all the TextView and Button widgets in your app, you will have to extend these classes to create your own TextView and Button classes. I have named them as MyTextView and MyButton. And then, I can use these buttons in my layout xml files, with the fully-qualified name of my custom classes.

<?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">
    <com.beanie.samples.customfont.MyTextView
        android:layout_marginTop="10dip" android:id="@+id/textView"
        android:layout_width="fill_parent" android:layout_height="wrap_content"
        android:text="@string/hello" android:textSize="20sp"></com.beanie.samples.customfont.MyTextView>

    <com.beanie.samples.customfont.MyButton
        android:layout_marginTop="10dip" android:id="@+id/textView"
        android:layout_width="fill_parent" android:layout_height="wrap_content"
        android:text="@string/hello" android:textSize="20sp"></com.beanie.samples.customfont.MyButton>
</LinearLayout>
In both these classes, we have a method called init() which is called from all the constructors. The method is just 3 line long.
        if (!isInEditMode()) {
                  Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/ds_digib.ttf");
                  setTypeface(tf);
        }
Simple!!! You might notice that there is an if condition. This is not required, but it does help when you are preparing your layouts on eclipse, and you need to keep checking if everything's fine. This method, isInEditMode() returns true while eclipses tries to render the view from the XML.

This is what the documentation says:
Indicates whether this View is currently in edit mode. A View is usually in edit mode when displayed within a developer tool. For instance, if this View is being drawn by a visual user interface builder, this method should return true. Subclasses should check the return value of this method to provide different behaviors if their normal behavior might interfere with the host environment. For instance: the class spawns a thread in its constructor, the drawing code relies on device-specific features, etc. This method is usually checked in the drawing code of custom widgets.
If you don't put this condition, the layout editor will complain about not being able to set the TypeFace. So, in the layout editor, you will see your TextView and Button widgets with the default font. But, when you run your app on an emulator or a device, you can see the goodness of your custom fonts.

The source code for the sample project can be found here. You can find 3 different fonts (ttf file) in the assets folder to play with.

Friday, February 26, 2010

Sign your Android applications for release

The process of application signing can be very confusing for new developers. But, once you have done it 2-3 times, it will be more like a chore for you after that. It's not very complicated at all. So, here are the steps that you need to follow.

Note: If you already have a self-signed certificate/keystore, skip to step 2(b):

Step 1: Right click on the Android project. From the context menu, select, Android Tools -> Export Signed application package. In the dialog that will open, you will see the name of your project. If you have selected the wrong project, here is a chance to correct yourself. Click on "Next".

 

Step 2:  The next screen is the most complicated screen that you will get in this process. Here, you can have two situations.

a. If you don't have a keystore already, you will need to create one first. Once created, you should preserve this keystore somewhere, safely, so that you don't accidentally delete it. You will need this keystore everytime you want to sign and update your application to the Android Market. Please, please, please, DO NOT LOSE THIS KEYSTORE.

                                       

Since, we are creating a new keystore this time, select the second radio, and browse to a location where you want you new keystore to be saved. Enter and confirm a password. Remember this password. Click on "Next".



Enter all the fields in this screen and press enter. Now your keystore will be created. Please rememeber all your passwords. Else, this keystore will be useless for you later.For simplicity, keep both the passwords same.



On this screen, specify a name and location of the apk and click on "Finish". Your signed application package will be created for you in the location you have mentioned here. You can now take this apk and directly upload it to the Android market, or put it on your phone.
b. When you already have a kestore, it's more easier to sign your application. On the second screen, select, "Use existing keystore". Enter the location of the keystore and password. Click on "Next". On the next screen, select you alias from the drop down, enter the alias password, click on "Next". On the next screen, specify a name and location where you want the apk to be created and click "Finish". You are done now.



Isn't it simple? Just one point to note: If you are using an old ADT version, this option of signing your apps from eclipse might now be there. In such a case, you will have to do it through command line. You can find many tutorials on how to achieve that.


Thanks,
Kumar