14

Please, let me know, how can I set the default background of a custom button to null.

I mean... I know I can define a "style" which set android:background to "@null", and ask users to explicitly apply the style in their layout. For example:

<style name="MyButton" parent="@android:style/Widget.Button">
    <item name="android:background">@null</item>
</style>

and

<com.xxx.widget.MyButton
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    style="@style/MyButton"
    android:text="MyButton" />

Above code is working well. But how can I apply this style in my class "MyButton" internally and let users not to set style explicitly?

For example, how to make following layout works as before:

<com.xxx.widget.MyButton
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="MyButton" />

I tried to do this in the constructor as below, but its not working.

public MyButton(Context context, AttributeSet attrs) {
    this(context, attrs, com.xxx.R.style.MyButton);
}

PS. I want to apply this "null" background when a user does not set the background explicitly.

Narendra Singh
  • 3,990
  • 5
  • 37
  • 78
Henry
  • 942
  • 3
  • 11
  • 21
  • I think you should change the title of your question. You can always have a default style, but that would be able to change later. Perhaps you neeed to say 'final style' or something like that – ikbal Mar 12 '13 at 16:01
  • 1
    See http://stackoverflow.com/questions/4493947/how-to-define-theme-style-item-for-custom-widget for some tips on this. – Steve Pomeroy Mar 28 '13 at 16:28
  • Had a similar requirement: http://stackoverflow.com/questions/31504413/theme-style-for-custom-view/31505624#31505624 – stefan Jul 19 '15 at 20:41

3 Answers3

5

Really late to the party here. But in case someone stumbles upon the same problem, here is my solution.

Let's say we want to have a custom view that extends a TextInputLayout. Here's what the class would typically look like:

class MyTextInputLayout: TextInputLayout {
    constructor(context: Context) : this(context, null)
    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int)
        : super(context, attrs, defStyleAttr)
}

Now, in order to use our custom style as default, change the third constructor (the one with three arguments) into this:

class MyTextInputLayout: TextInputLayout {
    constructor(context: Context) : this(context, null)
    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int)
        : super(ContextThemeWrapper(context, R.style.MyTextInputLayoutStyle), attrs, defStyleAttr)
}

Now we can use the custom view in a layout XML file like this:

<com.xxx.MyTextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

we no longer need to define style="@style/MyTextInputLayoutStyle" in the component.

In short, we need to replace the context passed to the super constructor with the one that wraps our style. This way we don't need to define R.attr.MyTextInputLayoutStyle in the existing theme. Useful when you want to use the custom view as a library.

Falih Mulyana
  • 51
  • 1
  • 3
2

In your MyButton() constructor, why not call setBackground(null)?

Karim Varela
  • 7,562
  • 10
  • 53
  • 78
-2

This is more or less a cheap way to do it, but why not set the background to a transparent square. Like this ->

transparent_square.xml: (Put it in the drawable directory)

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/square"
    android:>

    <solid android:color="#00808080"></solid> // The first two zeros set the alpha
                                              // to 0

</shape>

Then set the background of the button to your drawable.

Button:

<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Send"
        android:id="@+id/sendButton"
        android:layout_weight="1"
        android:background="@drawable/transparent_square"/>
Jacob Pickens
  • 471
  • 1
  • 7
  • 15
  • Sorry, but I think this doesn't answer the question. @Henry is asking how to do this by default without specifying in the xml of the Button. – Smeagol Jun 07 '19 at 07:30