I have recently come across a situation while developing an app where i have to display different languages in text view . currently I am displaying few using fonts/typeface like this :
Typeface tf = Typeface.createFromAsset(this.getAssets(),
"DroidHindi.ttf");
TextView textView1 = (TextView) findViewById(R.id.textView2);
textView1.setTypeface(tf);
textView1.setText("कचजड, कचजड");
Typeface tf1 = Typeface.createFromAsset(this.getAssets(),
"asunaskh.ttf");
TextView textView = (TextView) findViewById(R.id.textView1);
textView.setTypeface(tf1);
textView.setText("یہ انگریزی نہیں");
Typeface tf2 = Typeface.createFromAsset(this.getAssets(),
"Banglafont.ttf");
TextView textView2 = (TextView) findViewById(R.id.textView3);
textView2.setTypeface(tf2);// এই ইংরেজি নয়
textView2.setText("এই ইংরেজি নয়");
its fine my question is that i have to support for some 20 different languages then things will become very tedious when i apply this in different activities . Any alternative way to achieve.
You do is to create a class and a function with access public that will return you TextView Object with the Font that you want to suppose.
TextView public SetLanguage(TextView tv,string type)
{
TextView newtv = tv;
Typeface tf;
switch(type)
{
case "urdu":
tf = Typeface.createFromAsset(this.getAssets(),"urdu.ttf");
break;
case "hindi":
tf = Typeface.createFromAsset(this.getAssets(),"hindi.ttf");
break;
// up so on
}
newtv.setTypeface(tf);
return newtv;
}
// and call it any where..
TextView textView1 = (TextView) findViewById(R.id.textView2);
textView1 = classobj.SetLanguage(textView1,"urdu");
//assign string of text to it
Initialize your typefaces when the app starts up, and make a method which takes any view and sets the typeface based on the language.
Just like we do with other resource files, we can also define a font directory for any locale or country code we want to. The downside to this approach is that both the fonts have to be named the same although they are different but otherwise a clean solution.
Related
Here is an example
https://play.google.com/store/apps/details?id=com.entertainment.algebra_calculator_1
I just wanted to know how the formulas are written in xml like 1/x, superscripts, subscripts.
This should work for super script
TextView mTextView = (TextView) findViewById(R.id.textView);
mTextView.setText(Html.fromHtml("10<sup>3</sup>"));
For sub script
TextView mTextView = (TextView) findViewById(R.id.textView);
mTextView.setText(Html.fromHtml("A<sub>2</sub>"));
my problem is something I've never seen before...
I'm trying to make an android app, and I've stuck at one small bug, and this one is very tricky.
I have 20 TextViews, 10 medium size and 10 regular size, the little ones.
And somehow two of these textviews, one medium and the other is a little one, change it's text one for another. The first should show a text and the second a number. Somehow they are inverse, and I keep looking at the code, and I can't figure out why.
Some of you are thinking that is a simple thing to solve, that I've just misplace the codes, but I'll tell, I've checked over and over again, and it seens right.
public void showWritings(){
TextView tv1stSpentReportM = (TextView) findViewById(R.id.tv1stSpentReportM);
tv1stSpentReportM.setText(vetorGastos[9].getTipoGasto());
TextView tv2stSpentReportM = (TextView) findViewById(R.id.tv2stSpentReportM);
tv2stSpentReportM.setText(vetorGastos[8].getTipoGasto());
TextView tv3stSpentReportM = (TextView) findViewById(R.id.tv3stSpentReportM);
tv3stSpentReportM.setText(vetorGastos[7].getTipoGasto());
TextView tv4stSpentReportM = (TextView) findViewById(R.id.tv4stSpentReportM);
tv4stSpentReportM.setText(vetorGastos[6].getTipoGasto());
TextView tv5stSpentReportM = (TextView) findViewById(R.id.tv5stSpentReportM);
tv5stSpentReportM.setText(vetorGastos[5].getTipoGasto());
TextView tv6stSpentReportM = (TextView) findViewById(R.id.tv6stSpentReportM);
tv6stSpentReportM.setText(vetorGastos[4].getTipoGasto());
TextView tv7stSpentReportM = (TextView) findViewById(R.id.tv7stSpentReportM);
tv7stSpentReportM.setText(vetorGastos[3].getTipoGasto());
TextView tv8stSpentReportM = (TextView) findViewById(R.id.tv8stSpentReportM);
tv8stSpentReportM.setText(vetorGastos[2].getTipoGasto());
TextView tv9stSpentReportM = (TextView) findViewById(R.id.tv9stSpentReportM);
tv9stSpentReportM.setText(vetorGastos[1].getTipoGasto());
TextView tv10stSpentReportM = (TextView) findViewById(R.id.tv10stSpentReportM);
tv10stSpentReportM.setText(vetorGastos[0].getTipoGasto());
TextView tv1stSpentReportV = (TextView) findViewById(R.id.tv1stSpentReportV);
tv1stSpentReportV.setText(String.valueOf(vetorGastos[9].getValorGasto()));
TextView tv2stSpentReportV = (TextView) findViewById(R.id.tv2stSpentReportV);
tv2stSpentReportV.setText(String.valueOf(vetorGastos[8].getValorGasto()));
TextView tv3stSpentReportV = (TextView) findViewById(R.id.tv3stSpentReportV);
tv3stSpentReportV.setText(String.valueOf(vetorGastos[7].getValorGasto()));
TextView tv4stSpentReportV = (TextView) findViewById(R.id.tv4stSpentReportV);
tv4stSpentReportV.setText(String.valueOf(vetorGastos[6].getValorGasto()));
TextView tv5stSpentReportV = (TextView) findViewById(R.id.tv5stSpentReportV);
tv5stSpentReportV.setText(String.valueOf(vetorGastos[5].getValorGasto()));
TextView tv6stSpentReportV = (TextView) findViewById(R.id.tv6stSpentReportV);
tv6stSpentReportV.setText(String.valueOf(vetorGastos[4].getValorGasto()));
TextView tv7stSpentReportV = (TextView) findViewById(R.id.tv7stSpentReportV);
tv7stSpentReportV.setText(String.valueOf(vetorGastos[3].getValorGasto()));
TextView tv8stSpentReportV = (TextView) findViewById(R.id.tv8stSpentReportV);
tv8stSpentReportV.setText(String.valueOf(vetorGastos[2].getValorGasto()));
TextView tv9stSpentReportV = (TextView) findViewById(R.id.tv9stSpentReportV);
tv9stSpentReportV.setText(String.valueOf(vetorGastos[1].getValorGasto()));
TextView tv10stSpentReportV = (TextView) findViewById(R.id.tv10stSpentReportV);
tv10stSpentReportV.setText(String.valueOf(vetorGastos[0].getValorGasto()));
}
}
the Medium TextViews has a M in their names, and the little ones has a V.
In the variable and in the id.
The tv2stSpentReportM is showing the text of tv2stSpentReportV and tv2stSpentReportV the text of tv2stSpentReportV.
To make things more clear, here is the code of my "Gastos.class", This one is what I have in the array vetorGastos[].
public class Gastos {
private String tipoGasto;
private double valorGasto;
public String getTipoGasto() {
return tipoGasto;
}
public void setTipoGasto(String tipoGasto) {
this.tipoGasto = tipoGasto;
}
public double getValorGasto() {
return valorGasto;
}
public void setValorGasto(double valorGasto) {
this.valorGasto = valorGasto;
}
}
As I've told you before, and no one believe me, here as printscreen showing you exacly what I've told. The id's are correct. So what now? And disbelievers, thank you for removing reputation points just cause you don't believe. Expand the Image so you could see that the id's are correct.
I already solve this one now, just rewrite ONE of the id's, and it get right. So if it was just a misplaced id, i should rewrite two id's. I don't know why. but it solve the problem.
I've already solve this one now.
I don't know why, but just rewrite ONE of the id's solved the problem.
If it were a misplaced id, I should change two ID's not rewrite one, the same writing.
I want to change the font on a text view
But the tv is modified at the pression of a button, and i define "final" the tv, and the app crashed...
what i can do it?
Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/HeroQuest.ttf");
final TextView tv = (TextView) findViewById(R.id.textView1);
tv.setTypeface(tf);
This code work if tv is not final
try
Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/HeroQuest.ttf");
TextView tv1 = (TextView) findViewById(R.id.textView1);
tv1.setTypeface(tf);
final TextView tv = tv1;
....
Define your TextView as field
i.e
private TextView tv;
if u need more clarification on crash.paste the code in your OnClicklistener
I am developing demo application in which I am using view pager. Now I want to know that can we change the text style of Title displayed in view pager.
Please give your suggestions on it.
Thanking you in advance.
Working demo
TextView txt = (TextView) v.findViewById(R.id.custom_font);
and then change your font
Something like this:
switch (position) {
case 0:
v = (LinearLayout) LayoutInflater.from(cxt).inflate(R.layout.lcmeter, null);
TextView txt = (TextView) v.findViewById(R.id.custom_font);
Typeface font = Typeface.createFromAsset(getAssets(), "Chantelli_Antiqua.ttf");
txt.setTypeface(font);
break;
}
I want to apply Font type without using XML. By code, I want to change Font type and it should be able to apply for a whole application, when I click Arial else Times New Roman etc.
try this code to change the font of app
TypeFace typeFace = Typeface.createFromAsset(getAssets(), "font.ttf");
TextView view= (TextView) findViewById(R.id.view);
view.setTypeface(typeFace)
I would reccomend having a list preference, in a preference screen. You can then set the fonts from that. i.e:
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
String font = sp.getString("font","-1")
if(font.equals("timesroman")) {
TypeFace typeFace = Typeface.createFromAsset(getAssets(), "timesnewroman.ttf");
}
else if(font.equals("arial")) {
TypeFace typeFace = Typeface.createFromAsset(getAssets(), "arial.ttf");
}
TextView view= (TextView) findViewById(R.id.view);
view.setTypeface(typeFace)