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 Build a Self-Destructing Message Android App Starting the App Adding the Login Screen

Alex Feinberg
Alex Feinberg
2,025 Points

Fragment in the Ribbit login and sign up activities

In the Ribbit Android project, when creating the login and sign up activities with the latest and greatest New Activity wizard, you need to provide a Fragment Layout Name in addition to the Layout name. This results in two files in the layout folder (e.g. activity_login.xml and fragment_login.xml. Fleshing out the fragment_login.xml (as opposed to the activity_login.xml as in the video) is simple, but things start getting tricky when you try to add the OnClickListener in the LoginActivity. The findViewById(R.id.signUpText) method returns null, presumably because that ID does not exist in the activity_login.xml, but is in the fragment_login.xml instead. I've played with the fragment manager to try to find the signUpText TextView from the fragment, but haven't cracked it yet. I'm sure there's an easy way to drill down into the fragment to get the desired view. Right?

2 Answers

Ben Jakuben
STAFF
Ben Jakuben
Treehouse Teacher

I'm making some notes about this today and recording a fix next week. I answered your other post first and mentioned some options. I'll copy and paste them in here:


Anyhow, what this means is that the Login and Signup Activities will look a little different. I'm going to records some fixes next week and then we'll pop them in, but in the mean time, check out these instructions on how to remove Fragments (you'll have to scroll down a little). Or you can keep them and make layout changes in the fragment XML files and add code in the Fragment's onCreateView() method instead of the Activity's onCreate() method.

Or a third option: download the project files and continue from there! Hope this helps...

David Emelianov
David Emelianov
3,778 Points

Hey Ben, Could you clarify your second and third solutions?

For the second: Will all be well if I only make layout changes to the fragment XML files (leaving the other activity ones untouched) and add code in the Fragment's onCreateVew() instead of the Activity's onCreate() method? i.e. will leaving the non-fragment files untouched cause problems for the app?

For your third solution (downloading the project files) can I just download the code for the app as it should be at this point in the project? That's what it sounds like you're saying, but it seems a bit too good to be true... EDIT: My god. It's true. Answered my own question. Thanks!

Thanks!

Ben Jakuben
Ben Jakuben
Treehouse Teacher

Hey David,

Sorry for the delay! I'll try to clarify if you're still working on this part.

For 2): Yes, all will be well if you work within the existing Fragments. The way they are included by default makes them virtually the same as the Activities holding them. Everything that goes on in the Activity goes on in the Fragment; it's just like we're introducing one extra container in the mix.

For 3): You got it! I'm making a point to call out the project files early on in each project going forward, because it's not clear how they can help!

P.S. The Android Studio Beta has a much better handle on all this! Look for a new course using that soon!

Alex Feinberg
Alex Feinberg
2,025 Points

I removed the fragments and all is will. Thanks! I look forward to seeing updates to this or other Android projects which use the new default fragments approach.