0

Is it possible to create a button by passing parameters in XML ? doing it that way ?

<Button
    button:typeParameter="primary"
    button:size="md"/>

And after passing these 2 parameters the button is created as it should be, is it possible to do this?

I already have a button on which I created new attributes, now I want to know if it is possible to pass parameters and this button be called, without the need for the developer to have to code all these lines.

<customButton
    android:fontFamily="@font/mondrian_family_font"
    app:fontFamily="@font/mondrian_family_font"
    android:id="@+id/mdnButtonPrimary"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAllCaps="false"
    android:text="Button primary"
    android:paddingLeft="40dp"
    android:paddingRight="40dp"
    android:textColor="@color/color_neutral_lightest"
    android:layout_marginTop="10dp"
    android:textSize="@dimen/font_size_XXS"
    mdnbutton:radius="@dimen/border_radius_pill"
    mdnbutton:defaultColor="@color/color_brand_primary_medium"
    mdnbutton:focusColor="@color/color_brand_primary_darkest"
    style="?android:attr/borderlessButtonStyle"/>

It is possible that he will use these attributes after he installs my library. Now I want that when the developer is going to create his layout, instead of creating a button from scratch, he just passes parameters in the XML and the button is rendered.

14
  • It sounds like you might be looking for custom View attributes. However, you'd have to create a custom Button subclass, and you wouldn't be able to specify only those two custom attributes, there. You'll need at least the standard android:layout_width and android:layout_height attributes set, as well. Commented Mar 9, 2020 at 15:44
  • I strongly recommend you read this: developer.android.com/reference/android/widget/Button Commented Mar 9, 2020 at 15:45
  • Yes, I know I will need to create custom attributes, I just need to know if it is possible to pass two parameters in the XML and after I pass these two parameters the button is created as it should be. Commented Mar 9, 2020 at 15:53
  • It's not clear what you're asking. What exactly do you mean by "the button is created as it should be"? What are you thinking might be incorrect or improper? Commented Mar 9, 2020 at 16:02
  • I pass these two parameters and the button is created with background, with rounding and other things. I already do the button configuration, I just want to simplify things. Commented Mar 9, 2020 at 16:08

1 Answer 1

0

I suggest for you to make it as a style. But having it depend on two attributes is a bit hard, so I suggest to make a style for every combination of the two, so you have a style called primaryMd for example. Your button would be like this:

<Button
    style="@style/primaryMd" />

you can define this style in you res folder in styles.xml like this for example then:

<resources>

    <style name="primaryMd" parent="@style/Widget.AppCompat.Button.Borderless">
        <item name="android:fontFamily">@font/mondrian_family_font</item>
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:textAllCaps">false</item>
        <item name="android:text">Button primary</item>
        <item name="android:paddingLeft">40dp</item>
        <item name="android:paddingRight">40dp</item>
        <item name="android:textColor">@color/color_neutral_lightest</item>
        <item name="android:layout_marginTop">10dp</item>
        <item name="android:textSize">@dimen/font_size_XXS</item>
        <item name="mdnbutton:radius">@dimen/border_radius_pill</item>
        <item name="mdnbutton:defaultColor">@color/color_brand_primary_medium</item>
        <item name="mdnbutton:focusColor">@color/color_brand_primary_darkest</item>
    </style>

</resources>

I'm not entirely sure if it works with these custom mdnbutton attributes, but you could try it out maybe

Sign up to request clarification or add additional context in comments.

5 Comments

how to use my radius and defaultColor attribute?
judging from stackoverflow.com/questions/15824289/… you might be able to declare them in the style without the mdnbutton: in front of it
there is a possibility that I don't need to repeat the attributes, as I have several types of Buttons, and sometimes they only change one Color.
It works, it's a good solution. However, as I have several types of buttons that sometimes what change from one to the other is a border, or a background. The problem is that I have to keep repeating the attributes several times. @IvoBeckers
one style can inherit from another style by using the parent= after the name of the style. That can reduce having to repeat a lot of things. Also when you apply a style to a button you can still overwrite any values at the button itself, for example applying this primaryMd style to a button but also typing android:paddingLeft="60dp" would give it 60dp instead of the 40dp of the style

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.