Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Studio: Can you display a view just for preview?

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>
like image 889
Julian Honma Avatar asked Jun 22 '17 08:06

Julian Honma


People also ask

What is layout Preview?

The Layout Preview displays a representation of how your XML code will be displayed on the device. It also allows you to see the different configurations of your layout, such as how would it look like while in portrait or landscape, or how does that TextView look on multiple locales such as English, German or Greek.

Where is layout Preview in Android Studio?

Use Android Studio's main menu In the main menu, select File > New > XML > Layout XML File.

What is Camerax preview?

↳ androidx.camera.core.Preview. A use case that provides a camera preview stream for displaying on-screen. The preview stream is connected to the Surface provided via SurfaceProvider . The application decides how the Surface is shown, and is responsible for managing the Surface lifecycle after providing it.


1 Answers

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

like image 69
lelloman Avatar answered Sep 22 '22 05:09

lelloman