To add a fragment to an activity programmatically, you need a placeholder in the activity's layout. The most common strategy is to place a FrameLayout object in the location where you want the fragment to appear, and then you can add or replace the fragment at runtime with a fragment transaction. A FrameLayout is a view group that's designed to only have one child object, so it makes the perfect placeholder.
- [Instructor] To demonstrate adding…a fragment with Java, I'll start with this project…named, AddWithJava.…I'll open the main activity's layout file,…and I'm going to change the fragment declaration…to a frame layout.…And I'll change its ID to fragment container.…I'll remove the class declaration…and also the tools attribute that incorporates the fragment…when I'm designing the activity layout.…Then I'll look at the layout in design view…and see that it now shows a blank screen.…
I'll also run the app on my device to confirm…that it behaves that way on the actual device.…So now I'll go back to Android Studio…and I'll add some Java code.…I'm going to add this code in the OnCreate method…so that the fragment is added as the activity starts up.…You could also do it in response to any other event…such as a button click.…Within the OnCreate method, I'll create an instance…of my SimpleFragment class.…I'll name it, fragment,…and I'll instantiate it with, new,…and then I'll call the No Arguments constructor.…
Then, I'll get a reference to the Fragment Manager…
Author
Released
9/1/2016- Configuring Android Studio
- Understanding fragments
- Creating a fragment class and layout
- Adding and removing fragments with Java
- Creating layouts for multiple screens
- Understanding arguments and callback methods
- Passing arguments to a fragment
- Choosing layout at runtime
- Displaying dialogs with fragments
- Using fragments for managing dialogs, shared preferences, and more
Skill Level Intermediate
Duration
Views
-
Introduction
-
Welcome1m 4s
-
What you should know1m 50s
-
Using the exercise files1m 39s
-
-
1. Getting Started
-
Understanding fragments3m 17s
-
2. Display Fragments in Activities
-
Display a fragment with XML3m 16s
-
Add a fragment with Java3m 50s
-
3. Manage Fragments at Runtime
-
Pass arguments to a fragment5m 18s
-
Choose layout at runtime3m 21s
-
-
4. Communication between Fragments and Activities
-
5. Display Dialogs with Fragments
-
Display a custom dialog5m 1s
-
Pass arguments to the dialog4m 16s
-
6. Other Uses of Fragments
-
Create a ViewPager adapter4m 53s
-
Conclusion
-
Next steps1m 11s
-
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.
CancelTake notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.
Share this video
Embed this video
Video: Add a fragment with Java