coolDude
coolDude

Reputation: 717

Reusing the Same Layout to Display Different Text

I have learned the basics of Android development: LinearLayout and RelativeLayout, how MainActivity and activity_main interact, etc. I also have some Java experience from the past, although I wouldn't consider myself an expert.

The app I am now trying to create in general consists of two views: a welcome screen and a screen which displays a fact. There are multiple facts that the app will show, so the second layout will be shown more than once. Both will beLinearLayouts containing TextViews. The overall flow of the app will be as follows:

enter image description here

The thing that I'm struggling with now is determining the best way to design this app. I understand that there are an immense amount of options to do this. What I've done up to this point is create the Welcome layout and the layout of the Fact. I've used an Intent in order to transition from the Welcome to Fact layout. And it works just fine. However, using Intents for this just seems inefficient to me: I need to create a separate class and XML layout for each fact. But each fact will use the exact same layout, just with different text to be displayed. And obviously a different transition for the Back and Next buttons. I feel like there must be a more efficient way to do this for Android.

To give everyone a scope of my knowledge, I've thus far only learned about designing a single layout for an Android app, so only one MainActivity and activity_main. I was exposed to Intents, but very minimally.

With all of that in mind, my overall question is the following:


Is there a way for me to reuse the Fact layout and simply load in the next fact each time the user hits the Next button?


From Java, I am aware of arrays, ArrayList,switch statements, etc. So, that makes me feel like I should be able to utilize something along those lines to be able to load in the next fact.

If not through Java though, there must be some sort of pre-built class or XML layout provided in Android that allows me to achieve this somehow. I'm just not sure what to use....

In addition to my overall question, I also was curious about the following:

How should I handle the Next and Back buttons? In my current implementation of the app, I've used an Intent to switch from the welcome screen to the first fact. However, as I said, I feel like this is an inefficient approach.

I did find out about ViewSwitchers, but that would again require me to create a separate layout for each fact (at least from what I understand of them). However, I could possibly use it to switch from the welcome screen to the fact screen. But that still doesn't get me to where I want to be. I also looked into Fragments, but I don't feel those are applicable to my design at all.

Any help on this topic would be greatly appreciated!

Upvotes: 2

Views: 1208

Answers (3)

OneCricketeer
OneCricketeer

Reputation: 191831

You could use Fragments and you can share a single XML layout between all your Activitys and Fragments, however that is actually more work than required for your demonstrated flow.

You can make a Fact object and an ArrayList that you can easily cycle through in one Activity (or Fragment). This fact will store a title and some text. (The welcome screen is just another fact, to make things simple)

You would add some click listeners to your buttons that will update the position in the list and you can change the textviews from there.

Additionally, if you want to persist these Facts and not have to create new objects when your app loads, then you can look into SQLite.

Upvotes: 0

KISHORE_ZE
KISHORE_ZE

Reputation: 1466

You can do this without entering any complex topics.

Have a layout for your facts. Just 1. And a java file for it. I assume you know how to give an id to the text view and get it in the java file. I also assume this fact is shown on a some basis. (Random?). Well whatever you do to open that particular xml intent in you welcome screen use the same criteria and then on your text view use the TextView.setText() method. Store your facts in an array like this

String[] facts = {"fact 1", "fact 2"}

And so on. In your set text method Do TextView.setText(facts[0]) Replace 0 with a random number.

Upvotes: 3

Richard Goodman
Richard Goodman

Reputation: 317

If you use the viewPager, you can swipe between "pages". You can do this using fragments, and have a fragment "factory" to reuse your layouts.

I've just worked through this run through ...

http://www.i-programmer.info/ebooks/android-adventures-mastering-fragments.html

Upvotes: 0

Related Questions