Create Android app with Xamarin.Android
7 minute(s) read | Published on: Sep 16, 2021 Updated on: Jan 26, 2022 |
Using Visual Studio and programming with C languages like C #, you can design and run more applications. These applications can be for Android or iOS operating systems. In this article, we want to design a simple app using Visual Studio.
In this article, we want to design the first application, a very simple application using Xamarin. To design this app, we want to use the RadioButton group and Button to design and create an application.
This program and application that we want to design are so that by clicking on the Button, a RadioButton that is related to that Button is displayed.
We want to design this simple app in Visual Studio 2013 because this version of Visual Studio is the lowest version of Visual Studio that can support Xamarin.
1- Open Visual Studio.
2- Then, from the menu that appears, select the File option and then select New. Then we select Project until I create a new project.
3- Then, in the window that opens, select Android from the left and then select the Blank App option to create a new and empty project and design an application for the Android operating system.
4- In the Solution Explorer section, we can also view the default structure of the program and change it if necessary.
5- We can create the UI in Main. axml.
6- We enter the codes and commands that we must enter to design the developed application( in German: Entwerfen einer entwickelten Anwendung ) in the UI and the MainActivity.cs file.
7- Then I open the Main. axml file and from the toolbox, we must insert a RadioGroup and Button to the program and Project we have created.
8- We must choose a unique id for all these elements added to our Project and program.
9- To do this, we must select RadioGroup and then enter the option and the F4 key. After that, the properties window will be displayed in which we can select a unique name or id for each of the elements in the Project and program.
10- In addition to id, we can also change the text related to the Button.
11- We must even change the written text and the id related to each of the Radio buttons, a subset of RadioGroup.
12- To do this, we must select a Radio button so that after that, pressing the F4 key will open a window related to properties, and we will be able to change the text and id in that window.
13- We must open the MainActivity.cs file and add the following code in the corresponding file.
14- After the SetContentView command (Resource.Layout.Main), we must also add the below code.
In the inserted codes, we add the Button to the UI, and then we also enter the command and the related event.
15- After the OnCreate command (Bundle bundle), we must add the below commands.
In the LN line's code, we first find the RadioGroup used in the UI.
In the second line, we will find the code listed above for the RadioButton id we have selected.
In the third line, we create an alert box so that one of the alerts related to it will be executed with each click on the Button.
In the fourth line of the codes listed above, we personalize the message that we want to be displayed so that the text inserted in the radio button is displayed each time we click on the Button.
16- In general, the code inserted in the MainActivity.cs file will be as follows after applying the changes.
17- Now is the time to run the designed program and app.
18- To run the app, press Ctrl + Shift + B.
19- After we have designed and implemented this program, three options are displayed for the audience and the user, and the user displays a message to each of the buttons and options and says which one of the buttons Has been selected.
Creating a translator application for the Android operating system
In this section, we also want to design an application that can show the translation of letters and the keyboard numbers entered by the user. Displays the equivalent number of letters that the user presses.
What is needed to build this application:
- Windows 7 or higher
- Visual Studio 2015 version
- The latest version of Xamarin in Visual Studio
- GenyMotion emulator
1- First, we must open Visual Studio and go to File- New-Project to create a project.
2- Select the Blank App Android option that appears in the New Project window to create and design an app for the Android operating system( in Slovak: navrhnite aplikáciu pre operačný systém Android.)
3- Then we must open the Resources folders, then the Layout folder for this Solution, and there we double-click on Main.axml.
4- Then, in the Xamarin design window that opens for us, we must search for the tool and the text option in the toolbox.
5- Among the results that are shown to us, we must select Text Large and add it to our design page. Enter a Phoneword. Then select the Enter key from the keyboard and press.
6- Like the previous method, we insert another tool called Plain Text into the design page.
7- Go to the Properties panel and page again and enter @ + id / PhoneNumberText for the id property of this tool and select the text property of this tool 1-855-XAMARIN.
8- Then, we must add another tool called Button to the page. The id property of this tool is equal to @ + id / TranslateButton, and we change its text property to Translate.
9- Then, we must insert another tool called TextView into the page. The value @ + id / TranslatedPhoneWord must be set for the id property of this tool. The text property of these tools is empty strings.
10- Save the Project and the applied changes by pressing Ctrl + S.
11- Right-click on the Project so that we can create a new class and enter the codes and related commands in that class.
12- By right-clicking on the Project, select Add new item, and then select the Code File. It is better to call it PhoneTranslator.cs.
13- We enter the following codes in the created class:
14- I save the Project (we can also save it by pressing the combination keys.)
15- Then, we must find the OnCreate function in the MainActivity section.
16- Then, we must insert the Button under base.OnCreate bundle and inside the relevant OnCreate function.
17- After calling the SetContentView function, you must put the following code in the OnCreate function:
18- The codes related to the translation of the keys pressed by the user are as follows:
19- Save the Project by pressing Ctrl + Shift + S.
20- After saving, we must build the Project. We can do this by pressing CTRL + Shift + B.
21- You may then encounter an error that may be caused by the namespace value not matching the project name in MainActivity.cs.
22- Create another feature using Label in MainActivity.cs. This is the feature that will display the relevant message at the top of the app to the user when running.
23- Run the app in the GenyMotion emulator.
DotNek Android development services