Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Xamarin Blueprints

You're reading from   Xamarin Blueprints Leverage the power of Xamarin to create stunning cross-platform and native apps

Arrow left icon
Product type Paperback
Published in Sep 2016
Publisher Packt
ISBN-13 9781785887444
Length 516 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Michael Williams Michael Williams
Author Profile Icon Michael Williams
Michael Williams
Arrow right icon
View More author details
Toc

Table of Contents (9) Chapters Close

Preface 1. Building a Gallery Application FREE CHAPTER 2. Building a SpeechTalk Application 3. Building a GPS Locator Application 4. Building an Audio Player Application 5. Building a Stocklist Application 6. Building a Chat Application 7. Building a File Storage Application 8. Building a Camera Application

Adding the Android photo screen

Implementing a photo view for cell selections is very similar, although with Android we will be using an intent to create a new activity, which in turn will inflate a new view to display the image and details. Let's start by adding a new XML called photo_view.xml, and paste in the following code:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:weightSum="4"> 
    <LinearLayout 
        android:orientation="vertical" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:layout_weight="1"> 
        <ImageView 
            android:id="@+id/image_photo" 
            android:scaleType="centerCrop" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:adjustViewBounds="true" /> 
    </LinearLayout> 
    <LinearLayout 
        android:orientation="vertical" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:layout_weight="3" 
        android:weightSum="2"> 
        <TextView 
            android:id="@+id/title_photo" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:layout_weight="1" /> 
        <TextView 
            android:id="@+id/date_photo" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:layout_weight="1" /> 
    </LinearLayout> 
</LinearLayout> 

The layout is very much the same as the custom_cell.xml sheet, although we are going to stack items vertically and set the following two properties to keep the correct image aspect ratio:

android:adjustViewBounds="true" 
android:scaleType="centerCrop" 

Note

Make sure XML sheets do not contain the same IDs as any other XML sheet.

Now that we have our user interface for the PhotoActivity, let's add the new activity:

[Activity (Label = "Gallery.Droid", Icon = "@mipmap/icon")] 
    public class PhotoActivity : Activity 
    { 
        /// <summary> 
        /// Raises the create event. 
        /// </summary> 
        /// <param name="savedInstanceState">Saved instance state.</param> 
        protected override void OnCreate (Bundle savedInstanceState) 
        { 
            base.OnCreate (savedInstanceState); 
 
            // Set our view from the "main" layout resource 
            SetContentView (Resource.Layout.Photo); 
 
            var imageData = Intent.GetByteArrayExtra ("ImageData"); 
            var title = Intent.GetStringExtra ("Title") ?? string.Empty; 
            var date = Intent.GetStringExtra ("Date") ?? string.Empty; 
 
            // set image 
            var imageView = FindViewById<ImageView> (Resource.Id.image_photo); 
            BitmapHelpers.CreateBitmap (imageView, imageData); 
 
            // set labels 
            var titleTextView = FindViewById<TextView> (Resource.Id.title_photo); 
            titleTextView.Text = title; 
            var dateTextView = FindViewById<TextView> (Resource.Id.date_photo); 
            dateTextView.Text = date; 
        } 
    } 

Looking at this new activity, what can we see? Notice the attributes at the top:

[Activity (Label = "Gallery.Droid", Icon = "@mipmap/icon")] 

There is no MainLauncher tag because this is not our starting activity. We then add the intent.GetExtras for the image data and strings required to display on our Photo interface.

Now we need to make one addition to the ListAdapter class:

public GalleryItem GetItemByPosition (int position) 
{ 
     return _items[position]; 
} 

When an item in the list is selected, we need to be able to access the selected GalleryItem. Our next step is to add the ItemClick delegate for the ListView. Open up the MainActivity class and add the following to the OnCreate function:

listView.ItemClick += (object sender, AdapterView.ItemClickEventArgs e) =>  
            { 
                var galleryItem = adapter.GetItemByPosition (e.Position); 
                var photoActivity = new Intent(this, typeof(PhotoActivity)); 
                photoActivity.PutExtra ("ImageData", galleryItem.ImageData); 
                photoActivity.PutExtra ("Title", galleryItem.Title); 
                photoActivity.PutExtra ("Date", galleryItem.Date); 
                StartActivity(photoActivity); 
            }; 

Place this after we set the list adapter. When an item is clicked, we simply pull out the gallery item from our adapter by the position passed from the ItemClickEventArgs. Once we have the gallery item, we create the new PhotoActivity intent and pass the extras.

That is all; run the application and play around selecting cells to display the PhotoActivity.

You have been reading a chapter from
Xamarin Blueprints
Published in: Sep 2016
Publisher: Packt
ISBN-13: 9781785887444
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image