Monday 28 January 2013

External font Across the Android Application

Sometimes in Android app we may need to maintain same font across application. At point of time previews post is not sufficient.

Here below Example shows how to use External font across the application.

Here First I have downloaded ".ttf” file from google. Created a new folder “font” in assets folder and paste your recently downloaded font file.




















we can do that by just extending your class from android TextView.

Below code is to my customized TextView class(OwnFontTextView.java)

package com.androidsurya.androidexternalfontexample;

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

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

    public OwnFontTextView(Context context, AttributeSet attrs) {
        // call the constructor which has the complete definition
        this(context, attrs, 0);
    }

    public OwnFontTextView(Context context) {
        super(context);
    }

    public void setFont() {
        String ttf_fontPath = "fonts/TIMES_SQ.TTF";
        Typeface font = Typeface.createFromAsset(getContext().getAssets(),
                ttf_fontPath);
        setTypeface(font);
    }
}

In our Layout insted of declare <TextView/> tag we just specify <com.androidsurya.androidexternalfontexample.OwnFontTextView/>
in any were in our application.


 <com.androidsurya.androidexternalfontexample.OwnFontTextView
        android:id="@+id/textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="Hello Android"
        android:textColor="#65E561"
        android:textSize="45dp" />

Then external font will be applied to Text.

Screenshot























Tuesday 22 January 2013

Android.view.windowmanager$badtokenexception unable to add window token null

Hi i am facing this Error when creating Dialog in Android

ERROR IS - android.view.windowmanager$badtokenexception unable to add window token null 

I resolved this issue using below one in my scenario.This may be help full for you

In my case i am trying to create my dialog like below code:

new Dialog(getApplicationContext());

This can be changed as

new Dialog(this);

Now Dialog will be appear without any Error.

If it is useful then please add your valuable comments Below.

Wednesday 16 January 2013

Diffrence between managedQuery() vs context.getContentResolver.query() in Android

In android managedQuery() will use ContentResolver's query(). The difference is
that with managedQuery() the activity will keep a reference to your
Cursor and close it whenever needed (in onDestroy() for instance.) If
you do query() yourself, you *will* have to manage the Cursor as a
sensitive resource. If you forget, for instance, to close() it in
onDestroy(), you will leak underlying resources (logcat will warn you
about it.)

Wednesday 9 January 2013

External Fonts in Android

Here i explains example for using external fonts in Android View.
Android applications using  external font files with .ttf extension we can change default font of Text.

Here I have downloaded ".ttf” file from google. Created a new folder “font” in assets folder and paste your recently downloaded font file.

Here we can change the font for a TextView simply using below code in your Activity.

String ttf_fontPath = "fonts/TIMES_SQ.TTF";
TextView textview = (TextView) findViewById(R.id.textview);
Typeface font = Typeface.createFromAsset(getApplicationContext().getAssets(), ttf_fontPath);
textview.setTypeface(font);

Screen Shots: 

Here we can see we are created font folder under assert folder of your project






















Final Output Screen Shot





Tuesday 8 January 2013

How to install .apk programmatically in android

  Here the blow code is used to call .apk file from the android programmatically.
  Status.apk is the .apk file name.you can use your .apk file name.
             
                Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(
Uri.fromFile(new File(Environment.getExternalStorageDirectory()
+ "/" + "Status.apk")),
"application/vnd.android.package-archive");
startActivity(intent);

Android SQLite Database Viewer or Debuging with Stetho

Every Android Developer uses SQLite Database to store data into the Android Application data. But to view the data in SQLite have a lot of...