how to make the setBackgroundColor work on all android versions in a button?

Viewed 43

i would like to setBackgroundColor on a button in a ClickListener. i succeeded to do it but i noticed it works only with certains android versions. i tested it with a phone with android 4.4 it works but with a android 9 version, it does not works. here is my code and the build.gradle file content:

 Button usd=findViewById(R.id.button_divide);
    Button cdf=findViewById(R.id.button_multiply);
    usd.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            SharedPreferences.Editor editor=sharedPref.edit();
            editor.putString("currency","USD");
            editor.apply();
            usd.setBackgroundColor(Color.parseColor("#515de1"));
            usd.setTextColor(Color.WHITE);
            cdf.setBackgroundColor(Color.WHITE);
            cdf.setTextColor(Color.parseColor("#515de1"));
        }

    });

this code extends and AppCompatActivity and my build.gradle file :

android {
compileSdkVersion 30
buildToolsVersion "30.0.3"

defaultConfig {
    applicationId "com.example.e_mpatanfc"
    minSdkVersion 16
    targetSdkVersion 30
    versionCode 1
    versionName "1.0"

    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
    }
}
compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
  }
}

here are my themes:

<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.EMpatanfc" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
    <!-- Primary brand color. -->
    <item name="colorPrimary">@color/purple_500</item>
    <item name="colorPrimaryVariant">@color/purple_700</item>
    <item name="colorOnPrimary">@color/white</item>
    <!-- Secondary brand color. -->
    <item name="colorSecondary">@color/teal_200</item>
    <item name="colorSecondaryVariant">@color/teal_700</item>
    <item name="colorOnSecondary">@color/black</item>
    <!-- Status bar color. -->
    <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
    <!-- Customize your theme here. -->
</style>

and the night version

<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.EMpatanfc" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
    <!-- Primary brand color. -->
    <item name="colorPrimary">@color/purple_200</item>
    <item name="colorPrimaryVariant">@color/purple_700</item>
    <item name="colorOnPrimary">@color/black</item>
    <!-- Secondary brand color. -->
    <item name="colorSecondary">@color/teal_200</item>
    <item name="colorSecondaryVariant">@color/teal_200</item>
    <item name="colorOnSecondary">@color/black</item>
    <!-- Status bar color. -->
    <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
    <!-- Customize your theme here. -->
</style>

and finally the values/style.xml content :

   <?xml version="1.0" encoding="utf-8"?>
<resources>
   <style name="BottomNavigationView">
     <item name="android:textSize">24sp</item>

  </style>
   <style name="BottomNavigationView.Active">
    <item name="android:textSize">24sp</item>
    <item name="android:textColor">#515de1</item>
 </style>
</resources>
2 Answers

You can usse the method setBackgroundTintList:

usd.setBackgroundTintList(ContextCompat.getColorStateList(context, R.color.xxx));
cdf.setBackgroundTintList(ContextCompat.getColorStateList(context, R.color.xxx));

Using a Theme.MaterialComponents.* theme, your Button is replaced at runtime with a MaterialButton. This method works for minsdk>14.

Use setBackgroundResource instead of setBackgroundColor like this

setBackgroundResource(R.color.red);

and define the colour in the colors.xml file

Related