1

I have an custom view which extends Button. And I just want to set the belove style to this view.

    <style name="MultipleButtonStyle" parent="android:Widget.Material.Light.Spinner.Underlined">
        <item name="android:textColor">@android:color/black</item>
        <item name="android:textAppearance">?android:attr/textAppearanceMedium</item>
    </style>

To be able set style programmatically, I have tried some way. But They didn't work for me.

The First way, is using the style in the constructor of the custom button like this :

public class SpinnerButton extends AppCompatButton{

public SpinnerButton (Context context) {
        super(context, null, R.style.MultipleButtonStyle);

    }
}

Second way is use setTextAppearance method. It didn't work me as well.

if (Build.VERSION.SDK_INT < 23) {
        super.setTextAppearance(getContext(), R.style.MultipleButtonStyle );
    } else {
        super.setTextAppearance(R.style.MultipleButtonStyle);
    }
ziLk
  • 3,120
  • 21
  • 45

1 Answers1

4

your constructor looks fine, are you overriding also other ones (e.g. this with AttributeSet, which is called when inflating from XML)? check THIS question about Views constructors and some styling

second way is improper, because parent of your style is a style for whole Spinner.Underlined, not only for TextAppearance. in this case you should extend @android:style/TextAppearance.Medium (or other). if you choose this way then remember about deprecated setTextAppearance method in API23 and you may use some helper without checking OS version:

TextViewCompat.setTextAppearance(textView, android.R.style.TextAppearance_Medium);
Community
  • 1
  • 1
snachmsm
  • 17,866
  • 3
  • 32
  • 74
  • I don't call the constructor from XML. the custom view is created dynamically from the java layer. Beside, I was supposing setTextAppearance set whole style for a view. Thanks allot. Your explanation explains why the ways don't work for me. However, I don't know how can I draw a button as a spinner. – ziLk May 10 '17 at 07:09