In this example we’ll show you how to support hindi, bangala, marathi fonts in Android application. Android phone doesn’t support Devanagari fonts. However if you want to use hindi, tamil, bangali or other Devanagari fonts in your Android Application you have an alternative of using external fonts. Follow below steps to achieve this
- Download an external Devanagari font that you like to use in your application. Here in this example, I have downloaded “Ananda-Lipi-Bold” font. You can also download from here
- Create a new folder “fonts” under assets and copy the downloaded .ttf font to your assets/font folder.
- Load .ttf to your TextView
Activity Layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#900001" android:padding="@dimen/activity_vertical_margin" > <TextView android:id="@+id/textView" android:gravity="center_horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="@string/hanuman_chalisa" android:textColor="#ecaa00" android:textSize="28sp" /> </RelativeLayout>
String.xml file
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">HindiFont</string> <string name="hanuman_chalisa"> श्रीगुरु चरण् सरोजरज, निजमनमुकुर सुधार ।\n बरणौ रघुबर बिमल यश, जो दायक फलचार ॥\n\n बुद्धिहीन तनु जानिके, सुमिरौं पवन कुमार ।\n बल बुद्धिविद्या देहु मोहिं, हरहु कलेश विकार ॥ </string> </resources>
Activity Java Code
import android.app.Activity; import android.graphics.Typeface; import android.os.Bundle; import android.widget.TextView; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView tv = (TextView) findViewById(R.id.textView); Typeface fontHindi = Typeface.createFromAsset(getAssets(), "fonts/Ananda Lipi Bold Cn Bt.ttf"); tv.setTypeface(fontHindi); } }
Never knew this, now i can Hindify my project.
getting error “r cannot be resolved to a variable”
Thanks 🙂
Thanks Nilanchala it is very useful for me i use it and complete our app requirement. I am also android developer and regular android blogger.
http://goo.gl/0m6Cv7
Did you check your ttf is fine ? From my experience, I have noticed devanagari font doesn’t load properly in some devices.
Looks like some code with semicolon. Can you paste the entire code?
I copy pasted exactly the same code you’ve presented.The error I am talking about is for the mentioned code.thanks
I’m pasting the entire code from the beginning:package com.example.spk.muni_darshan;
import android.app.Activity;
import android.graphics.Typeface;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.widget.TextView;
class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tv = (TextView) findViewById(R.id.textView);
Typeface fontHindi = Typeface.createFromAsset(getAssets(),
“fonts/DevLys 180.ttf”);
tv.setTypeface(fontHindi);
}
}
thanks
You are wrongly adding import statement again. I have refactored your code and it compiles fine. Take the code below
com.example.spk.muni_darshan;
import android.app.Activity;
import android.graphics.Typeface;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tv = (TextView) findViewById(R.id.textView);
Typeface fontHindi = Typeface.createFromAsset(getAssets(), “fonts/DevLys 180.ttf”);
tv.setTypeface(fontHindi);
}
@Override
public boolean o nCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
hey i want to make simple android calculator which support devanagri number instead english(1,2,3,4,5,6).. i am making the layout but how to achive in java code of arthematic operation?
hi, Nilanchala,
i need to change hindi font at the time of sending to server. when i m send through my application text is appear like ???????? ?? ???.. i am sending through edit text so please help me out how can i change font that will support to database.