How to Set Background Color of a View

How to set background color of a View

You made your button transparent. The first byte is the alpha.

Try v.setBackgroundColor(0xFF00FF00);

Android studio set background color of view

Try:

view.setBackgroundColor(ContextCompat.getColor(MainActivity.this, R.color.blue));

How to change background/color of the drawable set as background of a view?


GradientDrawable background = (GradientDrawable) textView.getBackground();
background.setColor(getResources().getColor(R.color.some_color));

I have considered that you have applied your shape drawable to textView as background.

So you need to use your view in which you have set your background to get background.

How to set background color to a dynamically created view?


   Drawable drawable = eventView.getBackground();

if (drawable instanceof GradientDrawable) {
GradientDrawable gd = (GradientDrawable) drawable.getCurrent();
gd.setColor(color);

}
if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
eventView.setBackgroundDrawable(drawable);
}
else {

eventView.setBackground(drawable);
}

How to : Set background color of Text with Swift UI

I'm not sure I understand what is your desired final effect but if you just want the green background on the label to go edge to edge just move the background modifier under the frame:

StrokeText(text: "App Name", width: 1.0, color: .black)
.foregroundColor(.white)
.font(.system(size: 30, weight: .bold))
.frame(maxWidth: .infinity, alignment: .center)
.background(Color.init(hue: 0.2722, saturation: 0.89, brightness: 0.29, opacity: 1.0))

How to change body background color with if in SwiftUI

You just need to embed your VStack inside a ZStack, where the back layer is a color that changes every time isOnLight changes.

Like this:

struct Example: View {

@State private var isOnLight: Bool = false
@State private var color: Color = .white

var body: some View {
ZStack {
color
.ignoresSafeArea()

VStack {
Toggle(isOn: $isOnLight) {
Text("Switch")
.font(.title)
.foregroundColor(.gray)
}
}
.padding()
}
.onChange(of: isOnLight) { value in
if value {
color = .yellow
} else {
color = .white
}
}
}
}


Related Topics



Leave a reply



Submit