Android 4 and Fragments: Listing 3

DetailsFragment class.

    internal class DetailsFragment : Fragment
    {
        public static DetailsFragment NewInstance(int playId)
        {
            var detailsFrag = new DetailsFragment { Arguments = new Bundle() };
            detailsFrag.Arguments.PutInt("current_play_id", playId);
            return detailsFrag;
        }
        public int ShownPlayId
        {
            get { return Arguments.GetInt("current_play_id", 0); }
        }
        public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            if (container == null)
            {
                // Currently in a layout without a container, so no reason to create our view.
                return null;
            }
            var ll = new LinearLayout(Activity);
            var scroller = new ScrollView(Activity);
            var text = new TextView(Activity);
            var iv = new ImageView(Activity);
            var padding = Convert.ToInt32(TypedValue.ApplyDimension(ComplexUnitType.Dip, 4,
Activity.Resources.DisplayMetrics));
            text.SetPadding(padding, padding, padding, padding);
            iv.SetPadding(padding, padding, padding, padding);
            text.TextSize = 24;
            text.Text = StarTrek.Quotes[ShownPlayId];
            iv.SetImageResource(StarTrek.Images[ShownPlayId]);
            ll.Orientation = Orientation.Vertical;
            ll.AddView(iv);
            ll.AddView(text);
            scroller.AddView(ll);
            return scroller;
        }
    }

About the Author

Wallace (Wally) B. McClure has authored books on iPhone programming with Mono/Monotouch, Android programming with Mono for Android, application architecture, ADO.NET, SQL Server and AJAX. He's a Microsoft MVP, an ASPInsider and a partner at Scalable Development Inc. He maintains a blog, and can be followed on Twitter.

comments powered by Disqus

Featured

  • VS Code Java Team Improves 'Getting Started' Experience

    Microsoft's dev team responsible for the Java on Visual Studio Code extensions released a new update that eases the "getting started" experience, addressing feedback from new users who want an easier onramp.

  • Data Prep for Machine Learning: Encoding

    Dr. James McCaffrey of Microsoft Research uses a full code program and screenshots to explain how to programmatically encode categorical data for use with a machine learning prediction model such as a neural network classification or regression system.

  • Surface Duo Debut Presents Dual-Screen Dev Challenges

    Microsoft officially launched its new dual-screen Android device, Surface Duo, presenting new challenges -- and opportunities -- for developers to leverage the new form factor.

  • What's New in Blazor Tooling Updates

    Here's a quick look at what four major third-party Blazor tooling vendors have offered lately for Microsoft's red-hot project that allows for web development with C# instead of JavaScript.

  • Xamarin.Forms 4.8 Ships with Experimental Drag & Drop, Gradients/Brushes

    Microsoft shipped Xamarin.Forms 4.8 with two new experimental features leading the improvements to the C#-based cross-platform UI toolkit: gradients/brushes for "painting" in an app and drag-and-drop functionality within an app.

.NET Insight

Sign up for our newsletter.

Terms and Privacy Policy consent

I agree to this site's Privacy Policy.

Upcoming Events