Jayesh
Jayesh

Reputation: 3881

How to set gujarati for textview in the android application?

I want to make a application in which i needs to show the textview in gujarati language.

I tried with

Typeface tf = Typeface.createFromAsset(getAssets(), "G-SARAL1.TTF");
text.setTypeFace(tf);

but this is not working, I set the gujarati text in string.xml file and then i set that string in the textview, but when i run the application, it display square boxs instead of gujarati character,

can anyone help me , how can i implement this task.

Upvotes: 3

Views: 6213

Answers (4)

asif juneja
asif juneja

Reputation: 61

I checked out all the given solutions but no one solved my prob so I did some more and more google and found out that, you need to create assets folder first and then paste all the fonts file which you required in your application here is a little guide for how we can add assets folder in our project

Step 1 : Navigate to the top left side of an android studio where you got menu like this and just change the option from the dropdown to Packages

Step 2 : now you will see parent as app and now just right click on the app and click new and then Folder and then Assets Folder

this is how you can add Assets Folder.

once you add Assets Folder then simply paste your ttf files into that Assets folder. and follow my code for reference

    TextView text_view = (TextView) findViewById(R.id.txtMsg);

    //TextView text_view = new TextView(this);

    Typeface faceShruti = Typeface.createFromAsset(getAssets(),
            "shruti.ttf");

    text_view.setTypeface(faceShruti);

    text_view.setText("In Gujarati\n" + "સ્વાગત");  

Upvotes: 0

Krunal Kapadiya
Krunal Kapadiya

Reputation: 3073

You can download the Montserrat fonts, this font is use for both language English and Gujarati. If you do not want to download the saperate fonts than can use this. You can make base text and can create method like below and use the fonts. One more thing you have to pass the

    fun setFonts(context: Context, fontName: String) {
        val typeface = Typeface.createFromAsset(context.assets, fontName)
        setTypeface(typeface)
    }

To create localized string file you can also use Translations Editor.

Upvotes: 0

Muhammad Usman Ghani
Muhammad Usman Ghani

Reputation: 1279

Try this code this will work

TextView mtxt = (TextView) findViewById(R.id.action_settings); 

           Typeface face1 = Typeface.createFromAsset(getAssets(), 

                     "Lohit-Gujarati.ttf"); 

           mtxt.setTypeface(face1); 

           mtxt.setText("પૂર્વ વડાપ્રધાન રાજીવ ગાંધીની હત્યા કરનારા 3 આરોપીઓની ફાંસીની સજા સુપ્રિમ કોર્ટે ઉમ્રકેદમાં ફેરવી નાંખી છે.આમ રાજીવગાંધીના" + 

                     "3 હત્યારાઓને હવે ફાંસીની સજા નહી થાય.સુપ્રીમ કોર્ટમાં 3 જજોની બેન્ચે આજે આ ચુકાદો આ"); 

Upvotes: 0

user1310156
user1310156

Reputation: 85

As gujarati and hindi languages are not supported by Android, you can still give that support to your Application.

For Gujarati copy the C:\WINDOWS\Fonts\Shruti.TTF file to your asset folder. Then use the following code.

    TextView text_view = new TextView(this);
    Typeface font = Typeface.createFromAsset(getAssets(), "Shruti.TTF");
    text_view.setTypeface(font);
    text_view.setText("ગુજરાતી");

Shruti.TTF file is for Gujarati font. Similarly you can add support for hindi file.

Upvotes: 7

Related Questions