I am trying to create a layout in Android Studio but the preview of the layout is not showing at all. There is no errour though.
How can i get it to work properly? I will add a screenshot.
Preview not loading the layout
Thanks in advance.
<?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:id="#+id/content_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.example.diceout.MainActivity"
tools:showIn="#layout/activity_main">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Let's Play!" />
<Button
android:id="#+id/rollButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="155dp"
android:layout_marginStart="155dp"
android:layout_marginTop="25dp"
android:text="Roll" />
</RelativeLayout>
TextView
#ivan franken Could you post your #layout/activity_main Xml code. I saw this line tools:showIn:#layout/activity_main
in code on your content_main.xml. I tested it in my project and got similiar result to yours. But when I deleted it, it worked fine.
This is the view of Scanner activity. I am using SurfaceView and an image button with a 48dip png image icon. I want to design it more. It may not be upto the level of the second screenshot which is of SuperBeam app.
Here is the xml code of my Activity.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.abhayjatindoshi.encryptorcode.qrlogin.Scanner">
<SurfaceView
android:layout_width="match_parent"
android:layout_height="400dip"
android:layout_weight="70"
android:id="#+id/camera_view"
android:layout_alignParentLeft="true"
/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/scanImage"
android:layout_below="#+id/camera_view"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:src="#drawable/ic_image_black_48dp"
android:layout_alignRight="#+id/camera_view"
android:layout_alignEnd="#+id/camera_view" />
</RelativeLayout>
Understand the design to create your layout.
Put your surface view also in frame layout i forget to add it.
I am trying to make a layout with an image, then a scroll view text, and then again an image, but when I am running this content_main.xml then my application is not opening.
<?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:id="#+id/context_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.example.hsports.myapplication.MainActivity"
tools:showIn="#layout/activity_main">
the following is the code for image which I want to place in the above section of app
<ImageView
android:id="#+id/imageView"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_above="#+id/scrollView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="10dp"
android:src="#drawable/shikher"
/>
The following is the code to add a scroll view text in the middle of the app
<ScrollView
android:id="#+id/scrollView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="116dp"
android:layout_above="#+id/photo"
android:layout_below="#+id/imageView"
>
This the Plain text which is inserted between the scroll view tags and under which we can scroll the huge text.
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/usedtext" />
</ScrollView>
the following is the code to add the image at last and view it below the scroll view text
android:layout_above="#+id/scrollView"
android:layout_alignParentBottom="true"
android:src="#drawable/shikher"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
/>
</RelativeLayout>
After when this code is executed and I try to open the application then its showing that the application cannot be opened.
I think the problem is that you are trying to get a reference to ScrollView from ImageView:
android:layout_above="#+id/scrollView"
before scrollview is created in the layout. Remove the line and try again.
I have a layout XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/containor"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff6da"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.vats.vatishs.cardviewprogrammatically.MainActivity">
<FrameLayout
android:id="#+id/card"
android:layout_width="220dp"
android:layout_marginTop="10dp"
android:layout_height="wrap_content">
<TextView
android:background="#color/white"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:text="Hello I am a text View." />
</FrameLayout>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="210dp"
android:background="#drawable/select_delete_chim" />
</RelativeLayout>
which works fine. The result will be a image view above frame layout but when I use the following xml in which i have replace frame layout with CardView:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/containor"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff6da"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.vats.vatishs.cardviewprogrammatically.MainActivity">
<android.support.v7.widget.CardView
android:id="#+id/card"
android:layout_width="220dp"
android:layout_marginTop="10dp"
android:layout_height="wrap_content">
<TextView
android:background="#color/white"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:text="Hello I am a text View." />
</android.support.v7.widget.CardView>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="210dp"
android:background="#drawable/select_delete_chim" />
</RelativeLayout>
then the result will be: ImageView at the back of CardView.
Any Solution for this?
One thing more, It will work fine on pre-lollipop devices but on lollipop it doesn't work.
Output with CardView on pre-lollipop devices (required output)
Output with CardView on lollipop devices
CardView has default elevation which is greater than ImageView, So we have to add greater elevation in ImageView than CardView's elevation.
e.g. imageView.setElevation(10); and cardView.setElevation(5);
Now, its working fine on all devices.
You can use this attr
app:cardElevation="0dp"
to the CardView in XMl .
Like this
<android.support.v7.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
app:cardElevation="0dp"
app:cardUseCompatPadding="true">
This will make other view in Front of CardView
Do it like this,
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/myCardView"
android:layout_width="match_parent"
android:layout_height="match_parent"
card_view:cardBackgroundColor="#android:color/transparent"
card_view:cardElevation="0dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_edit"
android:background="#android:color/transparent"/>
</android.support.v7.widget.CardView>
It will bring the ImageView to front.
Write this line in your java code after initializing the ImageView.
imageView.bringToFront();
Adding just app:cardElevation="-1dp" to the CardView in your xml will fix the issue.
Write Image view Above cardview,hopefully it solve the problem
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/containor"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff6da"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.vats.vatishs.cardviewprogrammatically.MainActivity">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="210dp"
android:src="#drawable/select_delete_chim" />
<android.support.v7.widget.CardView
android:id="#+id/card"
android:layout_width="220dp"
android:layout_marginTop="10dp"
android:layout_height="wrap_content">
<TextView
android:background="#color/white"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:text="Hello I am a text View." />
</android.support.v7.widget.CardView>
</RelativeLayout>
Here is a complete answer to your question
You can wrap CardView with a FrameLayout and put an ImageView outside of the CardView's parent like that
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout ...>
<FrameLayout...>
<android.support.v7.widget.CardView...>
<Content/>
</android.support.v7.widget.CardView>
</FrameLayout>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
... />
</RelativeLayout>
alternatively here is the Java code that supports Android versions below and after lollipop
image.bringToFront();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
image.setElevation(10);
}
this will bring the image to the front
an easy way is to use something like this:
<RelativeLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content">
....
</android.support.v7.widget.CardView>
</LinearLayout>
<ImageView
android:layout_width="wrap_parent"
android:layout_height="wrap_content">
note that this is because the elevation of cardview. its default elevation is bigger than ImageView.
this way you can use a LinearLayout to put CardView inside it. then you can use RelativeLayout to make ImageView on the top of CardView!
https://stacklearn.ir/course/kotlin-basic-programming-course
CardView -> app:cardElevation ="4dp"
ImageView -> android:translationZ="6dp"
Send the CardView to the back.
i try to get this picture at the top of my app as an "titlebar".
So i want that it repeats itself. But i am a little bit stuck here.
I have this code in my background.xml:
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#drawable/topbar"
android:tileMode="repeat" />
and this is my layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="0dp"
android:layout_marginBottom="0dp"
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp"
android:layout_marginStart="0dp"
android:layout_marginTop="0dp"
android:background="#drawable/bgColor"
android:gravity="top"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
....
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:src="#drawable/topbar" />
</RelativeLayout>
Does anyone know how to do so?
Thanks.
You need to set the width of ImageView as match_parent. That will do it. But you can also do that with 9patch images. For more information check out this link.
EDIT
Here is the layout with repeated background image. You need to set the src of ImageView as background.xml. And the reason there is a space between the image parent layout is that parent has padding. If you remove it, it will be fine as well.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="0dp"
android:layout_marginBottom="0dp"
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp"
android:layout_marginStart="0dp"
android:layout_marginTop="0dp"
android:gravity="top"
tools:context=".MainActivity" >
<ImageView
android:id="#+id/imageView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:src="#drawable/background" />
</RelativeLayout>