I want to set foreground color in FrameLayout programmatically (not in XML attribute). I have the color codes in RGB
How can I convert color to drawable:
frm.setForeground(Drawable);
Layout background color can be easily editable through MainActivity.java programming file. So developer can modify whole layout color on single button click event and the complete layout change at once. So here is the complete step by step tutorial for Set layout background color programmatically android.
Android Frame Layout, Frame Layout is designed to block out an area on the screen to display a single item. Generally, FrameLayout should be used to hold a single child view, because
Step 1: Create a new project in Android Studio and name it FrameTesting. (Select File -> New -> New Project. Fill the forms and click “Finish” button) Step 2: Now Open res -> layout -> activity_main. xml and add the following code. Here we are putting different TextView in Frame Layout.
Following are the important attributes specific to FrameLayout − This is the ID which uniquely identifies the layout. This defines the drawable to draw over the content and possible values may be a color value, in the form of "#rgb", "#argb", "#rrggbb", or "#aarrggbb". Defines the gravity to apply to the foreground drawable.
Create Drawable
from Color Using ContextCompat
int color = R.color.black_trans_60;
frm.setForeground(new ColorDrawable(ContextCompat.getColor(mContext, color)));
Use ContextCompat
instead of direct color as in new API ColorDrawable
takes ColorDrawable(@ColorInt int color)
You can create a Drawable
from color:
final int color = 0xFFFF0000;
final Drawable drawable = new ColorDrawable(color);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With