question

JakobAagesen-6346 avatar image
0 Votes"
JakobAagesen-6346 asked RobCaplan edited

Android UI control library

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)

dotnet-xamarin
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

JessieZhang-2116 avatar image
0 Votes"
JessieZhang-2116 answered

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.



5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.