The default template for Android projects has a little more built-in functionality than iOS. Android user interface layouts are defined in XML files that are readable by humans and editable. However, Xamarin Studio has provided an excellent design tool that allows you to drag-and-drop controls to define your Android layouts. Let's add some more features to your application and start using the Android designer.
Return to Xamarin Studio and carry out the following steps to add features to your app:
Main.axml
.Count: 0
into the label.
value is set to @+id/textView1
. Let's change it to @+id/myText
to be consistent with the button.Add
.Your Android application will now look identical to the changes you made in the designer, as follows:
Now, let's interact with the new label from the code. Switch back to Xamarin Studio and open MainActivity.cs
. Let's modify the activity to interact with the TextView field instead of the button. We use the FindViewById
method to retrieve a view by the ID we set up in the layout file. Xamarin Studio has also autogenerated a static class named Resource
for referencing your identifiers.
So let's retrieve the instance of the TextView field by placing this code in OnCreate
as follows:
TextView text = FindViewById<TextView>(Resource.Id.myText);
The Resource
class is a static class that the Xamarin designer will populate for you. For future reference, you may have to build your Android project for new IDs and other resources to show up in your C# files in Xamarin Studio.
Next, let's update the Click
event on the button:
button.Click += delegate { text.Text = string.Format("Count: ", ++count); };
This will rewire the button to update the text in TextView instead of on the button itself. Now if we run the application, we'll get an Android app that functions identically to the iOS one in the previous chapter. The Android app will look like the following screenshot:
Since we've added some of our own views to our layout, let's add a second activity to build on our understanding of activities in Android.
Return to Xamarin Studio and perform the following steps:
SecondActivity.cs
.Second.axml
.SecondActivity.cs
and, add the following code to OnCreate
:SetContentView(Resource.Layouts.Second);
SecondActivity.cs
, add the following line of code to the Click
event of your button:StartActivity(typeof(SecondActivity));
Second.axml
and drag a button into the view. Set its text to Finish
, for example, and set its ID to @+id/finish
.SecondActivity.cs
and add the following lines to its OnCreate
method:var finish = FindViewById<Button>(Resource.Id.finish); finish.Click += (sender, e) => Finish();
Your application's button will now launch a new activity in addition to incrementing the count on the label. Once SecondActivity
is visible, you can click on its button to finish the activity and return to the first activity. Down the road, if you need to pass information from one activity to another, you will need to create an Intent
object to pass to StartActivity
. The second activity in your app is shown in the following screenshot:
18.119.107.161