I used the following solution:
float opacity = 0.3f; //opacity = 0: fully transparent, opacity = 1: no transparancy
int backgroundColor = 0x000000; //background color (here black)
remoteView.setInt( R.id.loMain, "setBackgroundColor", (int)(opacity * 0xFF) << 24 | backgroundColor);
loMain is a main layout of my widget
If the main layout of the widget uses shape background (e.g. android:background="@drawable/shape_transparent") which has rounded corner then here is a more complex solution:
float transparency = 0.5f; //0...1
long colorFilter = 0x01000000L * (long)(255f * transparency);
try {
final Method[] declaredMethods = Class.forName("android.widget.RemoteViews").getDeclaredMethods();
final int len = declaredMethods.length;
if (len > 0) {
for (int m=0; m<len; m++ ) {
final Method method = declaredMethods[m];
if (method.getName().equals("setDrawableParameters")) {
method.setAccessible(true);
method.invoke(remoteView, R.id.loMain, true, -1, (int)colorFilter, PorterDuff.Mode.DST_OUT, -1);
break;
}
}
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
}
You can play with variables alfa and colorFilter to meet your needs.