Android, change fonts on a final text view - android

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

Related

how to set scroll view in android

RelativeLayout blur = (RelativeLayout) findViewById(R.id.relative5);
TextView name_tv = new TextView (this);
name_tv.setText("\n\n First Name");
blur.addView(name_tv);
Typeface font=Typeface.SERIF;
name_tv.setTypeface(font);
final EditText nameedit = new EditText(this);
nameedit.setHint("First name");
blur.addView(nameedit);
Typeface font33=Typeface.SERIF;
nameedit.setTypeface(font33);
nameedit.setMaxLines(1);
nameedit.setImeOptions(EditorInfo.IME_ACTION_NEXT);
nameedit.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
Similarly i add few text view and Edit text dynamically in this class.The problem is when i add scrollview in xml it show me error.....

How to write algebra formulas in android xml

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>"));

Multiple fonts for multiple languages

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.

Custom Font in View Pager in android

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;
}

How to apply Font type for a whole application in Android?

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)

Categories

Resources