Hello,
Welcome to our Microsoft Q&A platform!
In UWP I would inherit from another control (Button, TextBlock, UserControl, etc.) & create the accompanying xaml file
Xamarin.Android have this way to create a custom control. For example. We want to create a custom TextView.
public class MyTextView : TextView
{
protected MyTextView(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer)
{
}
public MyTextView(Context context) : base(context)
{
Initialize(context);
}
private void Initialize(Context context, IAttributeSet attrs = null)
{
if (attrs != null)
{
TypedArray ta = context.ObtainStyledAttributes(attrs, Resource.Styleable.test);
string text = ta.GetString(Resource.Styleable.test_text);
int textAttr = ta.GetInt(Resource.Styleable.test_testAttr, 0);
// Log.e(TAG, "text = " + text + " , textAttr = " + textAttr);
ta.Recycle();
}
}
public MyTextView(Context context, IAttributeSet attrs) : base(context, attrs)
{
Initialize(context, attrs);
}
public MyTextView(Context context, IAttributeSet attrs, int defStyleAttr) : base(context, attrs, defStyleAttr)
{
Initialize(context, attrs);
}
public MyTextView(Context context, IAttributeSet attrs, int defStyleAttr, int defStyleRes) : base(context, attrs, defStyleAttr, defStyleRes)
{
Initialize(context, attrs);
}
protected override void OnDraw(Canvas canvas)
{
base.OnDraw(canvas);
}
}
}
Then we can add properties like following code in the attrs.xml
<?xml version="1.0" encoding="utf-8" ?>
<resources>
<declare-styleable name="test">
<attr name="text" format="string" />
<attr name="testAttr" format="integer" />
</declare-styleable>
</resources>
we can reusable in the layout, just with custom control's name.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<App55.MyTextView
android:layout_width="200dp"
android:layout_height="200dp"
app:testAttr="520"
app:text="helloworld"
/>
</RelativeLayout>
If you want to create custom control in Xamarin.Android, just read native android's article, there are similar.
https://developer.android.com/guide/topics/ui/custom-components
If you want to know more details about Xamarin.Android's custom control or view, you can google: Creating an Android Custom View in Xamarin
===============
Update================
Is there any tutorial or documentation on learn.microsoft.com for this with slightly more detail? I was also slightly confused about attrs.xml and where to put it. Thanks again, hopefully it will get me started!
No, I did not find these articles. I read Google's Android article to create the custom-control in Xamarin Android. For attrs.xml, you can create an XML file in values folder, change the name of attrs.xml, please note his properties like following screenshot.
Best Regards,
Leon Lu
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.