You are on page 1of 6

Programming Projects II

2019/2020

02. Android and MVC (Model View Controller)


In this chapter we will update our GeoQuiz project to show more than one question to the user. As you may expect,
we will need to create a new data model in order to handle all the questions and answers.

Additional documentation to this guide can be found on The Big Nerd Ranch book, Android Programming (3rd edition). It can be found
on the eStudy folder for the course.
Create a new class called Question.
This class will contain the question itself and the value of the answer (True or False).

The text for the question it will be saved as a resource identifier in order to keep a good project structure.

Create a new class by selecting the project package and right-clicking on it, select the option New > Java Class.

Name: Question
Superclass: No needed on this exercise

Once the class is created, we will define its attributes, constructors and getters/setters.

👨💻 PRO TIP
If you want to create the basic method for your class, you just need to click ALT + Insert on your class. Android Studio will
show you the different options for creating those methods.

Additional documentation to this guide can be found on The Big Nerd Ranch book, Android Programming (3rd edition). It can be found
on the eStudy folder for the course.
With our recently created Question class we have a good start for implementing a proper MVC (Model View
Controller) pattern. If we want to apply it on our project, we need to change some aspects of it.

- Model: Question class


- Controller: MainActivity class
- View: All resources we may design, such as the screens (layout)

From now on, our app flow should follow this structure.

We are now familiar with the MVC pattern. We will apply this concept to our first app, the GeoQuiz.

First step is to add a new button to navigate the next question on our layout.

Last step is to define our pool of questions directly in an array on our class. We will keep track of the displayed
question using an index.

Once the user selects a question, we will track it with the index and display the correct question and answer. When
the user clicks on the next button, we will increase the tracking index and update the displayed information.

Additional documentation to this guide can be found on The Big Nerd Ranch book, Android Programming (3rd edition). It can be found
on the eStudy folder for the course.
Add an icon resource
We want to add an icon inside de next button of our app, let’s try to do it.

All graphics and other items that are not code are considered resources. Images are always created and placed in
the res/drawables directory.

Android works with several pixel density resources. This means, each graphic resource you’ll be using for your app, it
has to be generated in different resolutions. This are the most common pixel density resources we will use.

There are a couple more pixel density resources but, for the moment,
we will focus on these four (mdpi, hdpi, xhdpi and xxhdpi).

Our task as software developers is not to pick which resource will be


shown every time but it is an Android task to decide it.

👨💻 PRO TIP
If our app runs on a different pixel density screen, Android will try to
scale down or up the graphic resources to the proper size.

👨💻 PRO TIP #2
Our graphic resource can natively be jpg, png or gif. The name of
our resources should be lowercase and without spaces.

Additional documentation to this guide can be found on The Big Nerd Ranch book, Android Programming (3rd edition). It can be found
on the eStudy folder for the course.
Once we have imported our graphic resources, we just need to add them to our layout.

The Button has an attribute to add a drawable resource on the end (and on the beginning) of it. Just by assigning
this resource as the value of the attribute will be enough.

Additional documentation to this guide can be found on The Big Nerd Ranch book, Android Programming (3rd edition). It can be found
on the eStudy folder for the course.
🦸🦸
CHALLENGES
If the student is confident enough with the topics defined on this chapter, we propose different challenges…

1. Add a listener to the TextView. When a user presses the question, the next one should be showed.
2. Add a previous button.
3. Replace the text of the buttons by two arrow icons.

---

💁💁👩💻👨💻
Help needed?
Do you need assistance on something during this guide? Please follow the next steps.

1. As an engineer and future software developer you should be able to sort out any problem you may find on
the Internet, StackOverflow is your friend!!

2. Ask the question on the forum of the course (eStudy), we can create a good teamwork environment by
answering our colleague’s questions.

3. If any of the above worked, please contact me at alex.tarrago@salle.url.edu.

Additional documentation to this guide can be found on The Big Nerd Ranch book, Android Programming (3rd edition). It can be found
on the eStudy folder for the course.

You might also like