68

I am interested to try the Navigation graph showed in the Android Studio. But I got the preview unavailable after I import the google sample

I used the Android Studio 3.2 Preview Canary 16

enter image description here

<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    app:startDestination="@+id/launcher_home">

    <fragment
        android:id="@+id/launcher_home"
        android:name="com.android.samples.arch.componentsbasicsample.StartFragment"
        android:label="Home">

        <action
            android:id="@+id/end_action"
            app:destination="@id/end_dest" />

    </fragment>

    <fragment
        android:id="@+id/end_dest"
        android:name="com.android.samples.arch.componentsbasicsample.EndFragment"
        android:label="End"
        >

    </fragment>
</navigation>

Update on 10/6/2018:

Even I rebuild the project it doesn't work. But if added new screen, it showed the new one in preview mode

enter image description here

Long Ranger
  • 5,888
  • 8
  • 43
  • 72

3 Answers3

233

You should click on "text" tab in navigation editor (xml file of the navigation graph), and add:

tools:layout="@layout/layout_name"

inside destination element.

Should be something like this:

<fragment
    android:id="@+id/someFragment"
    android:name="com.freesoulapps.navigationtest.fragments.SomeFragment"
    android:label="Some Fragment"
    tools:layout="@layout/layout_name">
</fragment>
Alex
  • 9,102
  • 3
  • 31
  • 35
  • 1
    Okay. This solution works. But I have one question about this statement. How can I use the default fragment layout instead of the design time layout? – Long Ranger Jun 10 '18 at 02:25
  • 1
    The navigation editor doesn't know about the layout that you put inside "onCreateView" method of your fragment. – Alex Jun 10 '18 at 05:52
  • it is not correct, even you create the blank Activity with the Fragment one, the design panel still show the fragment layout in activity.xml without setting the design time attribute. – Long Ranger Jun 10 '18 at 08:39
  • I didn't understood, where you create blank activity? inside navigation editor? – Alex Jun 12 '18 at 08:52
19

there is another way to have the preview in navigation xml. First go in your xml fragment add

tools:context="com.packagename.nameFragment"

exemple for my frag layout

And that's it

if you go in your navigation file you can see the preview inside the selection and the navigation editor

enter image description here enter image description here

And if you look in the code is auto write

tools:layout="@layout/layout_name"

For me is more logic to have the preview before add the fragment in the navigation editor. May be there are method for add automatically the tools:context in the layout

Autocompletation not suggest for tools:context Fragment only suggest the tools:context Activity host so you need to write the fragment's name... if someone have a trick for this

learn more about tools:context : enter link description here

James
  • 311
  • 3
  • 4
12

Just add tools:layout="fragmentname" to every fragment whose preview is not visible. Example:-

<navigation 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/nav_graph"
    app:startDestination="@id/startFragment">

    <fragment
        android:id="@+id/pickupFragment"
        android:name="com.example.cup_cake.PickupFragment"
        android:label="fragment_pickup"
        tools:layout="@layout/fragment_pickup" />

</navigation>
Ayush Kumar
  • 121
  • 1
  • 2