CardView does not show elevation at run time - java

Here is the xml code :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/activity_main"
android:orientation="vertical"
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.example.himanshu.staticlogin.MainActivity"
android:weightSum="1">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
card_view:cardBackgroundColor="#color/cardview_dark_background"
card_view:cardElevation="10dp"
android:clickable="true"
/>
</LinearLayout>
It shows elevation in the preview but when I run the file it does not. What am I doing wrong?

set for cardview:
card_view:cardUseCompatPadding="true"

Use card_view:cardUseCompatPadding="true" in your xml to show elevation on pre Lollipop androids.

Related

My background is under navbar.How to set the background just below the navbar?

My background hides under the navbar.
How can I put the background start showing just below the navbar?
code:
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/backgroundnew"
android:scaleType="centerCrop" />
In your activity_main.xml, Have you closed the toolbar correctly?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:fitsSystemWindows="true"
app:popupTheme="#style/AppTheme.PopupOverlay">
</android.support.v7.widget.Toolbar>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:showIn="#layout/app_bar_main">
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
</LinearLayout>
If you didn't close your toolbar, the view is overlapped.

Android Studio: Layout hierarchy problems

I have a horizontalScrollView containing 1 image that is used as the background. This image can be scrolled left to right. I would like to place other imageviews on to the background image but not have them scroll with it. At the moment if I place another imageview on top of this, when I scroll the image comes as well.
The scroll view at far left:
The scroll view at far right:
XML Code:
<?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/activity_rear_gunner"
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"
android:focusable="true"
tools:context="com.example.mr_br.ibcc_bomber_command.rear_gunner">
<HorizontalScrollView
android:id="#+id/horizontalScrollView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true"
android:layout_alignParentTop="true"
>
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent" app:srcCompat="#drawable/background_scroll_2"
android:id="#+id/scrollView" android:layout_centerVertical="true"
android:scaleType="centerCrop"
/>
</HorizontalScrollView>
<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/item_frame"
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"
android:focusable="true"
tools:context="com.example.mr_br.ibcc_bomber_command.rear_gunner">
</RelativeLayout>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content" app:srcCompat="#drawable/placeholder_bomber"
android:id="#+id/enemy" android:layout_centerVertical="true"
/>
</RelativeLayout>
Very new to using layouts in android. Hope I made my problem clear if not I do apologize and will try to clarify if asked. I know this could have something to do with what layouts I am using and the child and parents of them layouts but like I said new to this.
The HorizontalSCrollView can have only one child, but that child could be another layout, so you can add as many ImageView you want.
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
>
<ImageView
android:layout_width="3000dp"
android:layout_height="match_parent"
android:background="#a30"
/>
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:background="#aa0"
/>
</RelativeLayout>
</HorizontalScrollView>

ScrollView not scrolling far enough - java Android Studio

I am working on a app where the user actions add LinearLayout's to the pre-existing LinearLayout #+id/linLayout. Sometimes these additional layouts will go off the bottom of the screen so I have put the main LinearLayout within a ScrollView. When my app starts to go off the bottom of the screen it is able to scroll a little bit but not far enough to see everything. At a certain point it is not possible to continue because a Button is off screen and cannot be pressed. Can anybody tell me what I'm doing wrong?
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="#dimen/activity_horizontal_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:showIn="#layout/activity_main"
tools:context=".MainActivity">
<LinearLayout 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:orientation="vertical" >
<Button android:id="#+id/bGenerateExample"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/generate_example" />
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/linLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
</LinearLayout>
</RelativeLayout>
What about using weight_Sum?
<LinearLayout 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:orientation="vertical"
android_weight_sum=5 >
<Button android:id="#+id/bGenerateExample"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="#string/generate_example" />
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="4">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/linLayout"
android:layout_width="match_parent"
android:layout_height="marth_parent"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
</LinearLayout>
now button covers %20 percent of screen and scrollView covers %80.You can change it easily with changing layout_weight values of scrollView, Button.

Android Toolbar not showing

currently I am using Android's new toolbar (in appcompat). It works fine until I try the following method: toolbar.bringToFront(). When I call this, the toolbar disappears. My layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:orientation="vertical"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context=".MainActivity">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="56dp"
android:background="#color/colorPrimary"
app:theme="#style/ToolbarTheme"
app:popupTheme="#style/ToolbarThemePopup"
/>
<com.asdev.sechat.SlidingTabLayout
android:id="#+id/sliding_tabs"
android:background="#color/colorPrimary"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<android.support.v4.view.ViewPager
android:id="#+id/view_pager"
android:layout_width="match_parent"
android:layout_height="fill_parent"
/>
Now the reason I need it in front is that the SlidingTabLayout has a shadow which covers the toolbar, and I don't want that. Anyone know how I can bring the toolbar to the top and still have it visible? Thanks.
from the docs:
Change the view's z order in the tree, so it's on top of other sibling views. This ordering change may affect layout, if the parent container uses an order-dependent layout scheme (e.g., LinearLayout). Prior to KITKAT this method should be followed by calls to requestLayout() and invalidate() on the view's parent to force the parent to redraw with the new child ordering.
https://developer.android.com/reference/android/view/View.html#bringToFront()
It makes sense that this would mess up your layout. you may have to look into a relative layout or find a different way to get rid of the shadow. or if you're min sdk 21 you could probably set the elevation on the toolbar manually.
relativelayout example:
<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"
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"
tools:context=".MainActivity">
<com.asdev.sechat.SlidingTabLayout
android:id="#+id/sliding_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/toolbar"
android:background="#color/colorPrimary" />
<android.support.v4.view.ViewPager
android:id="#+id/view_pager"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_below="#id/sliding_tabs" />
<android.support.v7.widget.Toolbar
android:id="#id/toolbar"
android:layout_width="match_parent"
android:align_parentTop="true"
android:layout_height="56dp"
android:background="#color/colorPrimary"
app:popupTheme="#style/ToolbarThemePopup"
app:theme="#style/ToolbarTheme" />
</RelativeLayout>
Use bringToFront in your java file for toolbar will solve your problem.
Edit
This is working solution in my app
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
app:popupTheme="#style/Theme.AppCompat.Light.DarkActionBar"
app:theme="#style/Toolbar" />
<com.asdev.sechat.SlidingTabLayout
android:id="#+id/sliding_tabs"
android:layout_width="match_parent"
android:layout_height="#dimen/tab_height"
android:background="#color/primaryColor"
android:minHeight="#dimen/tab_height"/>
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>

Background Image that fits the android studio project

When i applied an image to emulator on android studio, it gives an image without covering borders. Screen shot of this problem is as under, but what i get is to completely fill the emulator with my image.
XML code:
<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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:background="#drawable/background_image"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context=".MainActivity">
</RelativeLayout>
Your XML looks like :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp"
android:paddingBottom="16dp"" >
So the thing you've to do is remove the paddings from your XML, and if you have put the image correctly it will fit on your screen :)
EDIT1
<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:background="#drawable/background_image"
tools:context=".MainActivity">
</RelativeLayout>
EDIT2
<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:background="#drawable/background_image"
android:layout_centerInParent="true"
android:scaleType="centerCrop"
tools:context=".MainActivity">
</RelativeLayout>
EDIT3
Use this image instead : http://i.imgur.com/oqES1TU.jpg

Categories