Button Adapter Web Browser In Android Using Xamarin.Forms

Introduction
 
This article demonstrates how to use a button adapter web browser in Android using Xamarin.Forms. Xamarin is a platform that allows us to create a multi-platform mobile application for platforms, like Android, Windows, and iOS through a single integrated development environment (IDE). And with Xamarin.Forms, the interface design for all three platforms can be accomplished within its XAML-based framework. Xamarin apps are built with standard, native user-interface controls.
 
 
 
Let's start!
 
Step 1
 
Open Visual Studio and go to New Project >> Installed >> Visual C# >> Cross-Platform. Select Cross-Platform app, then give your project a name and location and click "OK" button.
 
 
 
Step 2
 
Next, go to project name > right click > Manage NuGet package for the solution. Select "Manage NuGet package". Just update the following NuGet packages to your project.
 
 
 
Now, select the following NuGet package and select your project to install on it.
  • Xamarin.Forms
 
 
 
Step 3 
 
Next, add an image to Solution Explorer >> Project Name.Android >> Resources >> Right-Click >> Drawable >> Add >> Existing Item. When I click an existing item button, now it opens a dialog box.
 
 
 
Next, a dialog box will appear. Choose image location and add images. 
 
 
 
The Images are added successfully. Then, move the cursor to that image and just verify the image.
 
 
Step 4 

Open Solution Explorer >> Project Name (Portable) >> MainPage.xaml. Double click for opening the design view of this page.
 
 
The code is given below.
 
 
 
XAML Code 
 
We created four buttons inside the stackLayout and BackgroundImage for adding the Contentpage. The button names are "C# Corner", "Android", "Windows", "IOS". Code copy is given here.
  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"  
  3.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"  
  4.              xmlns:local="clr-namespace:Browser"  
  5.              x:Class="Browser.MainPage"  
  6.              BackgroundImage="Code.png">  
  7.     <StackLayout>  
  8.         <Button Text="C# Corner"  
  9.                 Clicked="Button_Clicked"  
  10.                 FontAttributes="Bold"/>  
  11.         <Button Text="Android" 
  12.                 Clicked="Button_Clicked_1"   
  13.                 FontAttributes="Bold"/>  
  14.         <Button Text="Windows" 
  15.                 Clicked="Button_Clicked_2"   
  16.                 FontAttributes="Bold"/>  
  17.         <Button Text="Ios" 
  18.                 Clicked="Button_Clicked_3"   
  19.                 FontAttributes="Bold"/>  
  20.     </StackLayout>  
Step 5
 
Open Solution Explorer >> Project Name (Portable) >> MainPage.xaml.cs. Double click for opening the design view of this page.
 
 
The code is given below.
 
 
 
C# Code 
 
 This is the code for the button. Click the button and give the URL that you want to choose the link for.
 
 
Code copy is here. 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using Xamarin.Forms;  
  7.   
  8. namespace Browser  
  9. {  
  10.     public partial class MainPage : ContentPage  
  11.     {  
  12.         public MainPage()  
  13.         {  
  14.             InitializeComponent();  
  15.         }  
  16.   
  17.         private void Button_Clicked(object sender, EventArgs e)  
  18.         {  
  19.             var url = "https://www.c-sharpcorner.com/";  
  20.             Device.OpenUri(new Uri(url));  
  21.         }  
  22.   
  23.         private void Button_Clicked_1(object sender, EventArgs e)  
  24.         {  
  25.             var url = "https://en.wikipedia.org/wiki/Android";  
  26.             Device.OpenUri(new Uri(url));  
  27.         }  
  28.   
  29.         private void Button_Clicked_2(object sender, EventArgs e)  
  30.         {  
  31.             var url = "https://en.wikipedia.org/wiki/Microsoft_Windows";  
  32.             Device.OpenUri(new Uri(url));  
  33.         }  
  34.   
  35.         private void Button_Clicked_3(object sender, EventArgs e)  
  36.         {  
  37.             var url = "https://en.wikipedia.org/wiki/IOS";  
  38.             Device.OpenUri(new Uri(url));  
  39.         }  
  40.     }  
  41. }  
Step 6 
 
Next, select the Built & Deploy option, followed by selecting from the list of Android Emulator or Simulator. You can choose any API (Application Program Interfaces) Level Emulator or simulator to run it.
 
Output 
 
After a few seconds, you will see your app working.
 
Click the "C# Corner" button or any four buttons will open the "Open With" option. Choose browser then the given URL is displayed.
 
 
 
Finally, we have successfully created Xamarin.Forms.
 
Conclusion 
 
I hope you have learned how to create a Button Adapter Web Browser in Android Using Xamarin.Forms with Visual Studio and C#. 


Similar Articles