Ryan
Ryan

Reputation: 835

Android Studio - How To Add A Fragment? (January 2019 Update)

The new Android Studio update came out sometime last week (January 2019 Version 3.3). In the previous version you'd be able to create new project and click a checkbox that said "add a fragment". Unfortunately, in the new update I can't find this checkbox or anything related.

Do we now have to add fragments by hand? Or is there a way to create a project with a fragment?

Upvotes: 3

Views: 1277

Answers (2)

Angel Koh
Angel Koh

Reputation: 13485

Another way to do it while retaining the current Activity is to auto generate the fragment using

New (or Alt+INS) -> Fragment -> Fragment (any of the subtypes).

this will generate your frament xml layout together with the code stub.

Then in the main_activity.xml replace the "helloWorld TextView" (if you create an empty activity) or "content_main.xml" (if you create the one with FAB) with the following...

<fragment
    android:id="@+id/fragment"
    android:name="com.example.MainFragment" 
    tools:layout="@layout/fragment_main" /> 

assuming you name it as MainFragment in the com.example package.

Upvotes: 1

Ryan
Ryan

Reputation: 835

I found the solution. Before Version 3.3 you were able to create a new project and set the basic activity as a fragment. Since Version 3.3 update it no longer gives you the option to create a fragment when creating a new project.

So this is how you're supposed to do it:

Create a new project with whatever activities you want.

Switch to Android View -> Right click on "app" under the navigator -> New -> Activity -> Basic Activity Now a separate window will open up and you're able to select the checkbox "Use a fragment".

Upvotes: 3

Related Questions