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

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>

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

Related

How to add Imageview on WebView in Final Android App

As i am going to make Alerting App, I am using my website instead of XML code. But for going to my alert activity for web Activity, I made an intent which works onclick on my small image view.
It's my XML code Below:
activity_web.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".WebActivity">
<WebView
android:id="#+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true">
</WebView>
<ImageView
android:id="#+id/imageView"
android:layout_width="81dp"
android:layout_height="75dp"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
tools:srcCompat="#drawable/bell_icon" />
</RelativeLayout>
But the main Problem is that imageview is not visible when I install's the app in my mobile.
It is also visible in my android Studio Layout.
You Check My Android Studio Layout here
Now tell me what should do for making visible it in final build apk which will work in my mobile.
you can set it properly by using constraint layout, here is a code maybe it's work!
In this i set an ImageView at the top and webview is set at bottom of ImageView.
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="180dp"
android:background="#color/white"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"/>
<WebView
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintTop_toBottomOf="#+id/imageView"
app:layout_constraintBottom_toBottomOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
you are using wrong attribute namespace, I'm supprised that AS preview tool is showing this image, it shouldn't. but your ImageView is there on device, it is clickable (wehn you set so), it just have transparent content (wrongly resolved srcCompat attr, as below). as a test you may add android:background="#789810" tag, ImageView will show up with visible background (still without image)
in your code you have
tools:srcCompat="#drawable/bell_icon"
tools namespace points on "some tools", like used tools:context=".WebActivity". for setting custom attribute of particular View you have to use (custom) resources namespace, these are declared in your XML with xmlns:app="http://schemas.android.com/apk/res-auto" line, thus you should use below line
app:srcCompat="#drawable/bell_icon"
btw. if you are using srcCompat the you should also use AppCompatImageView. it works with usual ImageView only because AS project by default is exchanging all ImageViews to AppCompatImageViews during building project to APK/bundle. this behavior may be changed or disabled, then your ImageView will stay without any image set. fully proper line for setting image is default src attribute of ImageView, so:
android:src="#drawable/bell_icon"
android namespace in here as this attribute belong to framework ("built-in"), not some additional library and View (AppCompatImageView comes from AppCompat or AndroidX library)

When to use CardViews vs tools:listitem when creating RecyclerViews?

I came across this code:
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/darker_gray"
tools:listitem="#layout/recyclerview_item" />
where tools:listitem was a layout file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/textView"
style="#style/word_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/holo_orange_light" />
</LinearLayout>
My question is, when creating recyclerviews, when should we choose Cardviews as its items and when should we choose layouts such as the one mentioned above?
What are the advantages one provides over the other or are they the same(Note that the recyclerview_item layout XML doesn't have any Cardview tags, so they aren't atleast literally the same)?
tools:listitem only sets a design-time preview for a list item by providing a specific layout resource, be it a CardView-based layout or something different.
You can use tools:listitem along with other tools attributes and #tools/sample resources to emulate components' runtime or compile-time behaviors such as layouts, dummy data, visibility e.t.c.
Those attributes do not affect your app's runtime behavior in any way.
Additionally, you can read this tutorial on tools attributes.
As for cards vs regular layouts, there are some in using cards as containers: cards support elevation, shadows, rounded corners and have a consistent visual style while supporting different content lengths with no additional actions required e.t.c
Personally, I'd use a CardView-based layouts in cases where I need to show some list items with straightforward layouts in a platform-consistent way.

Android Studio preview - set an adapter to a RecyclerView

Is it possible to set up an Adapter to a RecyclerView on the layout xml file so that it renders it design-time? Similar to the way it uses tools:context=my.package.MyFragment to render other things.
So far I know, you can't really add your own adapter, but is possible to use the decent default preview adapter, specifying your layout item.
Something like that:
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:listitem="#layout/your_item_layout"
/>
</FrameLayout>

Eclipse Content Assist Not Working Properly For Android Layout

Whenever I have Views inside a ViewGroup inside a android.support.v4.widget.DrawerLayout, Eclipse's autocomplete starts working weird. It doesn't display most of the properties
Example with DrawerLayout -> LinearLayout -> ImageView:
In the previous screenshot, you can see that I typed "android:scale..." and the IDE's intellisense isn't showing me the ImageView's android:scaleType
If I use Eclipse's Properties window for the ImageView, I can see that it is only displaying the basic properties for "View" (and not for ImageView...):
Now if the ImageView is not a descendant of the DrawerLayout and I put it somewhere else, the autocomplete works properly:
I've seen related questions such as:
Autocomplete does not work in XML files in particular hierarchy
Content Assist doesn't work in xml with support library
but they have been dead with no answers for quite a while...
Content assist doesn't work for Views (be it TextViews, EditText, Buttons...) that I add inside the DrawerLayout. It only suggests the 'basic' properties.
Does anybody know why this happens or how to fix this?
Sample layout:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/some_image" />
</LinearLayout>
<RelativeLayout
android:id="#+id/drawer_view"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start" >
</RelativeLayout>
</android.support.v4.widget.DrawerLayout>
Just to give some closure to this 5 months-old question, I'll quote and link to Atul O Holic's answer. Thanks to Dileep Perla for pointing me there.
When using support package Widgets this is a common scenario (a weird
one too).
Here you using, android.support.v4.widget.DrawerLayout and hence the
issue. Happened with me a lot of time.
You can also try couple of options mentioned here.
I've found that using the tag can be handy in this case. Just put your layout that is inside the drawer in a separate layout file and then include it inside the drawer layout.

Modified source of custom view in library wont affect on XML layout

I'm using in my project this library with custom ImageView which is able to handle pinch zoom and double tap zoom events: https://github.com/jasonpolites/gesture-imageview. I need to change in that library's source few things, because I want to use it with ViewPager. The problem is that the when I modifying source code of custom view in library and after this I adding this modified custom view to layout in XML file like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:gesture-image="http://schemas.polites.com/android"
android:id="#+id/layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#000000">
<com.polites.android.GestureImageView
android:id="#+id/image"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
gesture-image:min-scale="0.75"
gesture-image:max-scale="10.0"
android:src="#drawable/image"/>
</LinearLayout>
my changes don't want to appear, I'm breaking source code (for testing) by changing all values from 0 to 100, when I creating layout programatically my changes appears, and custom view works as i assumed, but when I definie layout in XML there's no effect, even if I clean up some java files of library from source code custom view in XML layout still works like there's no change in source.
Have you tried Project => Clean of both your main project and the library?

Categories

Resources