I am creating a custom class CustomButton that represents a Button with a custom style. CustomButton's style is programatically retrieved from styles.xml and set. A CustomButton is programatically added to the app's layout. However, when I run an app, even though the background color is correct (light blue), the color of the text is incorrect (should be red or #ff0000):
Below is my code:
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:background="#ffffff"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@ id/tile_list"
android:orientation="vertical">
</LinearLayout>
</LinearLayout>
styles.xml:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<!-- Removes the TitleBar from the display -->
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="CustomButtonStyle">
<item name="android:background">#b3e0ff</item>
<item name="android:textColor">#ff0000</item>
</style>
</resources>
MainActivity.java:
package com.example.stackoverflowquestion;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.LinearLayout;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Retrieve the Equation Lists from secondary storage and display them.
CustomButton tile1 = new CustomButton(this);
LinearLayout tileListLayout = findViewById(R.id.tile_list);
tileListLayout.addView(tile1);
}
}
CustomButton.java:
package com.example.stackoverflowquestion;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import androidx.appcompat.widget.AppCompatButton;
public class CustomButton extends AppCompatButton {
private Context context;
public CustomButton(Context context) {
super(context);
this.context = context;
// Set the style of the Tile.
this.setStyle();
}
public CustomButton(Context context, AttributeSet attrs) {
super(context, attrs);
// Set the style of the Tile.
this.setStyle();
}
// Set the styling of the Tile, retrieved from the defined style in styles.xml.
@SuppressWarnings("ResourceType")
private void setStyle() {
int[] attributesToRetrieve = {android.R.attr.background, android.R.attr.textColor};
TypedArray attributeValues = this.context.obtainStyledAttributes(R.style.CustomButtonStyle,
attributesToRetrieve);
// Set the background.
this.setBackgroundColor(attributeValues.getColor(0, Color.WHITE));
// Set the text color.
this.setTextColor(attributeValues.getColor(1, Color.BLACK));
this.setText("Custom Button");
}
}
build.gradle (app):
apply plugin: 'com.android.application'
android {
compileSdkVersion 31
buildToolsVersion "29.0.2"
defaultConfig {
applicationId "com.example.stackoverflowquestion"
minSdkVersion 26
targetSdkVersion 31
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.0.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}
CodePudding user response:

