0

Once I click the button, I want text of different sizes to be displayed. I tried using the built-in html support for it but I found out that font size tag is not supporI'm using a TextView to do this but I keep getting an error saying "could not execute method of the activity".

Here is my activity main part:

<TextView
    android:id="@+id/header"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="32px" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/rams" 
    android:onClick="ramsShow" />

I have the header string resource in string.xml:

<string name="header">Header</string>

This is the method in my MainActivity that is called when the button is clicked:

public void ramsShow(View view)
{
    TextView test= new TextView(this);
    test = (TextView) findViewById(R.id.header);
    String tmp = "hi";
    test.setText(tmp);
}
covfefe
  • 1,819
  • 4
  • 26
  • 64

3 Answers3

0

As Ketan suggested you should have:

public void ramsShow(View view)
{
    TextView test = (TextView) findViewById(R.id.header);
    String tmp = "hi";
    test.setText(tmp);
}

To adjust sizes have a look here: How to set text size of textview dynamically for different screens

Community
  • 1
  • 1
Gavin
  • 452
  • 4
  • 11
0

In Activity class, above the onCreate Method, add this line.

TextView test = null;

Within onCreate method, add this code

test = (TextView) findViewById(R.id.header);

Idea is to move the line from ramsShow method to onCreate method.

Ketan Ahir
  • 6,458
  • 1
  • 20
  • 43
Prem
  • 4,665
  • 4
  • 28
  • 61
0

Within onCreate() write this code...

RelativeLayout lView = new RelativeLayout(this);

            myText = new TextView(this);
            myText.setText(Html.fromHtml("<b><center><font size='3'><font color=\"#145A14\">Welcome standard\n"</font></font></center></b>"));
lView.addView(myText);

setContentView(lView); }

Ashraf
  • 11
  • 2