I have a ImageView as background of appWidgets layout. So I want to change both background resource (nine patch image) and background transparency. I tried as below:
rv.setInt(R.id.widget_background, "setBackgroundColor", 0);
rv.setInt(R.id.widget_background, "setBackgroundResource", nBackgroundId); // set background_id
rv.setInt(R.id.widget_background, "setAlpha", nTransparency); // set transparent
But nothing is happened, could anybody tell me the solution to solve my problem?
Which version is your android sdk? It requires Froyo or greater
Related
My App run correctly on Android 4.0, while when run on 8.0, the Button's background color won't changed, I tried :
btnThree.setBackground(getResources().getColor(color.btnThreeColor));
btnThree.setBackgroundColor(btnThreeColor);
btnThree.setBackgroundResource(btnThreeColor);
they all didn't work. How to set background color?
If you want to do something only for ">=26", I think you can add this condition.
if(android.os.Build.VERSION.SDK_INT>=26){
//set button's background
}
Also you can check more details at here.
I found solution:
btnThree.setBackgroundColor(getResources().getColor(btnThreeColor));
you should input in getColor():
R.color.btnThreeColor, and the Android Studio would transfer it into:btnThreeColor.
I have weird problem with ImageView on android 5(api 21).
When I set ImageView tint color in xml then I can’t change it on code!!!
I tried several ways to change the colorFilter of ImageView programmatically but it doesn’t work.
I have this problem only on api 21.
Any idea?
After searching lots of hours i found out this is a bug in api 21 that when you setting initialization tint color to ImageView or ImageButton you can't change it in code later.
The best solution is moving initial tint color to code to prevent this issue.
Use this
imageView.setColorFilter(ContextCompat.getColor(context, R.color.COLOR_YOUR_COLOR), android.graphics.PorterDuff.Mode.MULTIPLY);
As of my previous question
here
I successfully managed to do this by transparent image. Now i want to change background color of images dynamically in remoteviews(PS I want to make widget like that and color of images in given link changes dynamically).
I tried following code:
ColorFilter cf = new PorterDuffColorFilter(-15032095, Mode.MULTIPLY);
Drawable d= context.getResources().getDrawable(R.drawable.panel1);
d.mutate();
d.setColorFilter(cf);
rv.setBitmap(R.id.rl_noti_main, "setColorFilter", drawableToBitmap(d));
but it didn't help. How can i achieve this?
I have color codes in all formats integer, HEX or string whatever it will be needed.
Please note that i want to do this only for given shapes in this link and for remoteview.
Thanks in Advance :)
For someone else looking for it
remoteView.setInt(R.id.container, "setBackgroundColor", backgroundColor);
You cannot dynamically update widgets.
You can use the setBitmap() method to change the bitmap of a view inside a RemoteView. If the view is at the background, it should change the background. And then update the widget to make the changes to be applied.
If you are using RemoteViews in Notifications, you should update your notification after this. If you are using it in a Widget, you should use appWidgetManager.updateAppWidget(appWidgetId, views); function.
I have a seekbar and i am trying to change the thumb color. I found two photos that describes what i actually want to do.
I have this seekbar (default) :
and i am trying to change it's color thumb like this.. :
(I know how to change background progress color but not the thumb color)..
seekBar1calling.getProgressDrawable().setColorFilter(Color.WHITE, Mode.SRC_IN);
Is there any way to achieve this using java code? Thanks in advance!!
I just used an online style generator which gave me what i needed
http://android-holo-colors.com/
Just choose the widget and color you want and you are ready! ;)
Just use:
mySeekbar.getThumb().setColorFilter(myColor, PorterDuff.Mode.MULTIPLY);
Edit: method getThumb is only available since API 16+ (Jelly Bean).
The other answers are old and didn't work for me, this did.
seekbar.setThumbTintList(ContextCompat.getColorStateList(this, R.color.disabled));
Here is a quick and easy solution which worked for me as expected. Just implement this code after you initialized the seekbar to avoid NPE's.
You have to define the color itself under res/values/colors.xml
int blue = ContextCompat.getColor(context, R.color.seekbar_blue);
filterSeekBar.getThumb().setColorFilter(blue, PorterDuff.Mode.SRC_ATOP);
filterSeekBar.getProgressDrawable().setColorFilter(blue, PorterDuff.Mode.SRC_ATOP);
That's all!
Note: The color of the Progress-Background is not as dark as the Button / Thumb itself.
If you want to know more about the PorfuerDuff part check this answer on Stackoverflow:
Phasmal & Lee's answer to PorfuerDuff modes
How do I set the background color of the home screen app widget programmatically?
Remember widget is remoteView. You have very limited resource to updates UI of widget and not directly.
You can try :
remoteViews.setInt(viewId, "setBackgroundColor", Color.BLACK);
I never used it but i guess it may be the way.
I guess you need to change color dynamically.
You can change color of ImageView image in "RemoteViews" by doing this:
remoteviews.setInt(viewid, "setColorFilter", color);
Widget.setBackgroundColor(Color.LTGRAY);
Find the following code..
code:
Button button;
//to change background color..
button.setBackgroundColor(Color.Yellow);
use
Yourwidget.setBackgroundColor(Color.RED);
hope help