18

Creating an activity with navigation drawer in Android Studio causes this to be shown in the XML code:

<android.support.v4.widget.DrawerLayout
    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/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

If I remove the tools:openDrawer="start", on the layout preview, the navigation drawer will be closed instead of open.

I thought this was a replacement attribute, similar to android:text that can be replaced with tools:text to put some placeholder text in the layout preview. But when I change it to android:openDrawer or app:openDrawer, the app can't be compiled because they are unknown attributes.

So I wonder how tools:openDrawer works, and whether it is documented anywhere.

R. Zagórski
  • 20,020
  • 5
  • 65
  • 90
Randy Sugianto 'Yuku'
  • 71,383
  • 57
  • 178
  • 228
  • 2
    I didn't get what your question is about. If you add `tools:openDrawer="start"` to your `DrawerLayout` in the preview screen in IDE screen will be shown with opened drawer. If you remove that line, screen will be shown with closed drawer. `tools:...` attributes are used for previews only (showing some text, etc.). You've tried `android` and `app` because you wanted to open drawer on screen enter or just curious? – krossovochkin Oct 26 '15 at 08:50
  • What about docs: I think that everything changes too fast, so docs might be some kind outdated – krossovochkin Oct 26 '15 at 08:51
  • @krossovochkin I'm curious because of the lack of documentation for the recent android support libraries. It is indeed very hard to find an official, elaborated documentation for the new widgets. – Randy Sugianto 'Yuku' Oct 26 '15 at 12:06

2 Answers2

14

Android has a dedicated XML namespace intended for tools to be able to record information in XML files, and have that information stripped when the application is packaged such that there is no runtime or download size penalty. The namespace URI is http://schemas.android.com/tools and is usually bound to the tools: prefix:

This means that tools attribute is used by the IDE and not used in the code functionality.

I am still trying to find the complete documentation of all tools attributes. I can't find out the openDrawer attribute anywhere. But the following are great documentation on tools:

http://tools.android.com/tech-docs/tools-attributes
http://tools.android.com/tips/layout-designtime-attributes


UPDATE

Now a complete documentation of all tools attribute is available here.

Randy Sugianto 'Yuku'
  • 71,383
  • 57
  • 178
  • 228
Henry
  • 17,490
  • 7
  • 63
  • 98
13

It makes the navigation drawer visible on the preview screen in Android Studio essentially mimicking the user swiping the drawer onto the screen.

Without this attribute, you would see only the content view of the main screen.

I haven't tried it but I suspect substituting "start" for "end" would make the drawer on the right of the screen open (assuming there is one of course).

Kuffs
  • 35,581
  • 10
  • 79
  • 92
  • That sounds exactly how it is supposed to happen because without the openDrawer attribute, the drawer is not visible at all. The point of the attribute is to make it visible to demonstrate the UI with the drawer open. – Kuffs Aug 17 '20 at 06:52
  • Related to your last sentence.I tried with openDrawer="end", left and right, but the drawer is always on the left part (on the preview screen), so it does not work in Android Studio 4.0.1 As @Kuffs remarks removing the 'opendrawer' attribute will hide the drawer, also, adding in the navigationView tools:visibility="invisible" will hide it too. – LightMan Aug 17 '20 at 09:06