Android Application Development with C #

Monodroid and Monotouch are xamarin frameworks that allow you to develop a C # application for Android and iOS, respectively. Since this is a relatively new information technology on the Internet, there are not too many (with the exception of the off site and a large number of topics on stackoverflow.com), in Russian I did not find any tutorials and information in general.

To eliminate this misunderstanding, I decided to write a small tutorial on how to start developing applications for mobile platforms using these frameworks. In this article, I will only consider Monodroid.

image


What do you need to start development?
1) Visual studio C # professional version and higher (go and cracked)
2) The framework itself (and he, in turn, will install Java and a virtual machine for us and everything else)

If everything is clear with the first, then the second is more complicated, as it turned out during use, the free version of the monodroid cannot compile .apk files, so you should either buy it or use a crack from the Internet (which is far from the first page of Google).

Let's start

Running after installing all the necessary studio, we notice new types of projects to create:


Choose Android Application. Several standard directories will be created:


The Assets folder contains all the files used by the program, ala videos, sounds, HTML files, etc. Initially, there is nothing in it except a text box with a description of the directory itself and why it is needed.

In the Drawable folder, you need to place the image files used by the program.

The Layout folder contains GUI files.
The Values ​​folder contains various parameters that can be created and loaded when the application is running (for example, you can remember the login and password there).

After creating an empty project, we can compile it by pressing F5 - the settings of the virtual machine will open with the choice of the device on which to run the test program. If you connect your device on an android with USB debugging enabled (this is in the settings, the “for developer” tab), you can start and test directly on it. I highly recommend that you run tests on a real device. many touch elements cannot be tested on a virtual machine, in addition, I personally deploy the application for quite a while on my virtual machine. It takes about a minute between compilation and launching on a virtual machine.

By default, we have a button, when clicked, it will be displayed how many times we clicked on it. Let's try to do something more interesting.

Open the interface file and try to change it.


Bottom of the 2 tabs is viewing the interface code and the interface itself. On the right are the various components.
I must say right away: using the built-in form riveter is not for the faint of heart. She is so laggy and doesn’t do what you expect is just horror. Instead, you can use the droiddraw.org website after compiling the interface there and clicking on the Generate button can copy the code into the code tab and everything will be ok.

And in addition, the best way is to create an interface from the code, using formulas based on the ratio of screen resolution. Without this, a normal interface that looks attractive on all screens is difficult to create.
However, for now, let's go along a simple path.

There are several types of arrangement of objects on the screen - types of layers. However, many of them can contain each other and get along together. Let's consider of them:

1) LinearLayout - each element is lower than the previous one.
2) RelativeLayout - each element is relative to the previous one (for example, you can set the parameter to be located to the left of button 1, below the text box, indented 40 pixels below button 2, etc. The

settings for each component in our properties window are everything is pretty familiar, and the properties themselves are similar to Winform elements.

Having created a more or less attractive interface, you need to run it somehow.
For this there is an Activity. By default, we have a file called Activity1, in which the class is an inheritor from the Activity class.

The line above the declaration class-
[Activity (Label = "AndroidApplication1", MainLauncher = true, Icon = "@ drawable / icon")]
describes the title of the application window, the icon and whether or not to start this activity when the application starts.
From the main autostarted activity, you can start any other. After the startup of this activity after the application starts, we load the interface with the line SetContentView (Resource.Layout.Main);

To access any element, we must use the FindViewById <> () function; when assigning an instance of the tago class to the element that we need. Specifically, in our example, we see the line

FindViewById (Resource.Id.MyButton);

"MyButton" is the name of the button, you can see it when creating the interface in the code tab.

Through a simple construction Button button = FindViewById (Resource.Id.MyButton);
we can work with the button and process all actions with it. In this case, the click handler looks like this:

button.Click + = delegate {button.Text = string.Format ("{0} clicks!", Count ++); };

Having designed and written the application, I can compile the apk file by going to the build tab and clicking the Package for android button. 2 files will appear in the project folder, one of which is signed with an automatic signature — we can use it to install the device.

On this, I think, this short tutorial can be completed. In the future, most likely I will write a similar tutorial on porting and developing on iOS.

Also popular now: