Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

Jason Devers
Jason Devers
98 Points

Changing an activity TextView text from a class.

Hello everybody, I have MainActivity which contains a TextView and I would like to change the text of this TextView from another class. I have try a lot of things but nothing has work for me this far.

public class MainActivity extends AppCompatActivity {

public static final String  TAG = MainActivity.class.getSimpleName();

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    final ApiCall apiCall = new ApiCall();

    Button b1 = (Button)findViewById(R.id.b1);

    TextView originText = (TextView)findViewById(R.id.origin); // <--- TextView that has     to be change

    b1.setOnClickListener(
            new Button.OnClickListener() {
                public void onClick(View v) {

                    apiCall.call();

                }
            }
    );

}

}


public class ApiCall{

public static final String  TAG = MainActivity.class.getSimpleName();

private JsonDataClass mJsonData;

public void call() {

    String API_KEY = "";

    String url_request = "" + API_KEY;

    OkHttpClient client = new OkHttpClient();

    Request request = new Request.Builder().url(url_request).build();

    Call call = client.newCall(request);

    call.enqueue(new Callback() {

        @Override
        public void onFailure(Call call, IOException e) {

        }

        @Override
        public void onResponse(Call call, Response response) throws IOException {

            try {

                String googleJsonResponseData = response.body().string();

                //Log.v(TAG, googleJsonResponseData);

                if (response.isSuccessful()) {

                    mJsonData = getTravelInformation(googleJsonResponseData);

                    updateScreen(mJsonData.getTravelDistance()); 

                }

            } catch (IOException | JSONException e) {

                Log.e(TAG, "Execption acaught: ", e);

            }

        }

    });

}

private void updateScreen(String origin){

    //Change the textView from here to String origin

}

}

Rune Andreas Nielsen
Rune Andreas Nielsen
5,354 Points

There is a few ways you can do it. The best way from my view is to do it from your activity class, if you dont want to do that. You will have to pass the "TextView" object to your method, that way you can change it like you would normally.

So:

private void updateScreen(TextView textview){ // the textview text change code here }

Another way is to pass the TextView object to a constructor in the class you want to use it, and set it as an property for your class, that way you can access in all methods of your class.

Good luck

2 Answers

Suleyman Orazgulyyev
PLUS
Suleyman Orazgulyyev
Courses Plus Student 5,798 Points

Hey, as I understood you want to change your TextView from another class. Let's say your class MAIN contains this textView and you want to modify this textView from another class called PEOPLE.

In your class PEOPLE you need to create a public method called anything you want that takes into parameter your TextView and does anything you want, it will look something like this:

public class PEOPLE{

public void whatever (TextView     mTextView){

        mTextView.findViewById(R.id.id_of_textview);

mTextView.setText ("Hey I'm from class PEOPLE!");
    }
}

I hope it works and I hope I helped :)

Good luck! :)

Jason Devers
Jason Devers
98 Points

I fix the problem, thx for the help!