7

My app has a lot of views that are containers for fragments (which load an image and other views) and depend on an API to fetch images. To make the development of the design easier, I like to add a sample of that image in my xml. Right now, I'm adding a RelativeLayout with the FragmentContainer and a dummy ImageView using different visibility values for android:visibility and tools:visibility.

Is there a better way to show images just for preview purposes ? I'd like to have the preview Views not compiled in the release version.

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:visibility="gone"
        tools:adjustViewBounds="true"
        tools:src="@drawable/image"
        tools:visibility="visible" />

    <RelativeLayout
        android:id="@+id/FragmentContainer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</RelativeLayout>
Julian Honma
  • 1,674
  • 14
  • 22
  • check this out also https://www.youtube.com/watch?v=Hx_rwS1NTiI you can use JSON file i guess – Rahul Jun 22 '17 at 08:32

1 Answers1

13

If I understood your problem correctly, you could do something like this: instead of using dummy views, use

<include tools:layout="@layout/layout_preview" layout="@layout/layout_actual"/>

where layout_preview.xml is whatever you want to use only in the preview, and layout_actual.xml is what will be used in the app

in case you wanted to only add a view in the preview, but have no view at all in the app, you can use a layout_actual.xml with an empty merge tag

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"/>

if you don't want to include useless layout, you might want to create the dummy ones only for debug build type, it will show an error in the layout because layout_dummy will be missing but since it's tools attribute you should be able to compile and run

lelloman
  • 13,883
  • 5
  • 63
  • 85
  • Nice, I didn't think of that ! The only inconvenience I see with this method is that I'll be creating lots of files for preview purposes. These layout files would end up being compiled as well no ? – Julian Honma Jun 22 '17 at 09:06
  • 1
    lots of file, true, I think this fully qualify as a hack. if you don't want to compile them with the release you could create them only for debug build – lelloman Jun 22 '17 at 09:07
  • Ah, smart, that would work for me, thanks ! I'm going to edit my question to reflect my questions in these comments, can you update your answer after as well ? – Julian Honma Jun 22 '17 at 09:14
  • @lelloman how to make this empty layout include compile only on debug build just like you said? – NameTopSecret Sep 18 '21 at 20:34
  • @NameTopSecret you should put the XML in `debug/res/layout` directory instead of `main/res/layout` – lelloman Sep 19 '21 at 21:20