Android UI control library

Jakob Aagesen 1 Reputation point
2021-03-20T12:16:36.15+00:00

hi
I am new to Xamarin and device development, so bear with me if i don't use the correct naming.
I am having difficulties understanding the concept of creating a UI control in an Android Class Library.
This is not a real use case, but for simplicity i want to create a button in an Android Class Library to be able to use that button in several Xamarin solution.
I have seen that, when creating a Mobile App (Xamarin.Forms) solution it has an android project and an Xamarin forms project. The android project has a layout folder with some page designs. Instead of designing a whole page through the use of layouts, is it possible to only describe the design of a single button, in one of those layout xml files? A link to some god beginners documentation on how to use layout xml descriptions would be appreciated.

The second question is, how do i reference this specific button from another projects xaml page?
I have read an article on how to consume native views using Shared Asset Project, but if i use that approach, how do i use the SAP in a Xamarin forms project?
The only examples i have seen with SAP is that the android project loads the App.xaml object from the SAP rather than from an Xamarin forms App object and i would like to keep the Xamarin forms project for describing pages shared between different devices (ios, android)

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,296 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. JessieZhang-MSFT 7,706 Reputation points Microsoft Vendor
    2021-03-22T06:10:48.537+00:00

    Hello,

    Welcome to our Microsoft Q&A platform!

    This is not a real use case, but for simplicity i want to create a button in an Android Class Library to be able to use that button in several Xamarin solution.
    I have seen that, when creating a Mobile App (Xamarin.Forms) solution it has an android project and an Xamarin forms project. The android project has a layout folder with some page designs. Instead of designing a whole page through the use of layouts, is it possible to only describe the design of a single button, in one of those layout xml files?

    Do you want to create a new button and use the button in several pages's layout , right?

    If yes, you can use define a xml( MyButton.xml ) in folder layout and include it in other page's layout .
    You can refer to the following code:

    MyButton.xml

    <?xml version="1.0" encoding="utf-8"?>  
    <Button xmlns:android="http://schemas.android.com/apk/res/android"  
        android:id="@+id/mBtn"  
        android:layout_width="200dp"  
        android:layout_height="50dp"  
        android:text="my button"  
        android:textColor="@android:color/holo_red_dark"  
        android:background="@android:color/holo_orange_light"  
        >  
    </Button>  
    

    And include above button on other page's layout:

    <?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">  
      
        <include  
            layout="@layout/mybutton"/>  
      
    </LinearLayout>  
    

    Best Regards,

    Jessie Zhang

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

    0 comments No comments