last button in layout doesn't have elevation - android

I'm working with Android Lollipop.
I have this layout:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/buttonlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="#dimen/spacing_normal"
android:orientation="vertical" >
<Button
android:id="#+id/facebook_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="#style/FacebookButton" />
<com.myCompany.views.widgets.CustomTextView
android:id="#+id/or_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="#dimen/spacing_normal"
android:layout_marginBottom="#dimen/spacing_normal"
android:text="#string/app.widget.connexion.or"
app:font_name="Roboto-Medium.ttf" />
<Button
android:id="#+id/myCompany_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="#style/myCompanyButton" />
the two style applied to the buttons are simply ripple effect with different background colors. The thing is the first button has an elevation with a shadow when we click it and the second hasn't unless I add an other widget below him. I suppose it's due to the ripple effect and because I had to change the background attribute, but I don't know how to make the effect back.
I tried to use translationZ and elevation attribute, but it didn't work (or I failed to use them properly).
any ideas ?

Related

Android remove border hover from FloatActionbutton

current situation
So as you can see on the picture there is a shadow on the border of my button. Is there a way to remove the shadow so that that the color of the button corresponds with the color of my action bar. right know it looks ugly because you can see the shadow. This is my code:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout 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="wrap_content"
android:orientation="horizontal"
>
<TableRow
android:gravity="end">
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/logInButton"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:borderWidth="0dp"
app:backgroundTint="#color/faviconColor"
app:srcCompat="#drawable/ic_person_black_24dp" />
</TableRow>
</TableLayout>
Try setting android:elevation="0dp" to your floatingaction button.
Nevertheless, why even use a FloatingActionButton, and not just a normal button? FloatingActionButtons are afaik not made to be used like this, and if you need e.g. a round ripple effect, there are more elegant ways to do this.
EDIT:
If this does not work (as sometimes) use app:elevation="0dp" instead

Override the include's background attribute to change background color

Android Studio 1.5
I have this layout called chat_profile_header that will be used in many layouts. I have set the background to a indigo color. The problem is when I include this header in other layouts, I want to be able to change the background color based on that layout's theme. Everything in the header will remain the same.
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="#color/profile_header_indigo_500">
<de.hdodenhof.circleimageview.CircleImageView
android:id="#+id/profile_image"
android:layout_width="40dp"
android:layout_height="40dp"
android:src="#drawable/photorace"
android:layout_centerVertical="true"/>
<TextView
android:id="#+id/tvProfileName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/profile_image"
android:layout_toEndOf="#id/profile_image"
android:layout_centerVertical="true"
android:layout_marginLeft="8dp"
android:fontFamily="sans-serif"
android:textColor="#android:color/white"/>
</RelativeLayout>
Here I am using the above header included in this layout. However, based on this theme I want to change the header to another background color grey. However, I don't think I can override the background color attribute.
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white">
<include
layout="#layout/chat_profile_header"
android:background="#color/child_header_lighter_grey"/>
<android.support.v7.widget.RecyclerView
android:id="#+id/rvParentList"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
Is there way to do this without having to create different header layouts based on background. Seems a lot of duplication.
this does not seem to be possible. You can set background from java.
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white">
<include
android:id="#+id/layout1"
layout="#layout/chat_profile_header"/>
<include
android:id="#+id/layout2"
layout="#layout/chat_profile_header"/>
<android.support.v7.widget.RecyclerView
android:id="#+id/rvParentList"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
this java code
Linealayout layout1 = (Linealayout)findViewbyId(R.id.layout1);
Linealayout layout2 = (Linealayout)findViewbyId(R.id.layout2);
layout1.setBackgroundColor(R.color.color_1);
layout2.setBackgroundColor(R.color.color_2);
You can not set background color to <include> tag.
As you set the background color of include tag then it will be messed up with the include layout's background color.
So from my point of view you should make your include layout
transparent and then try to set background color of it.
In this case it won't get mixed up with the include layout's color.
Hope it will help you to get some idea regarding it.
You can always change your background color with layout theme you specified.
Just set the
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?attr/colorPrimary">
</LinearLayout>
Then set the colorPrimary under your theme style files. Warning: by setting the color primary, you are changing the color of the action bar as well.
<style name="MyCustomTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">#android:color/blue</item>
</style>
Using this method, your background will always be the same as the color you specified under the theme style.
Just in case, you doesn't know how to set the theme for you activity, you can always do in your AndroidManifest.xml
<activity
android:name=".Activity"
android:theme="#style/MyCustomTheme"
/>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:id="#+id/rl_profile_header"
android:layout_height="60dp"
android:background="#color/profile_header_indigo_500">
<de.hdodenhof.circleimageview.CircleImageView
android:id="#+id/profile_image"
android:layout_width="40dp"
android:layout_height="40dp"
android:src="#drawable/photorace"
android:layout_centerVertical="true"/>
<TextView
android:id="#+id/tvProfileName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/profile_image"
android:layout_toEndOf="#id/profile_image"
android:layout_centerVertical="true"
android:layout_marginLeft="8dp"
android:fontFamily="sans-serif"
android:textColor="#android:color/white"/>
assign id to your RelativeLayout
android:id="#+id/rl_profile_header"
then Initialize it where you want to use
like RelativeLayout rl=(RelativeLayout)findViewById(R.id.rl_profile_header)
then setColor as you need using rl.setBackgroundColor(R.color.red)
It seems not possible.
From https://developer.android.com/training/improving-layouts/reusing-layouts
"You can also override all the layout parameters (any android:layout_* attributes) of the included layout's root view by specifying them in the tag. "

Cast shadow on top of LinearLayout using android:elevation

I have this LinearLayout that is going to be placed on the bottom of an activity layout. I want this LinearLayout to have a 4dp elevation, just like the top toolbar should have, however, since android:elevation places the shadow below the ui component and this specific component (linearLayout) is going to be on the bottom of the screen, I won't see any elevation at all..
This is my LinearLayout code, and an image of it with the default elevation implemented:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="3"
android:background="#android:color/transparent"
android:orientation="horizontal"
android:elevation="4dp"
android:layout_alignParentBottom="true">
<ImageButton
android:id="#+id/playButton"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:scaleType="center"
android:clickable="true"
android:background="#drawable/bottom_toolbar_menu_selector"
android:src="#drawable/ic_play"
style="?android:attr/borderlessButtonStyle" />
<ImageButton
android:id="#+id/stopButton"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:scaleType="center"
android:clickable="true"
android:background="#drawable/bottom_toolbar_menu_selector"
android:src="#drawable/ic_stop"
style="?android:attr/borderlessButtonStyle" />
<ImageButton
android:id="#+id/bookmarkButton"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:scaleType="center"
android:clickable="true"
android:background="#drawable/bottom_toolbar_menu_selector"
android:src="#drawable/ic_bookmark"
style="?android:attr/borderlessButtonStyle" />
</LinearLayout>
Is there a way, using elevation to place a shadow on top of the ui component?
Thanks in advance!
You can't theoretically do it with android:elevation, in the sense that you can't choose the direction where the shadow is going to be cast.
There are two solutions.
1. Drawables
You could, for instance, put an ImageView right above your layout and set android:src="#drawable/shadow". This should be a vertical GradientDrawable defined in XML, as explained here.
2. Workaround
While most of the shadow is actually below the view, a subtle shadow is also above. A workaround might be using a very high value for elevation, like 40dp: the bottom part is going to be hidden due to your layout, while the top is going to be expanded and look like a common shadow.
In either case, you do not have control over the elevation value in dp, in the sense that you can't be sure your shadow is equivalent to the one cast by android:elevation=4dp.
use like as;
<LinearLayout
android:layout_width="match_parent"
android:layout_height="205dp"
android:background="#color/white"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginTop="-5dp"
android:background="#color/white"
android:elevation="3dp"
android:paddingTop="5dp">
// CHILD VIEWS
</RelativeLayout>
</LinearLayout>
Add this code above of view in which you want top elevation or add android:layout_marginTop="-4dp" and add below of above view
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="0.01dp"
android:layout_below="#+id/scrollbar"
app:cardBackgroundColor="#android:color/transparent"
app:cardCornerRadius="0dp"
app:cardElevation="#dimen/dp_4" />
Try this i am unable to set background through xml then i tried the programmatic way to solve that and i work perfectly refer this if facing problem on show shadow in Linear Layout
lprofile.setBackgroundResource(R.drawable.background_with_shadow);
lprofile is my ref of Linear layout and background_with_shadow is xml for applying shadow i hope i will for ur requirmement...thank u..

Textview background - what's setting it?

I have the following layout :
<?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" >
<fragment
android:id="#+id/top_banner"
android:name="com.apps4care.mycarerecord.fragments.patientBannerFragment"
android:layout_width="match_parent"
android:layout_height="64dp"
android:layout_alignParentTop="true"
tools:layout="#layout/patient_banner" >
</fragment>
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/top_banner" >
<LinearLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
</ScrollView>
<fragment
android:id="#+id/footer"
android:name="com.apps4care.mycarerecord.fragments.footerFragment"
android:layout_width="match_parent"
android:layout_height="32dp"
android:layout_alignParentBottom="true"
tools:layout="#layout/footer">
</fragment>
</RelativeLayout>
I then add a number of fragments into the that have the following layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/documentFooter"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="#+id/tvsectionHeading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_marginLeft="16dp"
android:text="Section Heading Text Goes Here"
android:textStyle="bold"
android:textAppearance="?android:attr/textAppearanceLarge" />
<WebView
android:id="#+id/wvsectionHTML"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/tvsectionHeading"
/>
</RelativeLayout>
This gives a screen that looks like
My question is in three parts :
1) Where did the background color come from on the TextView. It is not set in the XML layout file or the code.
2) Why is the background color on the TextView not consistent. The bars lower down the screen are lighter, especially in the horizontal centre of the screen.
3) The Scrollview content is meant to stop above the "footer" fragment but it does not, the bottom of the content on the Scrollview is hidden behind the "footer" fragment.
The background for the TextView comes from it's (default) style which is transparent.
what you are seeing is the default background of activity in the Holo light theme. you can change it by overriding it in your theme, or setting a background color to the relative layout in you main layout file
The TextView colour is consistent, but it the colour of your background page, the default colour, which has a glow towards the bottom (it is the page's default formatting). This happened to my application too. Try changing the background of your page and see if the issue is still there, it should go away.

Change background color dynamically and use a border

I have the following template
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#drawable/borders" >
<RelativeLayout
android:id="#+id/layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp">
<ImageView
android:id="#+id/category_starred_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/rating_not_important"
android:contentDescription="#string/app_name" />
<TextView
android:id="#+id/category_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/category_starred_icon"
android:textIsSelectable="false"
android:textSize="18sp"
android:paddingLeft="5dp"
android:paddingTop="5dp"
android:paddingRight="5dp"
android:textColor="#android:color/white"
android:hint="#string/category" />
</RelativeLayout>
</LinearLayout>
i get a warning This RelativeLayout layout or its LinearLayout parent is possibly useless; transfer the background
attribute to the other view, i want solve it, but i have the follow problem i have a border in the LinearLayout is like a drop shadow but when i change the background color in my code
mLayout.setBackgroundColor(Color.paseColor(mColorString));
i lost the border, how i can solve the warning witout lose the border, the main problem is the background colors are dynamically
Have a look at this: http://developer.android.com/guide/topics/resources/drawable-resource.html#LayerList
This is an drawable that organizes other drawables in a layer list (one on top of the other). With it, you can use other drawables, like shape, to define the borders and the background the way you want.
I can't make a complete answer now, but if you search for this, I'm sure you can do it. When I have more time, I may come back to help you, if you haven't found out how to solve this yet.

Categories

Resources