Page 1 of 1

Android, Part I: Welcome to Android

#1 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1377
  • View blog
  • Posts: 10,951
  • Joined: 06-February 08

Posted 05 October 2009 - 11:07 AM

*
POPULAR

Welcome to Android. We hope you enjoy your stay.

So, first off, a bit of an introduction. This is the first part of (hopefully) many tutorials related to the android platform. In this tutorial, we're only going to cover the very basics: how to design your application, and how to utilise the XML layouts to design your user interface.

Since this series of tutorials is more directed at the code, I'm not going to cover setup & installation. Instead, here's a link to get yourself set up with the SDK and an emulator.

Personally, I've never really been a huge fan of Java. And when I first tried Android, I wasn't too keen. But it's come a long way since it was first released. Once you get into the android SDK, it begins to get really interesting. Plus, the idea of earning a little bit of cash on the side for designing a little app for your phone has quite a nice appeal. :)

The market
The market is awesome. It's extremely easy to release and update your applications, and I'll be writing a tutorial all about publishing your apps, how to sign them, how to create a jar keystore, all the fun stuff.

Get started already!!1!one!
Okay. Enough intro, let's get to some development! :)

...sorry. No programming yet. We're going to be reading the project template, and I'll be explaining it line for line. It's best to take this stuff slow at first, because it's so different to a lot of "regular" projects. But at least there's code! :)

Create a project, you can call it anything, but I'm going to be following this structure along the series of tutorials:
Attached Image

layout.xml
The layout.xml is... sorta important. It's a great way to design your user interfaces, but it's not always necessary: you can create your interfaces through pure Java, which is personally my preferred method... but it's still good to know how to use the xml layouts. They help to keep the code for your activity more organised. (Coming from a C++/wxWidgets background, I'm in the habit of creating my programs through pure code, you may prefer the XML process)

Right. Enough rambling. I've analysed the ./res/layout/main.xml, and broken it up, line by line.

yep: we just use standard XML to declare the layout:
<?xml version="1.0" encoding="utf-8"?>

We use a LinearLayout to define our layout as being, well... linear:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

Next up, we have some properties:
	android:orientation="vertical"
	android:layout_width="fill_parent"
	android:layout_height="fill_parent"

Note the three properties:
android:orientation -- which direction the layout is filled. Contrary to what you may think, this isn't based on the angle of the phone. Using "vertical" simply means "add widget number 2 below widget number 1, add widget number 3 below widget number 2, ... add widget number n below widget number n-1". :)
android:layout_width -- the width of the object. We're going to use "fill_parent" to fill the width of the screen
android:layout_height -- the height of the object. We're going to use "fill_parent" again, to fill the height of the screen.
Lastly, we're going to close the LinearLayout item. Note we want more items inside the LinearLayout, so we're just going to use > instead of />
	>


Next, we're going to define a TextView. The TextView is like a Label in .NET, or a wxStaticText in wxWidgets. It basically puts a static piece of text on the screen.
<TextView
	android:layout_width="fill_parent"
	android:layout_height="wrap_content"
	android:text="@string/hello"
	/>

You'll notice it's pretty similar to how we defined the LinearLayout. Differences:
android:layout_height="wrap_content" -- the TextView will be resized according to the text it displays. The longer the string, the taller the View.
android:text="@string/hello" -- the string to display. @string refers to the ./res/values/strings.xml file. (More on this later)
/> -- this time, we're not putting any other items inside it, so we can close it off XML stylee. We could just as easily use
<TextView ... ></TextView>
but it seems a little pointless here. :)

Since that's all we want in the LinearLayout, we're going to close our layout off:
</LinearLayout>


strings.xml
This one's much simpler. Put simply, this file contains strings which are built in to the application at compile time. Note the hello string, which we referenced in our TextView in ./res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
	<string name="hello">Hello World, dic_tut1!</string>
	<string name="app_name">dic_tut1</string>
</resources>


The Java!
Lastly, we're going to put our layout together in the code. I've commented this bit up, so it's easy for you to understand.
package dreamincode.tutorials.part_one;

import android.app.Activity; // Activity: the "main" part of the application
import android.os.Bundle; // Bundle: stores a "saved state" for your app to remember how it was when moved to the background

public class dic_tut1 extends Activity { // inheriting the Activity class
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) { // consider this the "entry point" (main) of your application
		super.onCreate(savedInstanceState); // call the super to restore the state (does the work for you)
		setContentView(R.layout.main); // set the main content view to ./res/layout/main.xml
	}
}


That's all for now!
There's obviously more to it than this, things such as the AndroidManifest.xml file, but I'm not going to go into that now. We'll dive into that as and when we need it, it's really rather trivial and honestly nothing to worry about. :)

** EDIT **
I'll also attach the projects to each tutorial, so you can play with it yourself. :)
Attached File  dic_tut1.zip (24.25K)
Number of downloads: 972

This post has been edited by gabehabe: 06 October 2009 - 10:01 AM


Is This A Good Question/Topic? 18
  • +

Replies To: Android, Part I: Welcome to Android

#2 avacomputers  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 03-April 09

Posted 19 August 2010 - 12:05 PM

Thanks for an awesome write up.
Was This Post Helpful? 0
  • +
  • -

#3 harris700  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 21-May 10

Posted 23 September 2010 - 11:51 AM

Thanks for the tutorial. What IDE are you using? Eclipse or??
Was This Post Helpful? 0
  • +
  • -

#4 htc(hot-taco-cheese)  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 17
  • View blog
  • Posts: 71
  • Joined: 04-December 10

Posted 04 December 2010 - 07:15 PM

View Postharris700, on 23 September 2010 - 10:51 AM, said:

Thanks for the tutorial. What IDE are you using? Eclipse or??

Answering a bit late but, he is using Eclipse. You can tell by the icon in the first screen shot he posted in this thread.

This post has been edited by htc(hot-taco-cheese): 04 December 2010 - 07:15 PM

Was This Post Helpful? 0
  • +
  • -

#5 Guest_Aparna*


Reputation:

Posted 19 December 2010 - 10:13 PM

Great tutorials. Very easy to follow and the way it has been rendered keeps a reader going. Really enjoying the "stay"!

Thanks.
Was This Post Helpful? 0

#6 creativecoding  Icon User is offline

  • Hash != Encryption
  • member icon


Reputation: 926
  • View blog
  • Posts: 3,204
  • Joined: 19-January 10

Posted 27 December 2010 - 06:45 PM

Ahh, thank you so much! The documentation from Android is very bad IMO...

Also, can I change setContentView anytime, like on a button press?
Was This Post Helpful? 0
  • +
  • -

#7 sas1ni69  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 85
  • View blog
  • Posts: 431
  • Joined: 04-December 08

Posted 05 January 2011 - 11:35 PM

View Postcreativecoding, on 28 December 2010 - 08:45 AM, said:

Ahh, thank you so much! The documentation from Android is very bad IMO...

Also, can I change setContentView anytime, like on a button press?


Very true.

Thanks for the tut.
Was This Post Helpful? 0
  • +
  • -

#8 winstrol  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 20-November 11

Posted 20 November 2011 - 10:18 PM

you employ a great blog here! would you like to have the invite posts in my weblog?
Was This Post Helpful? 0
  • +
  • -

#9 javaChimp  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 19-November 12

Posted 21 November 2012 - 09:57 PM

awesome!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1