My first Mono for Android app

Source: Internet
Author: User
Document directory
  • Prepare the development environment
  • Create a Mono for Android Application
  • Summary
My first Mono for Android app

Mono for Android: Learn how to use Mono for Anrdoid to create an android app, how to use Intent to start an Activity, and how to pass parameters between the activities.

Prepare the development environment to download and install Mono for Android

It is easier to prepare the Mono for Android development environment in The OSX system. As long as you download an online installation program from Xamarin, the installation program automatically downloads and installs all the files, it even includes JDK and Android SDK. There is nothing to say, but it is very simple.

Configure the Android Simulator

Start MonoDevelop and click "Open AVD Manager" under the Tools menu. "Android Virtual Device Manager" is started to create a new Android Virtual Device named Droid4.1, select 4.1 for Target, 256 for SD, and WXGA720 for Skin. For more information, see the Google documentation.

Note that you need to add a hardware option GPU emulation and set it to true. Enabling GPU simulation can speed up the running of the simulator. Otherwise, the running of the simulator will be really slow.

After the creation, run the simulator to confirm that all configurations are normal. The entire development environment is ready.

Create a Mono for Android Application

Open MonoDevelop, select create solution, select Mono for Android on the left, and select Mono for Android Application on the right. Use the default template to create a Mono for Android Application, as shown in, the project name is "MyFirstApp ".

Familiar with default project templates

Now, do not do anything else. First familiarize yourself with this project. Open the Project Properties dialog box to see what settings are available for each node. Focus on the following nodes:

  • Build/General, select Target Framework, and set the Android SDK version used to compile the application;
  • Build/Mono for Android Build, such as Linker, deployment method, CPU architecture under the advanced tag, and internationalization;
  • Build/Mono for Android Application, and set Application information, that is, information of the AndroidManifest. xml file;

The default directory structure of the project is as follows:

Note the Assets and Resource directories:

AssetsDirectory. If the application needs to use binary resource files, such as special fonts and sounds, put them in this directory and set BuildAction to AndrioidAsset, the resources will be deployed together with the application, you can use a code similar to the following to access AssetManager during runtime:

public class ReadAsset : Activity{    protected override void OnCreate (Bundle bundle)    {        base.OnCreate (bundle);        InputStream input = Assets.Open ("my_asset.txt");    }}

In addition, the font file can be loaded as follows:

Typeface tf = Typeface.CreateFromAsset(    Context.Assets, "fonts/samplefont.ttf");

ResourceDirectory, including the images, layout descriptions, binary files, string dictionaries, and other resource files required by the application. For example, a simple Android application contains an interface description file (main. axml), an international string Dictionary (strings. xml) and the icon (icon.png), these files are stored in the "Resource" directory according to the following structure:

Resources/   drawable/      icon.png   layout/      main.axml   values/      strings.xml

To enable the compilation system to recognize Android resources from Resource files, you need to set the Build Action to "Android Resource ". After the preceding directory structure is compiled, a file similar to the following will be generated:

public class Resource {    public class Drawable {        public const int icon = 0x123;    }    public class Layout {        public const int main = 0x456;    }    public class Strings {        public const int FirstString = 0xabc;        public const int SecondString = 0xbcd;    }}

UseResource.Drawable.iconYou can reference the drawable/icon file,Resource.Layout.mainYou can reference the/layout/main. axml file and useResource.Strings.FirstStringYou can reference the first string in the values/strings. xml file.

These are similar to those described in the Android SDK documentation. In the Mono for Android environment, some additional information is added. net-specific style, for experienced. net developers can understand it at a glance.

Create Activity and View

Different from applications on other platforms, applications on these platforms usually have a single main function of entry. Applications are started by this function to create windows and maintain interfaces. The Android program is different. An Android program consists of interfaces provided by some loose activities, so it looks a bit like a Web application. Any Activity can be started through a URL.

Create an Activity, select File> New> File from the menu bar, and select Android Activity in the pop-up New File dialog box, as shown in:

The code for the newly created Activity is as follows:

[Activity(Label = "MyFirstApp", MainLauncher = true)]public class MainActivity : Activity {    protected override void OnCreate(Bundle bundle) {        base.OnCreate(bundle);    }}

Note that the ActivityAttribute mark of MainActivity specifies two attributes,Label="MyFirstApp"Display name of Activity,MainLauncher=trueIt is displayed in the Application List. during compilation, Mono for Android will generate an AndroidManifest. xml based on these tags and package them into the final Android Application.

Now, create the MainActivity view. First, select the Resources/layout directory of the project, choose File> New> File on the menu bar, and select Android Layout in the pop-up New File dialog box, as shown in:

Enter MainActivityLayout as the file name. MonoDevelop opens the design view by default. Switch to the Code view and paste the following code:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="horizontal"    android:layout_width="match_parent"    android:layout_height="match_parent">    <EditText        android:layout_width="0dp"        android:layout_height="wrap_content"        android:id="@+id/MessageEditText"        android:layout_weight="1"        android:hint="@string/MessageEditTextHint" />    <Button        android:text="@string/SendButtonText"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:id="@+id/SendButton" /></LinearLayout>

Switch to the design view, as shown in:

Add the following code under base. OnCreate (bundle) in the MainActivity. cs file to enable MainActivity to use MainActivityLayout:

this.SetContentView(Resource.Layout.MainActivityLayout);

In the same way, the code and design interface for creating SecondActivity, SecondActivityLayout, and SecondActivityLayout are as follows:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent">    <TextView        android:text="Large Text"        android:textAppearance="?android:attr/textAppearanceLarge"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:id="@+id/MessageTextView" /></LinearLayout>

Start Activity with Intent and PASS Parameters

If you run the program now, you can only see MainActivity, but not SecondActivity. If you want to start SecondActivity, you need to use Intent. Android uses Intent to start the Activity and transmit parameters between the activities.

Open MainActivity and add some code to make it look as follows:

[Activity (Label = "MyFirstApp", MainLauncher = true)] public class MainActivity: Activity {public const string ExtraMessage = "Cn. beginor. myFirstApp. mainActivity. extraMessage "; protected override void OnCreate (Bundle bundle) {base. onCreate (bundle); // sets the layout file this. setContentView (Resource. layout. mainActivityLayout); var sendBtn = this. findViewById <Button> (Resource. id. sendButton); // adds the event processing function sendBtn for the send button. click + = SendButtonClick;} void SendButtonClick (object sender, EventArgs e) {// obtain user input information var msgEditText = this. findViewById <EditText> (Resource. id. messageEditText); if (msgEditText = null) {return;} var msg = msgEditText. text; // create an Intent and pass the user input information var intent = new Intent (this, typeof (SecondActivity); intent. putExtra (ExtraMessage, msg); // start the second Activity this. startActivity (intent );}}

Open SecondActivity and add the code to receive the ExtraMessage and display it:

Protected override void OnCreate (Bundle bundle) {base. onCreate (bundle); // sets the layout file this. setContentView (Resource. layout. secondActivityLayout); // obtain ExtraMessage var Intent = this from intent. intent; var msg = intent. getStringExtra (MainActivity. extraMessage); // display ExtraMessage on TextView var textView = this. findViewById <TextView> (Resource. id. messageTextView); textView. text = msg ;}

Run this program now. The MainActivity is started first, and the display interface is as follows:

Click Send to start SecondActivity and display the input information on the interface:

Summary

Mono for Android has a good first-time experience. for experienced. Net developers, the speed of getting started is very fast. You just need to learn a little about the Android UI. The MonoDevelop interface is similar to VS, and it is easy to get started. In other words, the biggest benefit of Mono for Android is that it can use the existing one. net code, CodePlex, and Github have a wealth of resources to use, if you are familiar. net development, Mono for Android is also worth a try.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.