I am trying to implement my own custom progressbar using 9 patch images. I am looking at the Sound ProgressBar example.
http://www.jagsaund.com/blog/2011/11/6/customizing-your-progress-bar-part-one.html
I have two problems that I have been unable to figure out why its happening:
No matter what I do my progressbar is not an instance of
layerdrawable, I am not sure why this is happening so I am unable to
get reference to the layer I want and change it grammatically.
The 9 patch image starts off stretched to the entire width of the container (relativelayout) not sure how to tell it to not
automatically scale so I can control its width grammatically.
Here are the following code snipets:
Drawable progressbar.xml
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#android:id/background"
android:drawable="#drawable/progressbar_bg" >
</item>
<item android:id="#+id/progress">
<bitmap
android:src="#drawable/progressbar"
android:tileMode="repeat" />
</item>
</layer-list>
Style.xml
<style name="Widget">
</style>
<style name="Widget.ProgressBar">
<item name="android:indeterminateOnly">true</item>
<item name="android:indeterminateBehavior">repeat</item>
<item name="android:indeterminateDuration">3500</item>
<item name="android:minWidth">48dip</item>
<item name="android:maxWidth">48dip</item>
<item name="android:minHeight">48dip</item>
<item name="android:maxHeight">48dip</item>
</style>
<style name="Widget.ProgressBar.RegularProgressBar">
<item name="android:indeterminateOnly">false</item>
<item name="android:progressDrawable">#drawable/progressbar</item>
<item name="android:indeterminateDrawable">#android:drawable/progress_indeterminate_horizontal</item>
<item name="android:minHeight">1dip</item>
<item name="android:maxHeight">10dip</item>
</style>
Custom View inside XML layout file:
<com.custom.ahmad.views.NfcProgressBar
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/timeOutProgress"
android:layout_above="#+id/tvStart"
style="#style/Widget.ProgressBar.RegularProgressBar" />
Finally The custom Progressbar class:
public class NfcProgressBar extends ProgressBar {
private String text = "";
private int textColor = Color.BLACK;
private float textSize = 15;
public NfcProgressBar(Context context) {
super(context);
}
public NfcProgressBar(Context context, AttributeSet attrs) {
super(context, attrs);
}
public NfcProgressBar(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
protected synchronized void onDraw(Canvas canvas) {
Drawable progressDrawable = this.getProgressDrawable();
Log.i("Testing", "progressDrawable : " + ((progressDrawable != null) ? progressDrawable : "null"));
Log.i("Testing", "progressDrawable instanceof LayerDrawable : " + ((progressDrawable instanceof LayerDrawable)? "true" : "false"));
if (progressDrawable != null && progressDrawable instanceof LayerDrawable) {
// Get the layer and do some programming work.
}
}
Related
I'm searching for a specific value adopted in default component in android.
For example the colorPrimary is adopted to background color and the colorOnPrimary is adopted to text color in default button.
I want to know where the attrs are set to the button.
The button class is like below.
#RemoteView
public class Button extends TextView {
public Button(Context context) {
this(context, null);
}
public Button(Context context, AttributeSet attrs) {
this(context, attrs, com.android.internal.R.attr.buttonStyle);
}
public Button(Context context, AttributeSet attrs, int defStyleAttr) {
this(context, attrs, defStyleAttr, 0);
}
public Button(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
#Override
public CharSequence getAccessibilityClassName() {
return Button.class.getName();
}
#Override
public PointerIcon onResolvePointerIcon(MotionEvent event, int pointerIndex) {
if (getPointerIcon() == null && isClickable() && isEnabled()) {
return PointerIcon.getSystemIcon(getContext(), PointerIcon.TYPE_HAND);
}
return super.onResolvePointerIcon(event, pointerIndex);
}
}
So I searched R.attr.buttonStyle in android platform_frameworks_base repository, and found this codes, so searched again about this.
<!-- Button styles -->
<item name="buttonStyle">#style/Widget.Button</item> <!-- themes.xml -->
<!-- styles.xml -->
<style name="Widget.Button">
<item name="background">#drawable/btn_default</item>
<item name="focusable">true</item>
<item name="clickable">true</item>
<item name="textAppearance">?attr/textAppearanceSmallInverse</item>
<item name="textColor">#color/primary_text_light</item>
<item name="gravity">center_vertical|center_horizontal</item>
</style>
btn_default.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_window_focused="false" android:state_enabled="true"
android:drawable="#drawable/btn_default_normal" />
<item android:state_window_focused="false" android:state_enabled="false"
android:drawable="#drawable/btn_default_normal_disable" />
<item android:state_pressed="true"
android:drawable="#drawable/btn_default_pressed" />
<item android:state_focused="true" android:state_enabled="true"
android:drawable="#drawable/btn_default_selected" />
<item android:state_enabled="true"
android:drawable="#drawable/btn_default_normal" />
<item android:state_focused="true"
android:drawable="#drawable/btn_default_normal_disable_focused" />
<item
android:drawable="#drawable/btn_default_normal_disable" />
</selector>
About #drawable/btn_default_normal, it was a .9.png.
What I expected was that a background is colorPrimary, and textColor is colorOnPrimary.
But I can't find anything about this.
How can I find this?
If the android:tint attribute is set for a ViewGroup, should it apply to all descendant View's, or does it need to be applied to each individually?
The LinearLayout below (ButtonBar$LabeledButton) contains an ImageView and TextView that each specify their own color state list (CSL).
I'd like to set android:tint once in the ViewGroup so when it is disabled, all its members become disabled and change their tint accordingly (and also without having to override the setEnabled).
resources/layout/buttonbar_labeledbutton_addnew.axml
<?xml version="1.0" encoding="utf-8"?>
<!--LinearLayout-->
<view class="ButtonBar$LabeledButton"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ButtonBar_LabeledButton_AddNew"
style="#style/ButtonBar_LabeledButton">
<ImageView
android:id="#+id/ButtonBar_LabeledButton_Image"
style="#style/ButtonBar_LabeledButton_Image"
android:src="#drawable/v__ic_add_circle_outline_black_24dp"/>
<TextView
android:id="#+id/ButtonBar_LabeledButton_Label"
style="#style/ButtonBar_LabeledButton_Label"
android:text="Add New"/>
</view>
<!--/LinearLayout-->
resources/values/styles.xml
<!--LinearLayout-->
<style name="ButtonBar_LabeledButton">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:orientation">horizontal</item>
<??-item name="android:tint">#color/buttonbar_csl</item-??>
</style>
<!--ImageView-->
<style name="ButtonBar_LabeledButton_Image">
<item name="android:layout_width">40dp</item>
<item name="android:layout_height">40dp</item>
<item name="android:tint">#color/buttonbar_csl</item>
</style>
<!--TextView-->
<style name="ButtonBar_LabeledButton_Label">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textSize">14sp</item>
<item name="android:textColor">#color/buttonbar_csl</item>
<!--item name="android:tint">#color/buttonbar_csl</item-->
</style>
ButtonBar.LabeledButton
[Register("ButtonBar$LabeledButton")]
public class LabeledButton : LinearLayout
{
public LabeledButton(Context context, IAttributeSet attributes) : base(context, attributes) { }
public override bool Enabled
{
set
{
var image = FindViewById<ImageView>(Resource.Id.ButtonBar_LabeledButton_Image);
if( image != null )
image.Enabled = value;
var label = FindViewById<TextView>(Resource.Id.ButtonBar_LabeledButton_Label);
if( label != null )
label.Enabled = value;
base.Enabled = value;
}
}
}
Update
Attributes
Every View and ViewGroup object supports their own variety of XML
attributes. Some attributes are specific to a View object (for
example, TextView supports the textSize attribute), but these
attributes are also inherited by any View objects that may extend this
class. Some are common to all View objects, because they are inherited
from the root View class (like the id attribute). And, other
attributes are considered "layout parameters," which are attributes
that describe certain layout orientations of the View object, as
defined by that object's parent ViewGroup object.
android:tint is specific to ImageView, and is ignored. I didn't inspect the attribute set in ButtonBar$LabeledButton's inflation constructor to see if it is at lest present to work with. Declaring a custom attribute would solve this, but then it's assignment to the ImageView and TextView in their now required custom classes' inflation constructors would be obscured (I prefer to leverage the framework as much as possible to minimize any extra, one-off code b/c of the maintenance and potential point-of-failure it introduces).
ButtonBar
public class ButtonBar : LinearLayout
{
public ButtonBar(Context context, IAttributeSet attributes) : base(context, attributes) { }
public override bool Enabled
{
set
{
SetChilderenEnabled(value);
base.Enabled = value;
}
}
private void SetChilderenEnabled(bool value)
{
for (int i = 0; i < ChildCount; i++)
{
GetChildAt(i).Enabled = value;
}
}
[Register("us.sam.views.ButtonBar$LabeledButton")]
public class LabeledButton : LinearLayout
{
private ImageView _buttonIV;
private TextView _labelTV;
private int _buttonIV_src;
private string _labelTV_text;
public override bool Enabled
{
set
{
if (_buttonIV != null)
_buttonIV.Enabled = value;
if (_labelTV != null)
_labelTV.Enabled = value;
base.Enabled = value;
}
}
public LabeledButton(Context context, IAttributeSet attributes) : base(context, attributes)
{
ReadAttributes(context, attributes);
LayoutInflater inflater = LayoutInflater.From(context);
_labelTV = (TextView)inflater.Inflate(Resource.Layout.ButtonBar_LabeledButton_LabelTextView, this, false);
_buttonIV = (ImageView)inflater.Inflate(Resource.Layout.ButtonBar_LabeledButton_ButtonImageView, this, false);
_labelTV.Text = _labelTV_text;
_buttonIV.SetImageResource(_buttonIV_src);
AddViewInLayout(_buttonIV, ChildCount, _buttonIV.LayoutParameters);
AddViewInLayout(_labelTV, ChildCount, _labelTV.LayoutParameters);
}
private void ReadAttributes(Context context, IAttributeSet attributes)
{
Android.Content.Res.TypedArray typedArray = context.ObtainStyledAttributes(attributes, Resource.Styleable.LabeledButton);
_buttonIV_src = typedArray.GetResourceId(Resource.Styleable.LabeledButton_button_imageview_src, 0);
_labelTV_text = typedArray.GetString(Resource.Styleable.LabeledButton_label_textview_text);
typedArray.Recycle();
}
}
}
recordexpandablelistview_groupbuttonbar_trecords.axml
<?xml version="1.0" encoding="utf-8"?>
<!--LinearLayout-->
<view class="us.sam.RecordsView$RecordExpandableListView$GroupButtonBar"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RecordExpandableListView_GroupButtonBar_TRecords"
style="#style/RecordExpandableListView_GroupButtonBar">
<!--LinearLayout-->
<us.sam.Views.ButtonBar
android:id="#+id/ButtonBar"
style="#style/ButtonBar">
<include layout="#layout/ButtonBar_LabeledButton_AddNew"/>
</us.sam.Views.ButtonBar>
<ImageView style="#style/ListItem_Divider_Horizontal"/>
</view>
ButtonBar_LabeledButton_AddNew.axml
<?xml version="1.0" encoding="utf-8"?>
<!--LinearLayout-->
<view class="us.sam.views.ButtonBar$LabeledButton"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ButtonBar_LabeledButton_AddNew"
style="#style/ButtonBar_LabeledButton"
button_imageview_src="#drawable/v__ic_add_circle_outline_black_24dp"
label_textview_text="#string/ButtonBar_LabeledButton_LabelTextView_Text_AddNew">
</view>
ButtonBar_LabeledButton_ButtonImageView.axml
<?xml version="1.0" encoding="utf-8"?>
<!--<view class="us.sam.views.ButtonBar$LabeledButton$Button"-->
<ImageView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ButtonBar_LabeledButton_ButtonImageView"
style="#style/ButtonBar_LabeledButton_ButtonImageView"/>
<!--android:src="#drawable/v__ic_add_circle_outline_black_24dp"-->
ButtonBar_LabeledButton_LabelTextView.axml
<?xml version="1.0" encoding="utf-8"?>
<!--view class="us.sam.views.ButtonBar$LabeledButton$Label"-->
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ButtonBar_LabeledButton_LabelTextView"
style="#style/ButtonBar_LabeledButton_LabelTextView"/>
<!--android:text="Add New"-->
styles.xml
<!--LinearLayout-->
<style name="RecordExpandableListView_GroupButtonBar">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">match_parent</item>
<item name="android:orientation">vertical</item>
<!--use isChildSelectable() override in BaseExpandableListAdapter instead-->
<!--item name="android:clickable">true</item-->
</style>
<!--LinearLayout-->
<style name="ButtonBar">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:orientation">horizontal</item>
</style>
<!--LinearLayout-->
<style name="ButtonBar_LabeledButton">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_marginLeft">10dp</item>
<item name="android:orientation">horizontal</item>
</style>
<!--ImageView-->
<style name="ButtonBar_LabeledButton_ButtonImageView">
<item name="android:layout_width">40dp</item>
<item name="android:layout_height">40dp</item>
<item name="android:tint">#color/button_bar_csl</item>
</style>
<!--TextView-->
<style name="ButtonBar_LabeledButton_LabelTextView">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_marginLeft">10dp</item>
<item name="android:layout_gravity">center_vertical</item>
<item name="android:gravity">center_vertical</item>
<item name="android:textSize">14sp</item>
<item name="android:textColor">#color/button_bar_csl</item>
</style>
<style name="ListItem_Divider_Horizontal">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">1px</item>
<item name="android:background">#android:color/black</item>
</style>
First of all I am pretty new in programming Xamarin and Android. I have created a SeekbarPreference but some how they layout does not display all correctly. The value is dropping of from the box (see picture).
My styles.xml:
<?xml version="1.0" encoding="utf-8" ?>
<resources>
<style name="MyTheme" parent="MyTheme.Base">
</style>
<style name="MyTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#EC6A1E</item>
<item name="colorAccent">#EC6A1E</item>
<item name="toolbarStyle">#style/custom_toolbar</item>
<item name="preferenceTheme">#style/PreferenceThemeOverlay.v14.Material.Fix</item>
</style>
<style name="custom_toolbar" parent="#style/Widget.AppCompat.Toolbar">
<item name="titleTextColor">#FFFFFF</item>
</style>
<style name="PreferenceThemeOverlay.v14.Material.Fix" parent="PreferenceThemeOverlay.v14.Material">
<item name="seekBarPreferenceStyle">#style/Preference.SeekBarPreference.Fix</item>
</style>
<style name="Preference.SeekBarPreference.Fix">
</style>
</resources>
Here is my settings.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.preference.PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<android.support.v7.preference.SeekBarPreference
android:id="#+id/preference_range_seek_bar"
android:layout_height="wrap_content"
android:key="range"
android:title="Range"
android:summary="Here you can set the range of your location"
android:max="10000"
android:defaultValue="500" />
<android.support.v7.preference.SeekBarPreference
android:key="favRange"
android:title="Favorite Range"
android:summary="Here you can set the range of your Favorite location"
android:max="10000"
android:defaultValue="500" />
<android.support.v7.preference.Preference android:title="Title" android:summary="This is the summary">
</android.support.v7.preference.Preference>
</android.support.v7.preference.PreferenceScreen>
What I do not understand is how can I find which xml attributes I can use on the SeekBarPreference to fix this. When I look in Googles Documents I cannot find a description of xml attributes on this SeekBarPreference.
I know my theme is fuzzy as I played a lot with it. But when I have it working I will adjust this.
Hopefully someone could help me, or have an example..
Ran into the same issue. Here you go with the solution, testion on emulator running API 25:
Step 1, Create a new class that extends SeekBarPreference
public class CustomSeekBarPreference extends SeekBarPreference {
private TextView mSeekBarValueTextView;
public CustomSeekBarPreference(Context context,
AttributeSet attributeSet,
int i, int i1) {
super(context, attributeSet, i, i1);
}
public CustomSeekBarPreference(Context context, AttributeSet attributeSet, int i) {
super(context, attributeSet, i);
}
public CustomSeekBarPreference(Context context, AttributeSet attributeSet) {
super(context, attributeSet);
}
public CustomSeekBarPreference(Context context) {
super(context);
}
#Override
public void onBindViewHolder(PreferenceViewHolder preferenceViewHolder) {
super.onBindViewHolder(preferenceViewHolder);
mSeekBarValueTextView = (TextView) preferenceViewHolder.findViewById(android.support.v7.preference.R.id.seekbar_value);
if (mSeekBarValueTextView != null) {
LayoutParams layoutParams = mSeekBarValueTextView.getLayoutParams();
layoutParams.height = LayoutParams.WRAP_CONTENT;
mSeekBarValueTextView.setLayoutParams(layoutParams);
}
}
}
In your preferences.xml replace the SeekBarPreference class name with the custom class name.
<com.google.xxx.view.CustomSeekBarPreference
android:key="cacheLimit"
android:title="#string/setting_cache_limit_title"
android:dependency="enableCaching"
android:defaultValue="20"
android:max="40"/>
In my Android app I have two different themes (light and dark).
For example:
<style name="AppThemeDark" parent="Theme.AppCompat">
<item name="colorPrimary">#android:color/black</item>
<item name="colorPrimaryDark">#android:color/black</item>
<item name="colorAccent">#android:color/holo_red_dark</item>
<item name="android:textColor">#android:color/white</item>
<item name="windowActionModeOverlay">true</item>
</style>
<style name="AppThemeLight" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="windowActionModeOverlay">true</item>
</style>
So, now I can apply, for example, different text colors to a TextView (white for dark theme and black for light):
<item name="android:textViewStyle">#style/TextViewDark</item>
<style name="TextViewDark">
<item name="android:textColor">?android:attr/colorAccent</item>
</style>
But it will apply to all TextViews.
The main question, is it possible to make in XML (not programmatically) next:
Light theme: Half of TextViews text color black, and another half green.
Black theme: TextViews that black in Light theme - red, and another half - blue (which are green in Light theme).
Create 2 classes extends TextView
public class OneTextView extends TextView {
public OneTextView(Context context) {
super(context);
init(context);
}
public OneTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public OneTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
private void init(Context context){
int[] attrs = new int[] { R.attr.myFirstColor};
TypedArray ta = context.obtainStyledAttributes(attrs);
int appColor = ta.getColor(0, 0);
ta.recycle();
// set theme color
setTextColor(appColor);
}
}
public class SecondTextView extends TextView {
public SecondTextView(Context context) {
super(context);
init(context);
}
public SecondTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public SecondTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
private void init(Context context){
int[] attrs = new int[] { R.attr.mySecondColor};
TypedArray ta = context.obtainStyledAttributes(attrs);
int appColor = ta.getColor(0, 0);
ta.recycle();
// set theme color
setTextColor(appColor);
}
}
each class you can use in xml like this
<com.route.to.class.OneTextView
android:layout_width="match_parent"
android:layout_height="match_parent" />
OneTextView can have black and red colors
SecondTextView can have green and blue colors
define attr.xml in values
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="myFirstColor" format="color" />
<attr name="mySecondColor" format="color" />
</resources>
Then in your styles.xml, define colors for each theme:
<style name="Theme.MyApp" parent="#style/Theme.Light">
<item name="myFirstColor">#color/black</item>
<item name="mySecondColor">#color/green</item>
</style>
<style name="Theme.MyApp.Dark" parent="#style/Theme.Dark">
<item name="myFirstColor">#color/green</item>
<item name="mySecondColor">#color/blue</item>
</style>
you have defined in styles.xml
<style name="TextViewDark">
<item name="android:textColor">?android:attr/colorAccent</item>
</style>
<style name="TextViewLight">
<item name="android:textColor">#color/green</item>
</style>
then you can use it in main.xml
<LinearLayout>
<TextView
android:id="#+id/light_text_view"
android:text"i´m use light theme"
style="#style/TextViewLight"/>
<TextView
android:id="#+id/dark_text_view"
android:text"i´m use darktheme"
style="#style/TextViewDark"/>
</LinearLayout>
you don´t need to define styles in AppThemes
In my Android application I need to apply style for a group of buttons, instead of styling each button individual. Something like this:
<?xml version="1.0" encoding="utf-8"?>
<!-- put here style="#ststyle/Button_Style" -->
<Button android:id="#+id/button1" android:text="#string/b01" />
<Button android:id="#+id/button2" android:text="#string/b02" />
<Button android:id="#+id/button4" android:text="#string/b03" />
<!-- end style -->
You can write the style for button like this ;
style_btn.xml
<style name="style_btn" parent="Wrap">
<item name="android:background">#drawable/btn_bg</item>
<item name="android:gravity">center</item>
<item name="android:textColor">#android:color/white</item>
<item name="android:textStyle">bold</item>
<item name="android:layout_marginTop">4dp</item>
<item name="android:minWidth">90dp</item>
</style>
apply that style to your button :
<Button
android:id="#+id/attach_file"
style="#style/style_btn"
android:layout_centerVertical="true"
android:background="#drawable/orange_bg"
android:drawablePadding="10dp"
android:drawableRight="#drawable/attach"
android:text="#string/str_email_attach" />
If you need the style to all of the buttons in your application, mention in your App theme style, Then no need to apply for every button.
<style name="YourTheme" parent="android:Theme.Light">
<item name="android:buttonStyle">#style/Button</item>
</style>
If you need the style to particluar button , then apply to every button
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/Button"
android:text="Button" />
I know I'm late joining the party but I stumbled across this when trying to figure out the same problem myself.
What I did was:
Depending on how may button groups you have (say 3 for example) you need to subclass button and create three custom button classes (see below)
//Custom button 1
public class CustomButton1 extends Button {
public CustomButton1(Context context, AttributeSet attrs) {
super(context, attrs, R.attr.attrStyle1);
}
public CustomButton1(Context context, AttributeSet attrs, int defStyle) {
super(context, null, R.attr.attrStyle1);
}
}
//Custom button 2
public class CustomButton2 extends Button {
public CustomButton2(Context context, AttributeSet attrs) {
super(context, attrs, R.attr.attrStyle2);
}
public CustomButton2(Context context, AttributeSet attrs, int defStyle) {
super(context, null, R.attr.attrStyle2);
}
}
//Custom button 3
public class CustomButton3 extends Button {
public CustomButton3(Context context, AttributeSet attrs) {
super(context, attrs, R.attr.attrStyle3);
}
public CustomButton3(Context context, AttributeSet attrs, int defStyle) {
super(context, null, R.attr.attrStyle3);
}
}
You can see from the custom classes I have passed a custom attr. These I define in my styles.xml and use them as reference. See my styles.xml below:
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
</style>
<style name="theme1">
<item name="#attr/attrStyle1">#style/CustomButton1</item>
<item name="#attr/attrStyle2">#style/CustomButton2</item>
<item name="#attr/attrStyle3">#style/CustomButton3</item>
<item name="android:background">#color/warning_yellow_colour</item>
</style>
<style name="CustomButton1" parent = "#android:style/Widget.Button">
<item name="android:textColor">#color/white_colour</item>
<item name="android:padding">20dp</item>
<item name="android:background">#color/banner_background_sensor_colour</item>
</style>
<style name="CustomButton2" parent = "#android:style/Widget.Button">
<item name="android:textColor">#color/white_colour</item>
<item name="android:padding">20dp</item>
<item name="android:background">#color/button_red_colour</item>
</style>
<style name="CustomButton3" parent = "#android:style/Widget.Button">
<item name="android:textColor">#color/white_colour</item>
<item name="android:padding">20dp</item>
<item name="android:background">#color/text_blue_colour</item>
</style>
<attr name="attrStyle1" format="reference"/>
<attr name="attrStyle2" format="reference"/>
<attr name="attrStyle3" format="reference"/>
By linking the style to the attr, you then apply that style to your custom class, which you can then duplicate as many times as needed
CustomButton1 theme1 = (CustomButton1)findViewById(R.id.theme1);
CustomButton2 theme2 = (CustomButton2)findViewById(R.id.theme2);
CustomButton3 theme3 = (CustomButton3)findViewById(R.id.theme3);
Hopefully this is of benefit to someone!