Hello, Android multiscreen
Handling Navigation in Xamarin.android
In these two guides, we'll expand the Phoneword app we created earlier to handle the second screen. This chapter mainly introduces the construction of some basic modules of Android, and also explains the architecture of Android in depth, so that we can better understand the structure and function of Android.
Hello, Android's multi-screen quick Start
In the walkthrough section of this guide we will add a call log function to our Phoneword application, i.e. add a second screen. The final application will have a second screen showing the call log as shown in:
In the accompanying in-depth section, we'll look back at the applications we've built, navigation, and other new Android concepts we've encountered on our way forward.
Let's get started!
Requirements
Since this guide is Hello, Android's follow-up, before starting this chapter you should first complete Hello, Android QuickStart. If you want to go directly to the following walkthrough, you can download the completed version of Phoneword (from Hello, Android QuickStart) and use it to start the walkthrough.
Walkthrough
In this walkthrough we will add a call history screen to our Phoneword application.
1. Let's open the Phoneword application in Visual Studio: & nbsp , &NB Sp , &NB Sp , &NB Sp , &NB Sp , &NB Sp , &NB Sp 2. let's start by editing the user interface. To Open the Main.axml file from Solution Explorer: 3. From the Toolbox, drag the button onto the design surface and place it under the call button:4.in the Properties pane, change the button ID to @+id/callhistorybutton:5.Let's set the button's Text property to @string/callhistory. The Android Design interface will display the string we entered as it is, but we will make some changes so that the button's text is displayed correctly: 6.Expand the Values folder under the Resources folder in Solution Explorer, double-click the string resource file, strings.xml:
7. Set the string name and value of Callhistory in Strings.xml and save it. <?xml version= "1.0" encoding= "Utf-8"? ><resources><string name= "Callhistory" >call History</ String></resources> Call History button text should be updated to reflect the new string value:8. Select the Call History button on the design surface, locate the enabled setting in the Properties pane, and set its value to False to disable the button. the buttons on the design surface will turn gray: 9.Let's create another activity to drive a second screen. in Solution Explorer, right-click the Phoneword project, select Add > New project ...: 10.in the Add New Item dialog box, select Visual C # > Activity modified file named CallHistoryActivity.cs:11. Replace the template code in CallHistoryActivity.cs with the following:
usingSystem;usingSystem.Collections.Generic;usingAndroid.app;usingAndroid.os;usingAndroid.widget;namespacephoneword{[Activity (Label="@string/callhistory")] Public classcallhistoryactivity:listactivity {protected Override voidOnCreate (Bundle bundle) {Base. OnCreate (bundle); //Create your application here varPhonenumbers = Intent.Extras.GetStringArrayList ("phone_numbers") ??New string[0]; This. ListAdapter =Newarrayadapter<string> ( This, Android.Resource.Layout.SimpleListItem1, phonenumbers); } }}in this lesson, we're going to create a Listactivity object and implement its code for populating the data, so we don't need to create a new layout file for this activity. we will discuss this issue in the Hello, Android multiscreen deep Dive section. 12.in our application, we want to get the phone number that the user dialed on the first screen, and then pass them to the second screen. we want to store the phone number as a collection of strings. to support the list, add the following using directives to the top of the Mainactivity class:
using System.Collections.Generic;
Next, let's create an empty list to hold the phone number. our Mainactivity class will be as follows:
" Phoneword " true " @drawable/icon " )] publicclass mainactivity:activity { static ReadOnly list<stringnew list<string>(); ... // OnCreate, etc. }
let's deal with the call History button. in the Mainactivity class, add the following code to register and get the button:
Button Callhistorybutton = findviewbyid<button> (Resource.Id.CallHistoryButton); + = (sender, E) = = { varnew Intent (thistypeof( callhistoryactivity)); Intent. Putstringarraylistextra ("phone_numbers", phonenumbers); StartActivity (intent); };
14.we want to extend the function of the Invoke button, and when the user dials the new number, add the phone number to the number list and enable the Call log button. let 's modify the Neutral code in the box:
Calldialog.setneutralbutton ("Pager",Delegate { //Add dialed number to list of called numbers.Phonenumbers.add (Translatednumber); //enable the Call History buttoncallhistorybutton.enabled =true; //Create Intent to dial phone varCallintent =NewIntent (Intent.actioncall); Callintent.setdata (Android.Net.Uri.Parse ("Tel:"+translatednumber)); StartActivity (callintent); });Save and build the application to make sure there are no errors. 15.deploy the application to the emulator or device. The following is the result of the Phoneword application running in Xamarin Android player: congratulations on your first time completing the multi-screen xamarin.android app! now it's time to dissect the skills and we'll explain them in the next section .
Hello, Android multi-screen version