How do I change TextView Value inside Java Code?


I am working on a android program. A user clicks on a button I do some math and I would like to change the values that I have on my view in some TextView objects. Can someone please tell me how to do it in my code?

6/5/2015 7:52:42 AM

Accepted Answer

I presume that this question is a continuation of this one.

What are you trying to do? Do you really want to dynamically change the text in your TextView objects when the user clicks a button? You can certainly do that, if you have a reason, but, if the text is static, it is usually set in the main.xml file, like this:


The string "@string/rate" refers to an entry in your strings.xml file that looks like this:

<string name="rate">Rate</string>

If you really want to change this text later, you can do so by using Nikolay's example - you'd get a reference to the TextView by utilizing the id defined for it within main.xml, like this:

final TextView textViewToChange = (TextView) findViewById(;
    "The new text that I'd like to display now that the user has pushed a button.");
5/23/2017 12:09:51 PM

First we need to find a Button:

Button mButton = (Button) findViewById(;

After that, you must implement View.OnClickListener and there you should find the TextView and execute the method setText:

mButton.setOnClickListener(new View.OnClickListener {
    public void onClick(View v) {
        final TextView mTextView = (TextView) findViewById(;
        mTextView.setText("Some Text");

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow