Reputation: 2040
So I'm making a game and one of the central aspects of the game (like most other games) is how much money you have. So, on each activity I have a TextView that displays the players money (I have a "global" static variable setup so I can access the player's current money from anywhere in the app).
As of now, I have a bunch of different textViews with ids like "moneycount1, moneycount2, etc." and on each activity, when I resume the activity, I just set the textview to display the player's money.
However, is there any way to have a "permanent" textview, that will appear in the same location, and display the same information, on every single activity in the app? This would save a lot of time and repetition.
Thanks
Upvotes: 0
Views: 490
Reputation: 8191
each activity has to have its own layout, so this will probably not be possible. the first option I could think of to make life easier, would be to:
consider using the include tag, to include the same component in different layout files : https://developer.android.com/training/improving-layouts/reusing-layouts
you simply make a layout file with your textview/components in and then use as:
<include layout="@layout/yourLayoutName"/>
in your layouts
alternatively, you could also have a single activity with different fragments in it, then store this global value in the activity, so that the fragments all have access to it OR maybe try build different layout files with this fragment inside it
have a look at this : https://developer.android.com/guide/components/fragments
which says that :
A Fragment represents a behavior or a portion of user interface in a FragmentActivity. You can combine multiple fragments in a single activity to build a multi-pane UI and reuse a fragment in multiple activities. You can think of a fragment as a modular section of an activity, which has its own lifecycle, receives its own input events, and which you can add or remove while the activity is running (sort of like a "sub activity" that you can reuse in different activities).
Upvotes: 2