Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set foreground color in FrameLayout in Android programmatically

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);
like image 888
Bhavesh Hirpara Avatar asked Jan 23 '13 07:01

Bhavesh Hirpara


People also ask

How to set layout background color programmatically Android?

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.

What is framelayout in 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

How to put different textview in frame layout in Android Studio?

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.

What are the important attributes specific to framelayout?

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.


2 Answers

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)

like image 143
kartikag01 Avatar answered Oct 07 '22 07:10

kartikag01


You can create a Drawable from color:

final int color = 0xFFFF0000;
final Drawable drawable = new ColorDrawable(color);
like image 32
Vladimir Mironov Avatar answered Oct 07 '22 07:10

Vladimir Mironov