Change Card View Elevation Color - java

How can I change the color of the card view elevation?
Or make it softer
It gets dark when I increase the app:cardElevation value

Add these 2 properties to your CardView
android:outlineAmbientShadowColor="#color/yourShadowColor"
android:outlineSpotShadowColor="#color/yourShadowColor"

here you can change color first you can make a drawable file and paste this code in it. then in your layout inside cardview, you can assign as background file.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#android:id/background">
<shape>
<corners android:bottomLeftRadius="8dp"
android:topLeftRadius="5dp"
android:bottomRightRadius="8dp"
android:topRightRadius="5dp"/>
<solid android:color="#ddd"/>
</shape>
</item>

Related

Using resources to set the size of an item in Layer-list

I have defined a custom background to RadioButton in my project so that I can change it in the java code. The background is a Layer-list that includes 2 items: XML drawable and image.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#color/colorAccent"/>
<size android:width="20dp" android:height="20dp"/>
<corners android:radius="3dp"/>
</shape>
</item>
<item
android:id="#+id/correct"
android:drawable="#drawable/correct_image"
android:height="15dp"
android:width="15dp"
android:gravity="center"/>
</layer-list>
The design will break because the height and width are not supported prior to API 23. Is there any way that I can set the size of the image programatically using dimension value?
I don't have any prior coding experience or knowledge, so any explanation provided with possible solution will be very helpful.
Thank you

How do I add user configurable buttons with tap states?

I'm trying to make a button in Android that has a border but with a different background tint when it is pressed, and the ability to change the color of the button background. I know adding a border is assigning it a shape and that tap states are through a selector with different items, but the problem is that the button background color is meant to be user configured.
Without tap states, I am able to allow the user to change the background color of my shape by just doing:
GradientDrawable bgShape = (GradientDrawable) btn.getBackground();
//color value is obtained from shared preferences
if (sharedPref.contains(pref_color)) {
String color = sharedPref.getString(pref_color, "");
bgShape.setColor(Color.parseColor(color));
}
But I can't do the first line if my button is going to be assigned a selector. I don't know how I would get the reference to the drawable shape.
For reference, my button border shape is:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke android:width="5px" android:color="#ffffff" />
</shape>
The selector would look like this:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/button_press" android:state_pressed="true"/>
<item android:drawable="#drawable/button_border" />
</selector>
Is there a way to accomplish this?
you can define an id for drawable layer and then change their properties in runtime
LayerDrawable drawSettings = (LayerDrawable)
getResources().getDrawable(R.drawable.sample);
GradientDrawable backSettings = (GradientDrawable)
drawSettings.findDrawableByLayerId(R.id.backtemp);
and after change color reset it to button background
view.setBackground(drawable);
this is a sample drawable sample.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/backtemp">
<shape
android:shape="oval" >
<solid android:color="#color/default_color"/>
</shape>
</item>
<item
android:drawable="#drawable/w_pref">
</item>
</layer-list>

How to draw multi colored line using xml in android

Is it possible to draw a line like shown in the fig. using android xml ? I need it for setting a background for view pager indicator
If you need hard edges like that, I'd suggest using a 9-patch: http://developer.android.com/tools/help/draw9patch.html
If you're ok with soft edges, you can use a gradient (which I got from using this online tool):
<gradient
android:angle="0"
android:centerX="50%"
android:centerColor="#000000"
android:startColor="#FF0000"
android:endColor="#FF0000"
android:type="linear"
/>
create a new drawable like this
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:bottom="-100dp"
android:top="-100dp">
<shape android:shape="rectangle">
<stroke
android:width="100dp"
android:color="#FF0000" />
<solid android:color="#ffffff" />
</shape>
</item>
</layer-list>
and then add this as background.

Add multiple Strings and ImageViews to one View

What I want to do is have something like the picture below.
The whole rounded corner rectangle needs to be clickable. Then Record: ### and ### need to be some sort of TextView or String. The green check mark needs to be an ImageView.
I am having trouble even knowing where to start with this. I know there is a way to achieve this because the app Unblock Me has something sort of like how I want it. Below is a screenshot of their app.
Any help on ideas of how to achieve this?
It is kind of easy.
First that rounded rectangle can be a linear layout ok.
In its background attribute you pass a layer list which will contain two items having colors black and white with radius of lets say 5dp
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- "background shadow" -->
<item>
<shape android:shape="rectangle" >
<solid android:color="#DDDDDD" />
<corners android:radius="15dp" />
</shape>
</item>
<!-- background color -->
<item
android:bottom="5px"
android:left="5px"
android:right="5px"
android:top="5px">
<shape android:shape="rectangle" >
<solid android:color="#FFFFFF" />
<corners android:radius="8dp" />
</shape>
</item>
</layer-list>
put this in a xml file and save it in a drawable folder and put it in the background attribute of your linear layout.
Now in your layout, with orientation horizontal, put three child elements
two textviews and one imageview with weights 35, 35, 30 and set image to the imageview that green tick mark sign.
Voila!

How to use a drawable shape in Android through Java?

If I have a drawable shape:
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke android:width="2dp" android:color="#FFFFFFFF" />
<gradient android:startColor="#DD000000" android:endColor="#DD2ECCFA"
android:angle="225"/>
<corners android:bottomRightRadius="7dp" android:bottomLeftRadius="7dp"
android:topLeftRadius="7dp" android:topRightRadius="7dp"/>
</shape>
How would I go about using this as the background for a textview? I tried something like below, but it didn't work.
TextView jObjTv = new TextView(getActivity());
jObjTv.setBackgroundDrawable(findViewById(R.drawable.sample_box));
The code you posted doesn't work because a drawable isn't a view and isn't a part of the layout.
You need to call textView.setBackgroundResource(R.drawable.sample_box) if you want to use the resource. If you want to use the Drawable, you need to create the Drawable using context.getResources().getDrawable(R.drawable.sample_box).

Categories

Resources