Issue
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>
Solution
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.
Answered By - Gabriele Mariotti
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.