button dynamic background color with rounded edges - android

I managed to have rounded edge for my button.
I also managed to have a dynamic background color (taken from a webservice).
The problem is when doing this :
btn.setBackgroundResource(R.drawable.radio_button_selector);
btn.setBackgroundColor(Color.parseColor(currentQuestion.backgroundColorButton));
One overrides the other, therefore I cannot have rounded edges AND dynamic background color.
I cannot use a dynamic color in the selector (as it's a static XML).
I cannot set the rounded edges programmatically (the method doesn't exists as far as I know).
How do I do ?

Use this
String backgroundColor= "#fc0000"; // set dynamic color here
btn.setBackgroundColor(Color.parseColor(backgroundColor));
another Examples:
GradientDrawable drawable = new GradientDrawable();
drawable.setShape(GradientDrawable.RECTANGLE);
drawable.setStroke(5, Color.MAGENTA);
drawable.setColor(Color.BLACK);
btnBlackColor.setBackgroundDrawable(drawable);

use this :-
final int color = Color.parseColor(homeCatPOJOS.get(position).getColor());
then implement it in background color :-
btn.setBackgroundResource(R.drawable.radio_button_selector);
btn.setBackgroundColor(color));
I think you are getting color code in String, first convert into int then implement it.
And for round edges make XML file for it and implement it statically.

Related

Fill the transparent part of the drawable image a specific color

Currently, I have this back arrow that I'm going to use in my toolbar
What I want is to fill the middle part which a specific color which is the middle part is a transparent part.
currently what i have is this :
Drawable backArrow = getResources().getDrawable(R.drawable.ic_back);
backArrow.setColorFilter(getResources().getColor(R.color.aub_red), PorterDuff.Mode.SRC_ATOP);
getSupportActionBar().setHomeAsUpIndicator(backArrow);
it only changes the color of the white color part into the red color which is wrong. what I want to fill the transparent part a color not the non - transparent part.
Used this
Drawable backArrow = getResources().getDrawable(R.drawable.ic_back);
backArrow.setColorFilter(getResources().getColor(R.color.colorAccent), PorterDuff.Mode.LIGHTEN);
getSupportActionBar().setHomeAsUpIndicator(backArrow);
for more information click hear
you can use :
DrawableCompat.setTint(yourdrawable,getResources().getColor(R.color.primary));
in java code, or in xml tag :
android:backgroundTint="#color/primary"

How to get rid of shadow that appears on my button? Android

I have created a button with a Gradient Drawable.
If you look at this button, it has those Extra Grey lines as pointed out by my red arrows. Those don't appear if I create a shape using XML but when i use
Gradient Drawable code seen below it shows these lines. How do I get rid of them??
GradientDrawable gd = new GradientDrawable();
gd.setColor(Color.parseColor("#FFFFFFFF"));
gd.setCornerRadius(20);
gd.setStroke(30, Color.parseColor("#0077CC"));
Button.setBackground(gd);
These become more apparent if i increase the setCorner Radius
Just add style to your button.
style="?android:attr/borderlessButtonStyle"

how to make rounded corners button on runtime even if background is already set in xml

I understand how to make Button has rounded corners using GradientDrawable. The question I have now is if I set my background already in xml and retrieve it by
int[] attrsArray = new int[] {
android.R.attr.background
};
TypedArray ta = context.obtainStyledAttributes(attrs, attrsArray);
Drawable background = ta.getDrawable(0);
ta.recycle();
so I have a Drawable type of background now. next thing I want to do is call setBackgroundDrawable() and pass the drawable background, but I won't be able to set rounded corners with Drawable type. I can't just cast it to GradientDrawable either. Is there a way to achieve this? this way makes things a lot easier because I'll be able to give Button any background and have the rounded corner.
Thanks
Not sure if your use case is same as mine but I had to once do something similar and I had an invisible Button with a RoundedImageView behind it. The RoundedImageView was below:
https://github.com/vinc3m1/RoundedImageView
Using that class you can set the radius of the image.

Android ListView ScrollView change edge glow color from blue

Is it possible to change the ScrollView and ListView edge glow color from blue to orange.?
If you want to change color of glow, when you pull listview (the blue glow effect)
use
int glowDrawableId = getResources().getIdentifier("overscroll_glow", "drawable", "android");
int edgeDrawableId = getResources().getIdentifier("overscroll_edge", "drawable", "android");
Drawable androidGlow = ContextCompat.getDrawable(this, glowDrawableId);
Drawable androidEdge = ContextCompat.getDrawable(this, edgeDrawableId);
androidGlow.setColorFilter(getResources().getColor(R.color.white_20), PorterDuff.Mode.SRC_IN);
androidEdge.setColorFilter(getResources().getColor(R.color.white_20), PorterDuff.Mode.SRC_IN);
mode SRC_IN will show only your color. Place this code in Application class if color will be same or before initialisation of each individual listview (if you want different glow of different listviews).
If you asking about color change of fadingedge ( a shadow a top and a bottom of listview)
change color of colorCacheHint inside listview
use the Android Holo Colors Generator by Jérôme Van Der Linden
http://android-holo-colors.com/
The Android Holo Colors Generator allows you to easily create Android
components such as editext or spinner with your own colours for your
Android application. It will generate all necessary nine patch assets
plus associated XML drawables and styles which you can copy straight
into your project.
Select the views you want to generate colors for and add pngs & the theme from the XML it generates
Yes. Create drawable you like and set it for the listview:
android:overScrollHeader="#drawable/header"
android:overScrollFooter="#drawable/footer"

How to set background color of a View

I'm trying to set the background color of a View (in this case a Button).
I use this code:
// set the background to green
v.setBackgroundColor(0x0000FF00 );
v.invalidate();
It causes the Button to disappear from the screen. What am I doing wrong, and what is the correct way to change the background color on any View?
Thanks.
You made your button transparent. The first byte is the alpha.
Try v.setBackgroundColor(0xFF00FF00);
When you call setBackgoundColor it overwrites/removes any existing background resource, including any borders, corners, padding, etc.
What you want to do is change the color of the existing background resource...
View v;
v.getBackground().setColorFilter(Color.parseColor("#00ff00"), PorterDuff.Mode.DARKEN);
Experiment with PorterDuff.Mode.* for different effects.
Several choices to do this...
Set background to green:
v.setBackgroundColor(0x00FF00);
Set background to green with Alpha:
v.setBackgroundColor(0xFF00FF00);
Set background to green with Color.GREEN constant:
v.setBackgroundColor(Color.GREEN);
Set background to green defining in Colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="myGreen">#00FF00</color>
<color name="myGreenWithAlpha">#FF00FF00</color>
</resources>
and using:
v.setBackgroundResource(R.color.myGreen);
and:
v.setBackgroundResource(R.color.myGreenWithAlpha);
or the longer winded:
v.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.myGreen));
and:
v.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.myGreenWithAlpha));
You can set the hex-color to any resource with:
View.setBackgroundColor(Color.parseColor("#e7eecc"));
// set the background to green
v.setBackgroundColor(0x0000FF00 );
v.invalidate();
The code does not set the button to green. Instead, it makes the button totally invisible.
Explanation: the hex value of the color is wrong. With an Alpha value of zero, the color will be invisible.
The correct hex value is 0xFF00FF00 for full opacity green. Any Alpha value between 00 and FF would cause transparency.
For setting the first color to be seen on screen, you can also do it in the relevant layout.xml (better design) by adding this property to the relevant View:
android:background="#FF00FF00"
and what is the correct way to change
the background color on any View?
On any View? What you have is correct, though you should drop the invalidate() call.
However, some Views already have backgrounds. A Button, for example, already has a background: the face of the button itself. This background is a StateListDrawable, which you can find in android-2.1/data/res/drawable/btn_default.xml in your Android SDK installation. That, in turn, refers to a bunch of nine-patch bitmap images, available in multiple densities. You would need to clone and modify all of that to accomplish your green goals.
In short, you will be better served finding another UI pattern rather than attempting to change the background of a Button.
try to add:
setBackgroundColor(Color.parseColor("#FF0000"));
I use at API min 16 , target 23
Button WeekDoneButton = (Button) viewWeeklyTimetable.findViewById(R.id.week_done_button);
WeekDoneButton.setBackgroundColor(ContextCompat.getColor(getActivity(), R.color.colorAccent));
mButton.setBackgroundColor(getResources().getColor(R.color.myColor));
You can simple use :
view.setBackgroundColor(Color.parseColor("#FFFFFF"));
You can simple use :
view.setBackgroundColor(Color.rgb(0, 198, 255));
This question talks about changing the background color of a view. In one of the answers, the person explains how to change the background color during runtime. Obviously you are going to look into how to modify other objects on the screen, but this should give you a good start by at least allowing you to modify the background color of the view on button click.
Stating with Android 6 use ContextCompact
view.setBackgroundColor( ContextCompat.getColor(this, R.color.your_color));
This works for me
v.getBackground().setTint(Color.parseColor("#212121"));
That way only changes the color of the background without change the background itself. This is usefull for example if you have a background with rounded corners.
In kotlin you could do it like this:
val backgroundColor = R.color.whatever_color_you_like
view.setBackgroundColor(getColorCompat(backgroundColor))
Where getColorCompat() is an extension function:
/**
* Extension method to provide simpler access to {#link ContextCompat#getColor(int)}.
*/
fun Context.getColorCompat(color: Int) = ContextCompat.getColor(this, color)
view.setBackgroundColor(R.color.primaryColor);
Adds color to previous color value, so i have a different color.
What works for me is :
view.setBackgroundResource(R.color.primaryColor);
Let suppose we have a primary color in values=>colors.xml as:
<resources>
<color name="primary">#FDD835</color>
</resources>
so if we want to use our custom color into setBackgroundColor(#ColorInt int Color) then we just need an annotation #SuppressLint("ResourceAsColor") with constructor/method which will be used as:
#SuppressLint("ResourceAsColor")
public _LinearLayout(Context context) {
super(context);
// Formatting our layout : )
super.setBackgroundColor(R.color.primary);
....
}
You must pass an int in the argument.
First Example:
view.setBackgroundColor(-500136)
Second Example:
int colorId = R.color.green;
view.setBackgroundResource(colorId);
This should work fine: v.setBackgroundColor(0xFF00FF00);
I tried all the above ways. But I havent achieve what i need. Here is my try.
If you are using hexcode for color and want to set the color as background of image, then this is the kotlin code.
val bitmap = Bitmap.createBitmap(30, 30, Bitmap.Config.ARGB_8888)
val canvas = Canvas(bitmap)
val colorCode = "#ffffff"
canvas.drawColor(Color.parseColor(colorCode))
mImageViewLogo.setImageBitmap(bitmap)
When calling setBackgroundColor on a view you need to set the alpha value to a non-zero value (e.g. 0xFF), otherwise the color will not show up.
TextView tv = (TextView)findViewById(R.id.myTextview);
int rgb = 0xF05922; // Orange
tv.setBackgroundColor(0xFF000000|rgb); // Use bitwise OR to add alpha to RGB value

Categories

Resources