Shadow of a rounded background android - android

I have a background drawable which has just two corners of it with a radius.
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:topLeftRadius="6dp" android:bottomLeftRadius="6dp"></corners>
</shape>
I apply it on a textview. I also attach an outlineprovider to it .
<android.support.v7.widget.AppCompatTextView
android:id="#+id/textView71"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginStart="16dp"
android:layout_marginTop="64dp"
android:background="#drawable/left_rounded_border_6dp"
android:backgroundTint="#color/colorAccent"
android:padding="#dimen/unit_medium"
android:elevation="10dp"
android:outlineProvider="bounds"
android:text="Challenge"
android:textAppearance="#style/TextAppearance.Heading5"
android:textColor="#color/colorTextWhite"
app:layout_constraintBottom_toTopOf="#+id/company_logo_iv"
app:layout_constraintEnd_toStartOf="#+id/textView73"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
This the shadow I see.
On close inspection you'd see that the shadow is not applied exactly at the curves. Rather it is applied on a rectangular area.
I am not sure why would that be .

Elevation creates shadows based on the view's outline provider. This defaults to the bounds of the background drawable - which is perfectly rectangular.
You can create and set your own outline provider with rounded corners - see https://developer.android.com/training/material/shadows-clipping#Shadows

create a drawable shape file
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:endColor="#color/grey_light_background"
android:centerColor="#color/grey_light_background"
android:startColor="#color/grey_light_background"
android:angle="90" />
<corners
android:radius="#dimen/dimen_15dp"
/>
</shape>
apply to text view background,
android:background="#drawable/background_grey_rounded_corner_15dp"

Related

MPAndroidChart - MarkerView doesn't show shadow

My app shows a line chart and a custom MarkerView. One problem with the MarkerView is that the shadow doesn't work. The same code works in activity's layout. But, if I copy it to the MarkerView's layout, the shadow doesn't show when I run the app.
To show the shadow, I set elevation to 5dp and a background colour on the View. Then, for its parent view, I set padding to 5dp, set clipToBounds to false. like below:
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#id/line_chart_view"
app:layout_constraintBottom_toBottomOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clipToPadding="false"
android:padding="10dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="some text"
android:elevation="10dp"
android:background="#FFFFFFFF"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
So for my markerview this elevation wont work. Reason for it is because under the hood everything will be drawn on a canvas. So what we did was basically creating a shadow in our MarkerView XML Layout:
markerview.xml (drawable):
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="oval">
<gradient
android:startColor="#FF808080"
android:endColor="#00000000"
android:gradientRadius="11dp"
android:type="radial" />
</shape>
</item>
<item android:top="1dp" android:left="1dp" android:right="1dp" android:bottom="6dp">
<shape android:shape="oval">
<solid android:color="#android:color/holo_blue_bright" />
<stroke
android:width="3dp"
android:color="#color/white" />
<size
android:width="20dp"
android:height="20dp" />
</shape>
</item>
</layer-list>
As you can see the top, left, right, bottom is there to manipulate the shadow. I know you are working with a textview, but maybe this can give you insight on how it actually works.
Hopefully this helps others who are struggling as well with this issue.

How to change a button shape style at runtime?

I am developing an app in java, android studio, where the user can choose the style color of the app.
With most components just use the .setBackground(colorUser);
The problem is in my buttons.
My buttons are all rounded, I created a shape for that.
My shape is in other file...
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="10dp"
android:shape="rectangle">
<solid android:color="#color/colorJetway" />
<corners
android:bottomLeftRadius="80dp"
android:bottomRightRadius="80dp"
android:topLeftRadius="80dp"
android:topRightRadius="80dp" />
</shape>
<Button
android:id="#+id/btnAdc"
android:layout_width="84dp"
android:layout_height="84dp"
android:layout_marginTop="12dp"
android:layout_marginEnd="205dp"
android:background="#drawable/btns_border"
android:onClick="btnAdicionar_click"
android:text="+"
android:textColor="#FFFFFF"
android:textSize="40dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#+id/txtQuantidade" />
That way if at runtime I make mybutton.setBackground(colorUser) my button loses its style ... it will have no rounded edges.
How can I do it?
It is not exactly what you are looking for.
However using the MaterialButton component it is very simple to have rounded buttons.
Just use app:cornerRadius to define the corner radius and app:backgroundTint to change the background color.
<com.google.android.material.button.MaterialButton
app:backgroundTint="#color/myselector"
app:cornerRadius="xxdp"
.../>
You can programmatically change these values using:
button.setCornerRadius(...);
button.setBackgroundTintList(..);
if your colors is limited you can create some drawables with the colors you want.
for example :
blackButton.xml :
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="10dp"
android:shape="rectangle">
<solid android:color="#color/black" />
<corners
android:bottomLeftRadius="80dp"
android:bottomRightRadius="80dp"
android:topLeftRadius="80dp"
android:topRightRadius="80dp" />
whiteButton.xml :
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="10dp"
android:shape="rectangle">
<solid android:color="#color/white" />
<corners
android:bottomLeftRadius="80dp"
android:bottomRightRadius="80dp"
android:topLeftRadius="80dp"
android:topRightRadius="80dp" />
and when you want change your button's background just use button.setBackground(getResources().getDrawable(R.drawable.blackButton)) or button.setBackground(getResources().getDrawable(R.drawable.whiteButton))

Background image in TextView showing up transparent

I have a circle png that I put as a background image in a TextView. However, the image shows up transparent even when I tried setting the alpha level to "1". Is there something wrong I did in my XML code?
<TextView
android:id="#+id/timeline_text_details_img"
android:layout_width="55dp"
android:layout_height="55dp"
android:layout_gravity="center"
android:layout_marginTop="25dp"
android:background="#drawable/circle"
android:gravity="center"
android:textColor="#fff"
android:textSize="14dp"
android:alpha="1"
android:textStyle="bold" />
The circle png I want needs to be thick white like how it originally is
but instead it looks like this when I run it
You can simply create a shape circle.xml and place it in drawable.
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<stroke
android:width="2dp"
android:color="#android:color/white"
/>
<solid
android:color="#000000"/>
</shape>
You can change the color scheme as you like.

Adding border around ripple effect button in android?

I have used the following code to add ripple effect to my button, but the border around it has disappeared and the button has merged with the back ground. I want to add border around it to distinguish it.
This is the code of my button:
<Button
android:id="#+id/email_sign_in_button"
style="?android:textAppearanceLarge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackground"
android:textColor="#ffffff"
android:text="#string/action_sign_in" />
This is the drawable for the button:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners
android:bottomLeftRadius="1dp"
android:bottomRightRadius="1dp"
android:radius="0.1dp"
android:topLeftRadius="1dp"
android:topRightRadius="1dp" />
<solid android:color="#android:color/transparent" />
<stroke
android:width="1dp"
android:color="#E8E6E7" />
</shape>
You can actually have both by using background and foreground properties, like so:
android:background="#drawable/the_name_of_your_button_file"
android:foreground="?android:attr/selectableItemBackground"
android:background="?attr/selectableItemBackground"
is causing the button to take on the theme, and blend into the background as if transparent. You are not pointing to the drawable file for the button, which has the stroke element, which gives the border.
If you want a border, you must use the stroke element in your drawable by pointing to it with:
android:background="#drawable/the_name_of_your_button_file"

Android ImageButton src overwriting background

I'm trying to create a rounded image button using the code below which works when you don't declare a src image. However when you do, it just places the image in but without the rounding effect on it.
style.xml
<ImageButton
android:id="#+id/imageButton2"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_below="#+id/imageButton1"
android:layout_centerHorizontal="true"
android:layout_marginTop="41dp"
android:src="#drawable/ellyfish"
android:background="#drawable/roundcorner"
/>
roundcorner.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#33DDFF" />
<corners android:radius="50dp" />
</shape>
Make your src image round, or make it transparent so you can see the rounded background behind it.

Categories

Resources