Android - Change drawable color in runtime - java

I''m using a drawable for the cells in my table layout in my Android application. I wanna change the cell colors on click event. Is it possible to change the color of the drawable in run time. my drawable is,
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape= "rectangle" >
<solid android:color="#FFFFFF"/>
<stroke android:width="1dp" android:color="#483D8B"/>
</shape>
And I wanna change the white color "#FFFFFF" with the click event. can java edits the color in the XML drawable file. Is it possible.
I just edited the background color and it dispersers the borders of the drawable.
String StatusColor=GetColor(Retailer_x.getStatus());
Stts.setBackgroundColor(Color.parseColor(StatusColor));
need help. Thank you!!!!

ShapeDrawable sd1 = new ShapeDrawable(new RectShape());
sd1.getPaint().setColor(CommonUtilities.color);
sd1.getPaint().setStyle(Style.STROKE);
sd1.getPaint().setStrokeWidth(CommonUtilities.stroke);
sd1.setPadding(15, 10, 15, 10);
sd1.getPaint().setPathEffect(
new CornerPathEffect(CommonUtilities.corner));
ln_back.setBackgroundDrawable(sd1);

Use selector of the Table Cell background to change state of the table cell when user click it.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- selected -->
<item android:drawable="#drawable/red_drawable" android:state_selected="true"/>
<!-- focused -->
<item android:drawable="#drawable/gray_drawable"/>
<!-- default -->
</selector>

Related

Change Card View Elevation Color

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>

How to apply transparency only on background color of buttons

I'm a beginner on Android Studio and I need your help to apply a specific transparency on my layout.
I applied a background and border color to my layout by using this drawable:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#android:color/black" />
<corners android:radius="3dp" />
<stroke android:width="5px" android:color="#ffa500" />
</shape>
Now I want to apply dynamically a transparency to this button (by using a seek bar) but only on the background color I don't want to modify my border color.
I tried to use setAlpha function but it also change my border color transparency.
How can I do this?
Thanks.
In your shape change the <solid android:color="#android:color/black" /> to <solid android:color="#00FFFFFF" />
This will make your shape transparent.
And in your xml <Button .... android:background="#drawable/your_shape" />
You need to use the alpha portion of the color your specify in item.
For example - #aarrggbb -- aa part is the alpha.
If you want to change stroke / solid color separately, I recommend using GradientDrawable to setStroke() and `setColor()'.
For reference -
https://developer.android.com/reference/android/graphics/drawable/GradientDrawable#setColor(int)
https://developer.android.com/reference/android/graphics/drawable/GradientDrawable.html#setStroke(int,%20android.content.res.ColorStateList,%20float,%20float)

How to add a border to a particular side of a view

This is how I go about adding a border to a EditText. How can I go about adding a border only on one side of a EditText, and define the color and width of the border?
EditText editText = new EditText(this);
editText.setText("Find");
editText.setWidth(555);
GradientDrawable border = new GradientDrawable();
border.setColor(0xFFFFFFFF); // white background
border.setStroke(1, 0xFF000000); // black border with full
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
editText.setBackgroundDrawable(border);
} else {
editText.setBackground(border);
}
Vielen dank im voraus.
To get border on one side, you can create your own drawable like this:
<?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="#FF0000" />
</shape>
</item>
<item android:right="5dp">
<shape android:shape="rectangle">
<solid android:color="#FFFF" />
</shape>
</item>
</layer-list>
And set this drawable as background to your EditText.
If you want to add border only one side of EditText then you should use View tag in your layout file to draw a simple line and place it near your EditText and use background property to set color of line.
To draw horizontal line use this:
<View
android:layout_width="match_parent"
android:background="#color/colorPrimary"
android:layout_height="2dp" />

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>

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!

Categories

Resources