Home > Mobile >  creating custom xml view
creating custom xml view

Time:08-03

I am new in android developing and I heared about custom library(module), I see some examples about how to create a module, and create a class. But I didnt know how to create a custom xml view and design it, Like,

<com.example.customButton
android:layoutwidth="120dp"
android:layoutheight="50dp">
</com.example.customButton>

Can anyone help me ?.

CodePudding user response:

In android to create a custom view you need to create a class that extends the View class such as:

class PieChart extends View {
    public PieChart(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
}

check the Android documentation

But I think you are instead aiming at creating a style for a button instead, for doing this create a custom style to apply to your button check this

Hope this helps!

CodePudding user response:

I may suggest you to use native components like material button to design buttons, instead of using custom libraries, because they could be deprecated in the future and you don't want to depend on that. You can always customize buttons, textview, edittext by setting specific properties.

Let me give you an example:

<com.google.android.material.button.MaterialButton
                android:id="@ id/signup_button"
                android:layout_width="wrap_content"
                android:layout_height="50dp"
                android:layout_marginTop="50dp"
                android:backgroundTint="@color/purple_200"
                android:text="Sign up"
                android:textAlignment="center"
                android:textColor="@color/white"
                app:cornerRadius="4dp"/>

CodePudding user response:

If you want to create a custom view for example custom Button you should create a class that Extend Button and you can override attributes and actions

package com.example;

public class CustomButton extends Button {

}

then build the project and you can use it in the xml

<com.example.CustomButton
    android:layoutwidth="120dp"
    android:layoutheight="50dp"/>

Check the official doc to learn how to customize it

https://developer.android.com/training/custom-views/create-view#java

  • Related