Skip to content

Instantly share code, notes, and snippets.

@MaartenS
Created February 14, 2016 13:35
Show Gist options
  • Save MaartenS/7809171a801a20142e6b to your computer and use it in GitHub Desktop.
Save MaartenS/7809171a801a20142e6b to your computer and use it in GitHub Desktop.

Android - Tools attribute in layouts

The XML tools attributes allow you to see a more clear picture of your complete layout, without writing throwaway code or using dummy values that a user might accidentally see. To use the tools attributes in your XML file, you will first need to add the tools namespace. You can do this by adding xmlns:tools="http://schemas.android.com/tools" to your file’s parent view element. Alternatively, you can start typing tools in any view element, then Opt+Enter (Alt+Enter on Windows) to automatically add the namespace. It’s also a good idea to include context for your tools, so any custom themes and styles are applied to the preview as well. Do this by including tools:context=".MainActivity" in the parent element.

tools:text="title" - Set text on your view only for the layout preview. There is no lint warning for using a hardcoded string here, since it is just for debugging.

tools:src="@drawable/my_img" - Set the image on your view only for the layout preview.

tools:visibility="visible" - Set your view elements to visible, invisible, or gone only for the layout preview. tools:listitem="@layout/custom_layout" - Show the actual list item layout inside your ListView preview. Use tools:text and tools:src to fill out your list item layout, and you will see the full picture of what your ListView should look like.

tools:showIn="@layout/acitivity_main" - Render your current layout inside the layout which includes it. This allows you to get the big picture of what your layout will look like, without building the entire project, or having a single monolithic layout file. The wrapping layout is greyed a bit in the preview, so it is clear which parts of the layout you are currently editing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment