Android Studio: can you display a preview only view?
There are many views in my application which are containers for fragments (which load an image and other views) and depend on an API to retrieve images. To make my design easier, I would like to add a sample of this image to my xml. Right now I am adding a RelativeLayout with a FragmentContainer and a dummy ImageView using different visibility values ββfor android:visibility
and tools:visibility
.
Is there a better way to show images for preview purposes only? I would like the Views preview not to be compiled in 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>
source to share
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 what you want to use in preview only, and layout_actual.xml
is what will be used in the application
if you only want to add a view to the preview, but have no view in the app, you can use 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 the useless layout you can create dummy just for the debug build type, it will show an error in the layout because the layout_dummy will be missing, but since it's the tools attribute you should be able to compile and run
source to share