Click here to Skip to main content
15,910,981 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
----------CustomTypefaceSpan.class-----------------
package com.example.notification;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.text.TextPaint;
import android.text.style.TypefaceSpan;

public class CustomTypefaceSpan extends TypefaceSpan {

private final Typeface newType;

public CustomTypefaceSpan(String family, Typeface type) {
super(family);
newType = type;
}

@Override
public void updateDrawState(TextPaint ds) {
applyCustomTypeFace(ds, newType);
}

@Override
public void updateMeasureState(TextPaint paint) {
applyCustomTypeFace(paint, newType);
}

private static void applyCustomTypeFace(Paint paint, Typeface tf) {
int oldStyle;
Typeface old = paint.getTypeface();
if (old == null) {
oldStyle = 0;
} else {
oldStyle = old.getStyle();
}

int fake = oldStyle & ~tf.getStyle();
if ((fake & Typeface.BOLD) != 0) {
paint.setFakeBoldText(true);
}

if ((fake & Typeface.ITALIC) != 0) {
paint.setTextSkewX(-0.25f);
}

paint.setTypeface(tf);
}
}
-------Notification class--------------
package com.example.notification;

import android.os.Bundle;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.graphics.Typeface;
import android.renderscript.Font;
import android.text.Html;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.view.Menu;
import android.widget.LinearLayout;
import android.widget.RemoteViews;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


Typeface font=Typeface.createFromAsset(getAssets(), "raavi.ttf");
SpannableStringBuilder sb=new SpannableStringBuilder("ਭਾਰਤ ਨਤੀਜੀਆਂ ਦੇ ਬਾਰੇ ਵਿੱਚ ਹਰੇਕ ਵਿਅਕਤੀ ਨੂੰ ਵਿਦਿਆ ਹਾਸਲ ਕਰਨ ਦਾ ਹੱਕ ਹੈ");

sb.setSpan (new CustomTypefaceSpan("", font), 0, sb.length()-1,Spanned.SPAN_EXCLUSIVE_INCLUSIVE);

showNotification(sb);


}

public void showNotification(SpannableStringBuilder sb) {
PendingIntent contentIntent;
// CharSequence text=" Satnam Shri vaheguru...You have a new sabad to read.";
CharSequence title="title";

NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

Notification notification = new Notification(R.drawable.ic_launcher, sb, System.currentTimeMillis());
notification.flags = Notification.FLAG_AUTO_CANCEL;

notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_LIGHTS;
notification.defaults |= Notification.DEFAULT_VIBRATE;


Intent showIntent =new Intent(this,MainActivity.class);
showIntent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
contentIntent = PendingIntent.getActivity(this, 0,
showIntent, 0);


notification.setLatestEventInfo(this,title, sb, contentIntent);
nm.notify(1, notification);





}






}
Posted
Comments
Sudhakar Shinde 25-Mar-13 6:28am    
This does not seem to be a question.Please format code using code tag so that it will be readable and also let us know in case you have any query.
Amrut Bidri 4-Dec-14 6:19am    
This font typeface is not working on Google Nexus, Moto G, and Android Wear Devices.
Can you help on this.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900