How to create new button in Xamarin

xamarin android button example
xamarin button size
xamarin custom button
xamarin forms toggle button
xamarin forms link button
xamarin navigation button
xamarin button states
xamarin button content

I'm really new in coding but I know a few things in C#. I'm learning by myself just for the sake of it, for fun. I've made some little programs in console and now I'm learning Xamarin.

For now, I've followed the xamarin tutorials (those with the "phoneword" app).

I'm now working on another app for android and I would like to know how to create a new button in code.

Example: On the main page, the user presses a button that creates a new page where he can fill a form. After he finished, a new button appear on the main page that leads to the form previously filled. So when the user open the app, he has to choose whether he opens a new form or accesses to the old form (because now there is a new button).

All I want to know is: how do I create this new button ?

I've look everywhere and can't find any working solution. I don't know if that matters, but all I can see is explanations for Xamarin.Forms and that's not what I'm using. I'm using Xamarin Native.

For example, I've tried :

Button b = new Button();
b.Content = "Test";
b.Click += OnClick;

But it's not working. VS returns various errors. I understand that "Button()" needs parameters, but I don't know what to do.

Someone can give a little help, please ?

PS: I'm not a native speaker, so please don't mind some mistakes with English grammar.

First you need to create parameters of layout & than add those params to button like this

var params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FillParent, ViewGroup.LayoutParams.WrapContent);    
Button b = new Button(this);
b.SetText("Test",TextView.BufferType.Normal);
b.Click=OnClick;
b.LayoutParameters = params;

Or add layout in your in axml like this

<LinearLayout 
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@id/linear_layout"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical" >

Than in your activity get you Layout using Id & add button to it.

LinearLayout l_layout = (LinearLayout)FindViewById(Resource.Id.linear_layout);
l_layout.Orientation = Orientation.Horizontal;
Button b = new Button(this);
b.Click=OnClick;
b.SetText("Test",TextView.BufferType.Normal);
l_layout.AddView(b);

Xamarin.Forms Button Tutorial, Create a Xamarin.Forms Button in XAML. Respond to the Button being tapped. Change the appearance of the Button . You will use Visual Studio 2019, or Visual​  Create a button. Launch Visual Studio, and create a new blank Xamarin.Forms app named ButtonTutorial . Ensure that the app uses .NET Standard as the shared code In Solution Explorer , in the ButtonTutorial project, double-click MainPage.xaml to open it. Then, in MainPage.xaml , remove all of the

With this You can create the button You only need to add the size of the button and add it to some layout.

Xamarin.Forms

Button button = new Button();
button.Text = "Title";

Xamarin.Android

Button button = new Button(this); //if your using a fragment you have to use this.Activity
button.Text = "Title"; 

Xamarin.iOS

UIButton button = new UIButton();
button.SetTitle("Title", UIControlState.Normal);

How to create new button in Xamarin, First you need to create parameters of layout & than add those params to button like this var params = new LinearLayout. On all three of the supported Xamarin Forms platforms (iOS, Android, UWP), the Button and ImageButton components allow you to set the corner radius of the button. If you set each of the height and the width of the button to exactly twice that of the radius of the corners, you will end up with a round button.

Xamarin forms button example

var Tittlebutton= new Button
    {
    Text = "Title"
    };

What is the best way to add buttons in Xamarin Forms?, but the thing is: I want to know which way is better for adding a button and to not have any future code problems. Thank you! share. Share a link to this question. Gets or sets an object that controls the position of the button image and the spacing between the button's image and the button's text. Corner Radius. Gets or sets the corner radius for the button, in device-independent units. Disable Layout. For internal use by the Xamarin.Forms platform.

how to create buttons dynamically in xamarin forms app, hi all, I wish to create buttons dynamically within a stacklayout in xamarin forms app.. I tried in .cs using stacklayout buttons by statically.. With this You can create the button You only need to add the size of the button and add it to some layout. Xamarin.Forms. Button button = new Button(); button.Text = "Title"; Xamarin.Android. Button button = new Button(this); //if your using a fragment you have to use this.Activity button.Text = "Title"; Xamarin.iOS

Xamarin Android Tutorial - Working with Button, Adding SfButton reference. You can add SfButton reference using one of the following methods: Method 1: Adding  Download the sample. The Xamarin.Forms RadioButtonis a type of button that allows users to select one option from a set. Each option is represented by one radio button, and you can only select one radio button in a group. The RadioButtonclass inherits from the Buttonclass.

Xamarin Forms with Visual Studio Part 2 [Label, Button, Entry , To achieve this we are going have to set the ContentLayout and Image properties from the Button we want to customize. Let's just jump right into it  The ImageButton view combines the Button view and Image view to create a button whose content is an image. The user presses the ImageButton with a finger or clicks it with a mouse to direct the application to carry out a particular task. However, unlike the Button view, the ImageButton view has no concept of text and text appearance.

Comments
  • "VS returns various errors" - it would be really helpful if you tell us what these errors are.
  • Xamarin Native can mean Xamarin.iOS, Xamarin.Android, Xamarin.Mac, maybe even some other flavors so you first need to say what you actually do.
  • @DiegoRafaelSouza : didn't do it on purpose. Sorry.
  • @IvanIčin as I wrote, it's for Android.
  • @Jason Below you can check screens I uploaded. Thanks for helping !
  • Thanks for responding, I have some issues with both ways. Here you can check what VS returns : link and link . Thanks for helping !
  • @Jojojojo The screenshots are hard to read (for instance I can't see your OnClick method), which it is why it is bad practise to use them. It is best to copy and paste your code and format it here according to the guidelines.
  • @jojojo - For first part, do that inside 'OnCreate' method of your activity & your are not passing 'this' in button at right side as I provided code. Create 'OnClick' method too or you can comment that line.
  • @CGPA6.4 as a matter of fact, I noticed that I didn't and try it again, but I uploaded the wrong screen. Thanks
  • So, I did what you said (comment the OnClick line) plus I rewrote the ID line (android:id="@+id/linear_layout"), and now it's working. Thanks a lot !
  • Thanks for responding. I tried but I don't know how to add a layout. So I tried @CGPA6.4 's way but got errors as you can see below.
  • which platform are you using ?