Navigation Preview unavailable in Android Studio 3.2 Preview

AndroidAndroid Architecture-ComponentsAndroid Architecture-NavigationAndroid Studio-3.2

Android Problem Overview


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

Android Solutions


Solution 1 - Android

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>

Solution 2 - Android

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

Solution 3 - Android

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>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionLong RangerView Question on Stackoverflow
Solution 1 - AndroidAlexView Answer on Stackoverflow
Solution 2 - AndroidJamesView Answer on Stackoverflow
Solution 3 - AndroidAyush KumarView Answer on Stackoverflow