Question

Lets say we have a main class with 5 buttons, where when each clicked go to an activity that displays information with 2 textviews.

The layout for all 5 activities will be the same, so naturally I would want to use one activity and reuse it by changing the text displayed in those 2 textviews for each button pressed.

How can I do this? Thinking about having a global intent in my main class, which is called by startActivity() for each button together with .putExtra() method to send the extra data in order to know from what button it came from (so that I can change the textviews).

Any other solutions that are better?

One more question, how does one change the title of the activity with java code while the app is running? Before the app starts one can use the xml, but how to change it if I'm trying to reuse the activity?

<activity android:name=".MynewClass"
          android:label="@string/class_text">
</activity>
Was it helpful?

Solution

The layout for all 5 activities will be the same, so naturally I would want to use one activity and reuse it by changing the text displayed in those 2 textviews for each button pressed.

That is certainly one possibility.

How can I do this?

Your solution (Intent with extras) seems fine. However, I would not make it a "global" Intent, since you are changing it via the extras. Just create a new Intent when you need it.

how does one change the title of the activity with java code while the app is running?

Call setTitle().

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top