0

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

Lele
  • 703
  • 3
  • 15
  • 34
  • Could you provide a stack trace ? – stefan.nsk Mar 22 '13 at 13:35
  • Attach please a stacktrace, but I'm pretty sure that "final" is not a cause of your crash. It's really doesn't change anything. – tundundun Mar 22 '13 at 13:37
  • i found a solution public class Personaggi extends Activity { TextView tv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //change font Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/HeroQuest.ttf"); tv = (TextView) findViewById(R.id.corpo); tv.setTypeface(tf); View.OnClickListener gestore = new View.OnClickListener() { public void onClick(View view) { //here put thw switch for the button, and i can use the tv without declare it final – Lele Mar 22 '13 at 13:39
  • i' posting the solution... i declared the tv as global – Lele Mar 22 '13 at 13:40

2 Answers2

0

try

Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/HeroQuest.ttf");
TextView tv1 = (TextView) findViewById(R.id.textView1);
tv1.setTypeface(tf);
final TextView tv = tv1;
....
Tuna Karakasoglu
  • 1,262
  • 9
  • 28
  • i found a solution public class Personaggi extends Activity { TextView tv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //change font Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/HeroQuest.ttf"); tv = (TextView) findViewById(R.id.corpo); tv.setTypeface(tf); View.OnClickListener gestore = new View.OnClickListener() { public void onClick(View view) { //here put thw switch for the button, and i can use the tv without declare it final – Lele Apr 03 '13 at 20:51
0

Define your TextView as field

i.e private TextView tv;

if u need more clarification on crash.paste the code in your OnClicklistener
Pragnani
  • 20,075
  • 6
  • 49
  • 74
Viswanath Lekshmanan
  • 9,945
  • 1
  • 40
  • 64