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.

3

3 Answers 3

6

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.

2
  • this worked for me, thank you sir Oct 21, 2021 at 14:57
  • You shouldn't generate the constructors with default arguments (i.e. this(context, attrs, 0) because 0 will resolve to an unintended style. Drop the 0.
    – masterwok
    Jan 6, 2022 at 19:30
2

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

-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"/>
1
  • 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 7, 2019 at 7:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.