I am using Navigation Editor in the android studio and my android studio version is 3.3.2.
my problem is when I add a fragment to the navigation editor, the attributes of the fragment are not showing in design mode. look at the picture:
mobile_navigation.xml
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:tools="http://schemas.android.com/tools">
<fragment id="#+id/currentWeatherFragment2"
name="ir.houmansanati.forecastmvvm.ui.weather.current.CurrentWeatherFragment"
label="current_weather_fragment" tools:layout="#layout/current_weather_fragment"/>
<fragment id="#+id/futureListWeatherFragment4"
name="ir.houmansanati.forecastmvvm.ui.weather.future.list.FutureListWeatherFragment"
label="future_list_weather_fragment" tools:layout="#layout/future_list_weather_fragment"/>
<fragment id="#+id/futureDetailWeatherFragment2"
name="ir.houmansanati.forecastmvvm.ui.weather.future.detail.FutureDetailWeatherFragment"
label="future_detail_weather_fragment" tools:layout="#layout/future_detail_weather_fragment"/>
<fragment id="#+id/settingsFragment2" name="ir.houmansanati.forecastmvvm.ui.setting.SettingsFragment"
label="SettingsFragment"/>
</navigation>
as you see my fragments in XML have name and label and id attributes but all of them are labeled as fragment. even when I set the attributes from the design mode after pressing Enter the text is gone but it changes in XML text.
I have tried Invalidate Caches/Restart but the problem still exists.
Any help?
You're using id=, name=, and label=. As per the Implementing Navigation documentation, these need to be android:id=, android:name=, and android:label=, respectively.
Note that your topmost <navigation> element must also contain xmlns:android="http://schemas.android.com/apk/res/android" in order to use the android: prefix.
Related
I have one app and one dynamic feature module i wish to navigate from
App nav graph
<?xml version="1.0" encoding="utf-8"?>
<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/mainFragment">
<!-- Main Fragment from App Module -->
<fragment
android:id="#+id/mainFragment"
android:name="com.xyz.MainFragment"
android:label="MainFragment"
tools:layout="#layout/fragment_main">
<action
android:id="#+id/action_mainFragment_to_nav_graph_home"
app:destination="#id/nav_graph_home" />
</fragment>
<!-- Home Navigation from App Module-->
<include app:graph="#navigation/nav_graph_home" />
<include-dynamic
android:id="#+id/nav_graph_dashboard"
android:name="com.feature.dashboard"
app:graphResName="nav_graph_dashboard"
app:moduleName="dashboard" />
</navigation>
And feature navigation
<?xml version="1.0" encoding="utf-8"?>
<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_dashboard"
app:moduleName="dashboard"
app:startDestination="#id/dashboardFragment1">
<fragment
android:id="#+id/dashboardFragment1"
android:name="com.feature.DashboardFragment1"
android:label="DashboardFragment1">
</fragment>
</navigation>
Returns error
java.lang.IllegalStateException: The included <navigation>'s id com.xyz.dashboard:id/nav_graph_dashboard is different from the destination id com.xyz:id/nav_graph_dashboard. Either remove the <navigation> id or make them match.
It seems that removing id from feature navigation solves the issue but i couldn't find how to make them match even though both <include-dynamic> and <navigation> have the same id android:id="#+id/nav_graph_dashboard" I don't need id for <navigation> for this example but i wonder if it's possible when <navigation> has one
Remove the + from your ID in your feature navigation graph:
android:id="#id/nav_graph_dashboard"
When you use the #+id syntax, you create a new ID in your dynamic feature's package (you'll note the exception explicitly calls out the package names for each resource ID for exactly that reason). By removing the +, you use the already created ID from the main module's package, which makes them match.
The same IllegalStateException can be achieved simply but including app:moduleName="app" in the root nav graph in the root module (usually app module). Take care to ensure that attribute is only set in dynamic feature module nav graphs.
I have a single activity application with 3 modules - app, list & detail. My activity is in app module, it's hosting the only NavHostFragment. All modules have their own navigation graphs. detail's starting point requires a long parameter. app's graph is parenting other graphs:
<?xml version="1.0" encoding="utf-8"?>
<navigation
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/nav_main"
app:startDestination="#id/nav_list">
<include app:graph="#navigation/nav_list" />
<include app:graph="#navigation/nav_detail" />
</navigation>
But by default it's disabled to add actions to included graphs on editor:
I can add a global action in xml file which then shows up in editor:
<?xml version="1.0" encoding="utf-8"?>
<navigation
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/nav_main"
app:startDestination="#id/nav_list">
<include app:graph="#navigation/nav_list" />
<include app:graph="#navigation/nav_detail" />
<action
android:id="#+id/action_global_detailFragment"
app:destination="#id/nav_detail" />
</navigation>
I don't want to use global actions but instead add proper actions which will encapsulate navigation pattern. Nested graphs already contain their navigation logic and only may need input for entry point. I'm not sure if this is not supported and I'm missing something or else why not ? What's the way to navigate between two or more included graphs ?
You have to set android:idto your included graphs, otherwise you cannot add actions.
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
<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
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>
there is another way to have the preview in navigation xml.
First go in your xml fragment add
tools:context="com.packagename.nameFragment"
And that's it
if you go in your navigation file you can see the preview inside the selection and the navigation editor
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
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>
I'm having a slight issue here where the overflow options for the ActionBar are showing up in the emulator but not the layout preview. I'm currently using Android Studio v 2.1.3.
Has anyone else come across the same issue?
Layout Preview
Emulator Screenshot
In your layout file, add the tools:menu attribute directly to the Toolbar widget:
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
tools:menu="#menu/your_menu"/>
According to the docs:
tools:menu
Intended for: Any root <View>
This attribute specifies which menu the layout preview should show in
the app bar. The value can be one or more menu IDs, separated by
commas (without #menu/ or any such ID prefix and without the .xml
extension).
However, I find that it only works if:
the attribute is added directly to the Toolbar widget
the value includes the #menu/ prefix
Try to add a tools:menu to your root view in the layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:menu="menu1,menu2" />
more info here:
http://tools.android.com/tech-docs/tools-attributes
I'm trying to minimize or collapse the NavigationView / NavigationDrawer when my app is running on tablets.
The result I want is the one used by GMail app, see screenshot below (you can see the desired layout collapsed on the left).
Does exists any method or a pattern to follow to achieve this?
After reading carefully this official NavigationDrawer guidelines, I found that my question used wrong keywords : it is called mini navigation drawer (that's why I edited my question first).
So I was able to find an answer:
Use third party library, which is proposed in this answer (yes, my question is a duplicate!);
Develop your own solution, by following this sample; be aware that you have to add some fancy animation and other decoration in order to respect Material design guidelines.
Any way, the trick is to simply add a margin left to the detail view (FrameLayout in this case), like this:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SlidingPaneLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--Master fragment-->
<fragment
android:name=".MainFragment"
android:layout_width="220dp"
android:layout_height="match_parent"
android:id="#+id/fragment_master">
</fragment>
<!--Detail layout -->
<FrameLayout
android:layout_width="1000dp"
android:layout_height="match_parent"
android:layout_marginLeft="56dp">
</FrameLayout>
</android.support.v4.widget.SlidingPaneLayout>