1

There are many similar questions to this one, but they all ask about setting the text in a TextView from another CLASS. I want to know if there is a short and simple way to change the text from another METHOD within the same class.

So everything is in one class, but the TextView is setup in method 1, and i want to set the text for this text view in method 2.

Before posting duplicate question replies and down-voting me, please read the question. I want to change the text from WITHIN the same class, just on different methods.

More info...

//Method 1 has:
final TextView tvTitle = (TextView) findViewById(R.id.tvTitle);

//Method 2 has...
tvTitle.setText("TITLE");
3
  • I don't understand what is your problem. You have just to do something like yourTextView.setText("New text"); in your method 2 Commented Dec 27, 2012 at 21:42
  • yourTextView cannot be resolved when i try to do that. it makes me do everything in the same method Commented Dec 27, 2012 at 22:07
  • Are you declared yourTextView as an attribute of your class ? Commented Dec 27, 2012 at 22:11

1 Answer 1

2

You have to declare the object before using it, you have two solutions to your problem :

First solution :

public class MyClass
{
    private TextView tvTitle;

    MyClass()
    {
           tvTitle = (TextView) findViewById(R.id.tvTitle);
    }
}

Or second method :

In the two method write this line :

TextView tvTitle = (TextView) findViewById(R.id.tvTitle);

But the first is a better solution

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.