Populating simple objects

For this short recipe, we will review some code samples we saw before without paying attention to them. Indeed, we will simply populate the TextView and TextLabel elements.

Getting ready

Create a new solution named populating GUIs object and then open the project of the same name that Xamarin Studio has created for you.

How to do it...

  1. Open the Main.xaml file under the Ressources/layout option:
    <?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">
    <Button
        android:id="@+id/myButton"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
      android:text="@string/hello" />
      <EditText
        android:inputType="date"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
      android:id="@+id/editText1" />
    </LinearLayout>

    If you remember from Chapter 3, Building a GUI, where we learned the different available layouts and how to build a GUI, we will see that we simply create a linear layout in which each element comes after the others. We also add a button and an editable text box.

  2. Add the following code as a replacement of the onCreate() method in the Main.cs file:
    protected override void OnCreate (Bundle bundle) {
      base.OnCreate (bundle);
      // Set our view from the "main" layout resource
      SetContentView (Resource.Layout.Main);
      // Find the button and change the text
      Button button = FindViewById<Button> 
      (Resource.Id.myButton);
      button.Text = "My Button";
      // A textView with a Date Format
      TextView textView = FindViewById<TextView> 
      (Resource.Id.editText1);
      textView.Text = "04/04/13";
    }
  3. Run the program.

    Heres a screenshot of what you should have on your emulator:

    How to do it...
  4. As simple as that, we populate a button and a TextView element with data.

How it works...

We learned in Chapter 2, Mastering the Life and Death of Android Apps, that if the OnCreate() method is one of the methods called toward the display of an activity and the increase() method happened before that, the activity is actually rendered to the clients. Therefore, in the onCreate() method, we have the possibility to manipulate the default look and feel of our GUIs before their first display to the user.

In the preceding code, we first assign the Main.xaml file as the layout of our activity just after calling the default constructor of the Activity class. Then, we use the generic method FindViewById<Button> in order to obtain references to our Button and TextView elements. Finally, we use the Text property to update the text they are displaying.

There's more...

In this There's More... section, we will showcase each component.

Labels

You can reuse the exact same code to update the values of classical labels such as EditableTextView, a specialization of TextView that is the class used for the label.

Retrieve the text

Obviously, text is a get set enabled property. Therefore, you can use the following to retrieve the text:

TextView textView = FindViewById<TextView> 
(Resource.Id.editText1);
var text = textView.Text;

Know if the text has been modified

We can supercharge the TextChanged() method of any TextView element in order to add some behavior when the text is changed.

textView.TextChanged += (object sender, 
Android.Text.TextChangedEventArgs e) => {
  // Some actions
};

See also

Check out the next recipe to see how to populate the datepicker.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.135.217.228