Intro to Android Development
  • Welcome
  • Syllabus
  • Hack Challenge
  • Resources
    • Lecture Videos
    • Ed Discussion
    • Git & GitHub Help/How-To
    • Setting up Android Studio
    • Starting an Android Studio Project & Making an Emulator
    • Importing, Exporting, & Submitting Your Projects to CMS
  • SP25 Course Material
    • Week 1 | Course Logistics, Kotlin, & Basic UI
      • Relevant Links
      • Demo/Lecture: Eatery Card
      • A0: Eatery Card (Follow-Along)
    • Week 2 | States, Components, LazyColumn
      • Relevant Links
      • Demo: Todo List
      • A2: Shopping List
    • Week 3 | Navigation & Animations
      • Relevant Links
      • Demo: Onboarding
      • A3: Stock Trading (RobbingGood)
    • Week 4 | MVVM and Flows
      • Relevant Links
      • Demo: Eatery Card 2
      • A4: Chat of a Lifetime
    • Week 5 | Dumb Components & UIEvents
      • Relevant Links
      • Demo: Music Player
      • A5: Rate My Vibe
    • Week 6 | Coroutines, Networking, JSON
      • Relevant Links
      • Demo: Retrofit
      • A6: You Should Even Lift, Bro.
  • Bonus Week | Android Job Search
    • Relevant Links
    • Android Technical Interview Question!
  • Textbook
    • 1. Introduction to the Editor and Views
      • 1.1 Introduction to the Editor
      • 1.2 SDK Management
      • 1.3 Kotlin Overview
      • 1.4 Views
      • 1.5 Android Studio Project Demo + Understanding The Editor
    • 2. Jetpack Compose
      • 2.1 Introduction
      • 2.2 Layouts
      • 2.3 Modifiers
      • 2.4 Animations
      • 2.5 Lazy Lists
      • 2.6 Reactive UI
    • 3. Intents and Manifest
      • 3.1 Activities
      • 3.2 Implicit Intents
      • 3.3 Explicit Intents
      • 3.4 Manifest
      • 3.5 Permissions
      • 3.6 Summary
    • 4. Navigation
      • 4.1 Types of Navigation
      • 4.2 Implementation of the Bottom Navigation Bar
    • 5. Data and Persistent Storage
      • 5.1 Singleton Classes
      • 5.2 Shared Preferences
      • 5.3 Rooms
      • 5.4 Entities
      • 5.5 Data Access Objects
      • 5.6 Databases
    • 5.5 Concurrency
      • 5.5.1 Coroutines
      • 5.5.2 Implementation of Coroutines
      • 5.5.3 Coroutines with Networking Calls
    • 6. Networking and 3rd Party libraries
      • 6.1 HTTP Overview
      • 6.2 3rd Party Libraries
      • 6.3 JSON and Moshi
      • 6.4 Retrofit
      • 6.5 Summary
    • 7. MVVM Design Pattern
      • 7.1 Key Idea
      • 7.2 Implementation Ideas
    • 8. Flows
    • 9. The Art and Ontology of Software
    • 10. 🔥 Firebase
      • 10.1 Setting up Firebase
      • 10.2 Authentication
      • 10.3 Analytics
      • 10.4 Messaging
      • 10.5 Firestore
  • Additional Topics
    • Git and GitHub
    • Exporting to APK
  • Archive
    • Archived Native Android Textbook Pages
      • 1. Layouts and More Views
        • 1.1 File Structure and File Types
        • 1.2 Resource Files
        • 1.3 Button and Input Control
        • 1.4 ViewGroups
        • 1.5 Summary + A Note On Chapter 2 Topics
      • 2. RecyclerViews
        • 2.1 RecyclerViews
        • 2.2 RecyclerView Performance
        • 2.3 Implementation of a Recycler View
        • 2.4 Implementation with Input Controls
        • 2.5 Filtering RecyclerViews
        • 2.6 Recyclerview Demo
      • 3. ListViews and Searching
        • 3.1 ListView vs. RecyclerView
        • 3.2 ListView Performance
        • 3.3 Implementation of a ListView
        • 3.4 Searching in a List View
      • 4. Fragments
        • 4.1 What are Fragments?
        • 4.2 Lifecycle of a Fragment
        • 4.3 Integrating a Fragment into an Activity
        • 4.4 Sharing Data Between Fragments
        • 4.5 Fragment Slide Shows
      • 5. OkHttp
      • 6. Activity Lifecycle
      • 7. Implementation of Tab Layout
    • Fall 2024 Course Material
      • Lecture 1 & Exercise 1: Introduction to Android
      • Lecture 1.5: Beauty of Kotlin
      • Lecture 2 & HW 2: Modifiers, Lazylists and Reactive UI
      • Lecture 3 & HW 3: Animations, Intents and Manifest
      • Lecture 4 & HW 4: Coroutines & Navigation
      • Lecture 5 & HW 5: Persistent Storage, Networking, and JSON Parsing
      • Lecture 6 & HW 6: MVVM, Flows
      • Bonus Lectures & Bonus HW
      • Bonus Lecture: Industry Practice
    • Spring 2024 Course Material
      • Lecture 1 & Exercise 1: Introduction to Android
      • Lecture 4 & HW 4: LazyLists
      • Lecture 6 & HW 6: Networking, Data, and Persistent Storage
    • Spring 2020 Course Material
      • Week 1: Intro to the Editor
      • Week 2: Views and Layouts
      • Week 3: Intent and Manifest
      • Week 4: ListView and RecyclerView
      • Week 5: Fragments
      • Week 6: Networking
    • Spring 2021 Lecture & HW 8: Networking & 3rd Party APIs
    • HackOurCampus Workshop
Powered by GitBook
On this page
  • Release Code:
  • Requirements:

Was this helpful?

  1. SP25 Course Material
  2. Week 3 | Navigation & Animations

A3: Stock Trading (RobbingGood)

I am Sobbin...hood...

PreviousDemo: OnboardingNextWeek 4 | MVVM and Flows

Last updated 1 month ago

Was this helpful?

This assignment has a release code! Please download it below, extract it, and open it in Android Studio. Ensure it can compile and run fine on your emulator before starting this assignment.

For our second assignment, we'll be making a stock-trading app competitor! Of course we won't be working with real money, but we've set up the above release code to give the semblance of trading your hard-earned cash!

Let's walk through the code, shall we?

NOTE: This is a longer assignment, so we made sure to provide lots of Hints!

Release Code:

Expand each of the files below for a description of the release code!

WalletRepository.kt

This repository manages the user's virtual wallet. It keeps track of the current balance and allows the user to deposit or withdraw money when buying or selling stocks. Additionally, it maintains a record of the stocks the user owns.

Methods you will use:

getBalance(): Gets a numeric value for the balance that you can freely use in your UI!

percentageBalanceChange(): Returns a float representing the percentage that the user's wallet has recently changed by (rise is positive, fall is negative). Free to use in UI.

buyStock(): Buys a stock.

sellStock(): Sells a stock.

amountStockOwned(): Tells you how many of a given stock you own. Free to use in UI.

YOU SHOULD NOT EDIT THIS FILE.

StockRepository.kt

This repository handles the dynamic pricing of stocks, where each stock is identified by a ticker symbol. It provides methods to retrieve the current price of a stock based on its ticker.

Behind the scenes, it randomly updates the stock prices every 3 seconds to simulate market fluctuations.

Methods you will use:

getStockPrice(): Returns the (current) stock price of the given ticker that you can use in your UI.

increaseOrDecreaseStockPrice(): Returns a boolean; if true, the specified stock has increased in price. If false, it has decreased. Can freely use in your UI.

Note: do NOT use `readStockPrice()`. The way it is implemented will not work in your UI.

YOU SHOULD NOT EDIT THIS FILE.

MainScreen.kt

MainScreen is an incomplete main screen that shows the home for our app, RobbingGood!

It consists of a place where your WalletRow and TickerRows will go!

Note: These two Repository files violate the MVVM paradigm. They are NOT a good example for what to do for the Hack Challenge; please refer to lectures 4, 5, and 6 for MVVM practices. These files are simply a bare minimum functionality we provide for this assignment.

Requirements:

Okay, onto the assignment!

(expand each file below to see your requirements!)

WalletRow.kt

WalletRow is a component that shows the user's balance with animations. If the user's balance has recently risen, the text shows green, otherwise red, and a little animation plays to swap the text out.

In general, try to match this UI:

  1. Display the user's wallet balance given by getBalance() as a nicely formatted ($XXX.XX), along with its percentage change (percentageBalanceChange()) to the far right, 2 decimal places max.

  2. Whenever the balance changes, an AnimatedContent should swap out the percentage text.

  3. If the percentage is positive, the percentage text should be a (bold) green, otherwise a (bold) red.

  4. Once you've made this component, WalletRow should be replaced on the MainScreen, and you'll also later use it in StockDetailScreen.

Hints:

  • Given a percentage, you can use the following code snippet to convert it into a nicely readable string:

// Format 2 digits after the decimal
val formattedPercentage = "%.2f".format(abs(percentage * 100f))
val percentageString = if (percentage < 0) {
    "(-$formattedPercentage%)"
} else {
    "(+$formattedPercentage%)"
}
  • To achieve the space in between the left and right parts of the row, you might want to use Arrangement.SpaceBetween.

TickerRow.kt

TickerRow is a UI component that displays a quick and dirty summary of a stock's price and which allows the user to click on the row to navigate to the Stock Detail Screen (SDS).

In general, try to match this UI:

You

  1. Similar to WalletRow, display the balance and percentage change of this stock.

    1. Whenever the stock price changes, an AnimatedContent should swap the balance/percentage text out.

    2. Green if rise, red if fall.

  2. If the user taps on this row, they should navigate to the SDS, sending the ticker as a navigation argument.

  3. Once you've made TickerRow, use it in the LazyColumn in MainScreen to show the tickers!

Hints:

  • You'll have to set up a NavHost! Please refer to our demo this week for tips.

    • (!!!) Make sure to then call your navigation code in MainActivity — it's currently just calling MainScreen().

    • Do NOT make the NavHost inside of MainScreen() — the nav host should exist outisde of main screen, and then have main screen as one of the routes, and SDS as another route.

  • Think about what the state should be for the AnimatedContent--there are multiple correct answers!

    • For example, you can say that the text and color is a state! Or that the price/percentage is a state, and then you calculate the text with those values inside the AnimatedContentbody. As long as it looks like it's animating right, you're good!

  • You can reuse snippets of code from WalletRowand A2 to achieve the money text formatting.

  • Use Modifier.clickable() to make any Composable clickable.

    • You'll likely want to thus provide an onClick: () -> Unit as an argument to TickerRow.kt.

StockDetailScreen.kt

The Stock Detail Screen is a full screen which displays the current price of the selected stock, the user's wallet, and the amount of that stock owned. It also provides the option to buy or sell instances of that stock, updating the user's wallet accordingly.

In general, try to match this UI:

  1. The ticker that the user has selected (and given to this screen as a nav argument) should be displayed at the top of this page as the title.

  2. In the (roughly) middle of the screen, there should be 2 large, bold texts— one for the balance, and one for the percentage.

    1. These should animate differently from the others; The float balance and percentage here should animate to the correct balance (meaning the literal number itself animates).

    2. These texts can just be black.

  3. The background of the entire screen should animate to a very light green if the stock is up, or a light red if the stock is down.

    1. You must use animateColorAsState to achieve a smooth color animation.

  4. At the footer of the screen, you should use WalletRow to display the user's current balance.

  5. Add a text of some sort to display how many of the current stock the user has. You may use amountStockOwned().

  6. Add two buttons, Buy and Sell. Buying should call buyStock()and Selling should call sellStock().

Note: To navigate back to the home screen, just use the in-built navigation. If you don't see nav buttons, on android, that just means dragging and letting go from the left-hand of the screen.

Hints:

  • You'll have to provide the stock ticker as a navigation argument. See lecture for to how to do this!

  • To achieve the faded green/red color, you can leverage doubling up a background Modifier! The following code will first make the background White, then layer a faded (0.2 opacity) color on top (for some backgroundColor):

Modifier.background(Color.White)
.background(backgroundColor.copy(alpha = 0.2f)),
  • See animateFloatAsStateand animateColorAsState.

  • You might want to take in the stock ticker (string) as an argument to the StockDetailScreen composable function!

    • (Similarly, you might want to take in the nav controller as an argument to MainScreen so that MainScreen knows how to navigate.)

Once you've completed A3, your solution should look something like this:

When you're done, export your project through Android Studio and upload your ZIP to CMS! Congrats on finishing A3!

126KB
a3-release.zip
archive
A3 Release Code! Extract and Open as an Android Project.
4MB
a3-soln-demo-fix.mp4
A3 Solution Demo! (Fixed 3/26/25)
Exporting with File -> Export -> Export to Zip File...