Beginner’s Android project

Objective: I’ll explain I made an Android app that does more than just print Hello World!

  • You will be able to save a list of movies that you have watched
  • Store your ratings along with IMDB/metacritic ratings
  • Get a graphical plot of how your likes correlate with the world’s

Assumption: This series assumes you know Java and have been able to make a “Hello world!” on your own

What will you be exposed to by the end?

  • Intents
  • Menus and lists
  • JSON
  • achartengine (popular charting engine)
  • Asynctasks
  • SQlist DB
  • Few best practices

Why did I make this post?

After completing the very popular course on coursera here – https://class.coursera.org/android-001, I had an inkling of what most words meant but was still clueless about was how everything melded together…why use a Listview instead of a View or a pendingintent in place of an intent

It all becomes clear once you try making something on your own, so let’s get started

STEP 1

Every screen you see on your phone is an Activity

Make a rough sketch of what you would like every screen to look like, this gives you a goal of how to design components

My app will have 4 activities

  • Main screen with 4 buttons to act as the main menu
  • A text field to entry your movie to search and a ratingbar to well…rate it along with IMDb info pulled from a webservice
  • A listactivity to list down all your saved movies with other info pulled form IMDB
  • A scatterchart showing your ratings against IMDb ratings

 STEP 2

Let’s make our main activity

Start by opening these 2 links in other tabs

Look at the layout, it is a linear layout with 4 buttons

Why linearlayout? Because we don’t need any fancy positioning, just 4 simple buttons in middle of the screen

Notice that the layout_gravity is center_horizontal and center_vertical to centralize it on the screen

Everything that we may want to reference in code should have an ID which we have done for all our buttons

Time to look at the activity

Our class extends Activity since it makes up one screen of our app

We implement View.OnclickListener because we want some action to happen when we click…you may ask what action and on what click? That is defined in the onClick method

Notice on line 14, I have defined my buttons globally. That is because the buttons could be used in many places around the class and using findViewById everywhere will be cluttery and wasteful

Why haven’t I assigned the buttons globally? Because they are not created yet…the layout gets created after setContentView along with the buttons and fields

onCreate / initialize

Initial setup of your layout using setContentView passing the layout xml

Assign your view variables using findViewById

Setting a click listener to all the buttons with setOnClickListener. When we click any of these buttons, our onClick method is called which decides what to do with it

onClick

onClick gets passed a view, in our case, it will be one of the 4 buttons we set a listener to

We use a switch/case to decide which button gets clicked and do some action

The first 2 buttons start a new activity by creating an intent, the third button gets an intent from elsewhere (explained later) and the last button uses an asynchronous task (also explained later)

The intent created takes 2 arguments, the context where ‘this’ is sufficient and the target Activity class to start

Advertisements

About nukeu666

Adventurer Otaku Photography bum Geek Gamer Metalhead Jazzhead SF/F head Foodie Broke
This entry was posted in Coding, Uncategorized and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s