Compose a button to use in various parts of my project

590 Views Asked by At

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>
2

There are 2 best solutions below

1
On BEST ANSWER

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"
    />
1
On

If you take a look at the source code for ProgressBar, you'll see that you can use a TypedArray to retrieve styled attributes from the XML AttributeSet.

public ProgressBar(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);
    ...
    final TypedArray a = context.obtainStyledAttributes(
            attrs, R.styleable.ProgressBar, defStyleAttr, defStyleRes);
    ...
    // this is where we get the drawable that is specified in the XML
    final Drawable progressDrawable = a.getDrawable(R.styleable.ProgressBar_progressDrawable);
    ...
}

You can do the same to get the app attributes you're looking for, but some of the attributes are marked as internal. In those cases I would consider overriding them in res/values/attrs.xml because they may change.

Note: progressDrawable is not necessarily internal, I just used it as an example

<resources>
    <declare-styleable name="ProgressBar">
        <attr name="progressDrawable"/>
    </declare-styleable>
</resources>

As for your second question, I'm not sure if you want to extend LinearLayout (which I would advise against due to the fact that you would have to write code to create views) or simply reuse the LinearLayout with the views inside it.

If it's the latter, then you can just make an XML file with just your LinearLayout inside of it with the appropriate children, let's call it myLayout.xml. Then, in your other XML files you can just place it in like so:

myLayout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout>
   <Toolbar/>
   <LinearLayout/>
</LinearLayout>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" 
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/lorem_ipsum"/>

    <include layout="@layout/myLayout"/>

</LinearLayout>