DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Integrating PostgreSQL Databases with ANF: Join this workshop to learn how to create a PostgreSQL server using Instaclustr’s managed service

Mobile Database Essentials: Assess data needs, storage requirements, and more when leveraging databases for cloud and edge applications.

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • Android Cloud Apps with Azure
  • How To Use Constraint Layout in Xamarin.Android
  • An Introduction to LUIS (Language Understanding Intelligent Service)
  • Configuring SSO Using WSO2 Identity Server

Trending

  • OneStream Fast Data Extracts APIs
  • Five Free AI Tools for Programmers to 10X Their Productivity
  • How To Optimize Feature Sets With Genetic Algorithms
  • Harnessing the Power of In-Memory Databases: Unleashing Real-Time Data Processing
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Deployment
  4. Xamarin: Android Activities, Context, Intents and Views

Xamarin: Android Activities, Context, Intents and Views

Marco Siccardi user avatar by
Marco Siccardi
·
Mar. 11, 14 · Interview
Like (1)
Save
Tweet
Share
30.26K Views

Join the DZone community and get the full member experience.

Join For Free

xamandroid

if you are coming from c#/silverlight, android can be a little diffusing. android does not have the same structure than a windows or windows phone app.

activities

android has activities. activities are the key classes of android were all actions take place. unlike other apps or programs, you do not have a “main” program that is your starting point when launched.

in android, the starting point is an activity. the activity needs to be declared as the starting point. when you start a new project in xamarin, an activity called “mainactivity” gets created automatically.

this activity has some attributes:

[activity (label = "gettingstarted", mainlauncher = true)]

the ‘label’ attribute is what you will see in the title bar when launching the app. the attribute ‘mainlauncher=true’ tells the application to start from here. think of this as your mainpage.xaml.cs in a windows phone app.

every activity has its own oncreate event, where you can put all your starting, button handlers, stylings etc. in.

but an activity still has more. it has events like onstart(), onpause(), onrresume() and onstop() and ondestroy() and onrestart(). i won’t get into deep this time, as the xamarin documentation has already a very good overview of those events: http://docs.xamarin.com/guides/android/application_fundamentals/activity_lifecycle/ .

those events are important to understand for a lot of your application logic, like:

  • saving data that has to be persistent
  • starting and pausing animations
  • register and unregister external events
  • fetch data that are passed from other activities
  • and more (we will cover some of them in my further posts)

i absolutely recommend to go to the link above to read and understand those events.

context

the context is often needed in an android application and allows your code to be run within an activity.

the context allows you for example:

  • to access android services,
  • to access application resources like images or styles,
  • to create views
  • to assign gestures to an activity

without context, often code does not get accepted by the ide or makes your code getting ignored while running the app. luckily, all methods that i used so far have been telling me if they want a context, so you need only learn to find out if you need context for the activity resources or application resources.

intents

but what if we want to have another page (like a separate about page for example)? how can we navigate between pages or call external functions?

this is what intents are for. intents are sending messages through the application if another function needs to be started, like the launch of a second page. within these intents, we have declare the actions that the app needs to do, and if we need a callback, this will also be passed with an intent.

let’s hold this high level, here are some lines of code that navigate to a second page (activity):

var second = new intent(this, typeof(secondactivity)); startactivity(second);

with this code, we are creating an new intent with context to our current running activity to launch the secondactivity.

to send data between activities, we use the putextra() method of intents:

var second = new intent(this, typeof(secondactivity)); 

second.putextra("firstpage", "data from first page"); 

startactivity(second);

of course, we need also some code to read the passed data on our second page:

intent.getstringextra("firstpage") ?? “data not available”;

we could now use this data on our second page by assigning it to a control or function.

passing data between activities works similar to passing querystrings in navigationsservice.navigate() method on windowsphone, so you should get familiar with it very fast.

views

the last part that is very important are views. in a view you declare how the activity looks like. think of it as your mainpage.xaml page in a windows phone app.

views can be very different, too. let’s start with the simple view. in our getting started project, we also have a layout file that holds our first view:

<?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" />
</linearlayout>

the view has a main linearlayout which is acts as  contentpanel. all other controls like buttons, textviews etc. are going into this. there are a lot of properties that can be set, we’ll leave it this easy for now. if you want to know more about those properties, you can have a look at the android documentation here: http://developer.android.com/guide/topics/ui/declaring-layout.html

this part is totally the same as with the one matching part in the android sdk, so you will need to get familiar with the android documentation as well.

to make a view visible, we need to assign it to an activity. in our sample app, we are doing this with this line of code in our oncreate event:

setcontentview (resource.layout.main);

this is the easy way for views. but there are more complex views in android, too.

views are also used in listviews, where you declare the look of the items and the list in a layout file, or also if you use the actionbar with a tab navigation, and also in the menu of an actionbar. i will cover all of these in my further blog posts, as we also need adapters to get data binded to views.

i hope you this post helps you to understand  the high level structure of an android application.

until the next time, happy coding!

Android (robot) Intent (military) xamarin app application Windows Phone

Published at DZone with permission of Marco Siccardi, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Android Cloud Apps with Azure
  • How To Use Constraint Layout in Xamarin.Android
  • An Introduction to LUIS (Language Understanding Intelligent Service)
  • Configuring SSO Using WSO2 Identity Server

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: