Can I show a sample view when the layout is normally empty & only populated programmatically?
You must include a layout so include it but set its visibility to gone or set ImageView sizes to zero while having what you like with tools attributes. Here I've done both.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<include layout="@layout/some_sample_layout"/>
</LinearLayout>
<LinearLayout
android:visibility="gone"
tools:visibility="visible"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="0dp"
android:layout_height="0dp"
tools:layout_width="100dp"
tools:layout_height="100dp"
tools:scaleType="centerCrop"
tools:src="@tools:sample/backgrounds/scenic" />
</LinearLayout>
I think the cleanest and most universal approach for displaying dynamically inflated views in AS designer would be to use <include />
tag, quite similar as in your example.
Our <include />
tag must have layout
property, to make the app buildable. We can omit it by attaching dummy layout with <merge />
root. Because it has no children, no views will be inflated.
Universal lt_merge_empty.xml
:
<?xml version="1.0" encoding="utf-8"?>
<merge
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="0dp"
android:layout_height="0dp">
<!--
Empty merge tag, so we can use <include/> to show dynamically inflated layouts in designer windows
For example:
<include
tools:layout="@layout/your_inflated_layout"
layout="@layout/lt_merge_empty"
/>
-->
</merge>
Then use it in your layout:
<include
tools:layout="@layout/your_inflated_layout"
layout="@layout/lt_merge_empty"
/>
Please note: for some reason, tools:layout
must be placed above layout
property, otherwise it won't be rendered.
Edit: This approach is more universal than @Sina's one bacause usually you will want to display the exact layout that you are dynamically inflating, so obviously you can't change its android:visibility
to gone
.