change FloatingActionButton and SubActionButton background color - android

Is it possible to change the background color of android Floating Action Button and Sub ActionButton when using min APi 16?
I have tried a lot of different situations like:
actionButton.setBackgroundTintList(getResources().getColorStateList(R.color.myColor));
But all say that the min API level is 16 and required is 21.
Is there a way to do it in API level 16 programmatically in the Java file?

To Change the floating action bar color, just write this in your xml
app:backgroundTint="#color/desired_color"
and in java is
actionButton.setBackgroundTintList(ColorStateList.valueOf(ContextCompat.getColor(context, your color)));

I think you can use
android:backgroundTint="#color/white"

Related

How to set color of a TextView's Drawable in XML - for Android API 15?

My app is using minSdkVersion 15 and I would like to set the color of a TextView's drawable using XML.
There is a solution to a similar question, but it requires API 23.
I'm currently changing the drawable color programmatically, but I would rather do it in XML - if it's possible to do what with API 15?

AppCompatButton on Android 5: app:backgroundTint works but supportBackgroundTintList does NOT :(

I need to reuse XML layout and change button color programmatically.
In Android 5 applying app:backgroundTint in XML changes button color but I need to do it programmatically and I do it in Recyclerview:
holder.button.supportBackgroundTintList = ContextCompat.getColorStateList(context, backgroundColorRes)
This has no effect.
The setSupportBackgroundTintList() method is annotated with #RestrictTo({Scope.LIBRARY_GROUP}), which means you are not supposed to call it directly. Instead, you should use ViewCompat.setBackgroundTintList().
Try changing your code to this instead:
val colorStateList = ContextCompat.getColorStateList(context, backgroundColorRes)
ViewCompat.setBackgroundTintList(holder.button, colorStateList)
If you look at the source code for ViewCompat.setBackgroundTintList(), you'll see that it does different things for API 21+ (Android 5 and higher) than earlier versions. Chances are good that the "support" background tint is only applied on earlier versions of Android, and ViewCompat will make it so that you don't have to think about that.

android - How to change the font of the Switch in xml?

I can change the textview's fonts with using android:fontFamily="#font/test2"
but this is not working on switches, so i created a custom style includes font but still no action. Also I tried with android:textAppearance="#font/test2"
still no action. I can change the font just programmatically. How can I change with using xml attributes?
android:fontFamily atribute in xml is not available for API levels below 16.
For API below 16 use app namespace.

Change foreground color of a layout programmatically

I want to change the foreground color of a linearlayout.
This is my code:layout.setForeground(new ColorDrawable(getResources().getColor(R.color.svbackclr)));
But this call require minimum api level 23.So how to do the same on pre 23 api.
As #Gaurav suggested FrameLayout you can use the forground color by programatically in the following way
int color = R.color.black_trans_60;
frm.setForeground(new ColorDrawable(ContextCompat.getColor(mContext, color)));
I suggest to use FrameLayout
In this you can use setForeground()
On any API level
This is a documentation bug . setForeground() existed on FrameLayout from API Level 1; it is only on View as of API Level 23.
So it best to extend FrameLayout

Implementing (switch\toggle button) depending on API level

My application will run on api level 9 devices or higher. Now i have a toggle_button on one of the layouts. I want to change this toggle button to switcher if the device is apilevel 14 or higher. How can i implement this? Sorry for my english.
If you are creating the content/pages in xml, simply copy your xml with same name but to the layout-v14 folder. Then change ToggleButton to SwitchButton, and Android will take care of rest :)
If you are coding it in jave, use:
if(Build.VERSION.SDK_INT > Build.VERSION_CODES.ICE_CREAM_SANDWICH){
//Add Switch
}else{
//add toggle
}
And when you'll want to use both (when using defining in xml) with the same java code use:
CompoundButton bt_toggle= (CompoundButton) findViewById(R.id.some_button);
bt_toggle.setChecked(false);
I have found a solution! link
It just adds a support library. And you can use switchers from api-level7!

Categories

Resources