Search code examples
androidandroid-xmlandroid-components

Compose a button to use in various parts of my project


I've a button and i need to use this same button with same proprieties in all my projects and i want to create a component to just call this component and if i need to change my button, change in just one class do change all the uses.

I have this button:

<br.com.simplepass.loading_button_lib.customViews.CircularProgressButton
            android:id="@+id/createMatch"
            android:layout_width="120dp"
            android:layout_height="wrap_content"
            android:background="@color/buttonColor"
            android:layout_marginTop="16dp"
            app:spinning_bar_width="4dp"
            app:spinning_bar_color="#FFF"
            android:alpha="0.5"
            android:textColor="@color/colorAccent"
            android:elevation="4dp"
            android:enabled="false"
            android:layout_marginLeft="16dp"
            android:textStyle="bold"
            android:text="@string/createMatch"
            android:textSize="14sp"
            app:spinning_bar_padding="6dp"/>

I create a class extending CircularProgressButton but some proprieties i don't have ideia how to access in the code. The proprieties with app: for example.

Merging two questions, how i make a component with a layout with various itens?

for example:

<LinearLayout>
   <Toolbar/>
   <LinearLayout/>
</LinearLayout>

I want to compose this xml in a class to call and get all this itens in all the layouts using my custom class.

<MyLinearLayout>
...
</MyLinearLayout>

Solution

  • some proprieties i don't have ideia how to access in the code. The proprieties with app: for example.

    Have a look here. You will have to subclass a View and provide a constructor that takes a Context and AttributeSet.

    class CircleView extends View {
    
        private String mTitleText = "Your default title";
    
        public CircleView(Context context, AttributeSet attrs) {
            super(context, attrs);
    
            // You can and should also pass a style as third argument
            final TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.CircleView, 0, 0);
    
            if (a.hasValue(R.styleable.CircleView_titleText)) {
                mTitleText = a.getString(R.styleable.CircleView_titleText);
            }
    
            a.recycle();
        }
    }
    

    You might have noticed the use of R.styleable.CircleView_titleText in my example. Before using custom attributes, you have to tell the compiler about them. This is achieved by adding a .xml defining your attributes and their expected format.

    <resources>
        <declare-styleable name="CircleView">
            <attr name="titleText" format="string" />
        </declare-styleable>
    </resources>
    

    how i make a component with a layout with various itens?

    Reusable Layouts is probably what you are looking for.

    An example of this would be to define a custom toolbar as toolbar.xml.

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v7.widget.Toolbar
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#e40056"
        android:elevation="4dp"
        android:minHeight="?attr/actionBarSize"
        app:titleTextColor="#ffffff"
        />
    

    and then include it in other views:

    <include
        android:id="@+id/myToolbarId"
        layout="@layout/toolbar"
        />