Day79 of #100DaysOfCode

Day79 of #100DaysOfCode

Hii folks 🙌

Today I will be continuing the same pathway in which we will calculate the price from order details.

Unit 3: Navigation

Pathway 3: Advanced navigation app Examples

https://developer.android.com/courses/android-basics-kotlin/course

Calculate price from order details

Looking at the final app screenshots of this codelab, we’ll notice that the price is actually displayed on each fragment (except the StartFragment) so the user knows the price as they create the order.

We will update the price in ViewModel.

package ...

import ...

private const val PRICE_PER_CUPCAKE = 2.00

class OrderViewModel : ViewModel() {
...
  • Now that we have defined a price per cupcake, we will create a helper method to calculate the price. This method can be private because it's only used within this class.
private fun updatePrice() {
_price.value = (quantity.value ?: 0) * PRICE_PER_CUPCAKE
}
  • For the subtotal text view, set the value of the android:text attribute to be "@{@string/subtotal_price(viewModel.price)}".
...

<TextView
android:id="@+id/subtotal"
android:text="@{@string/subtotal_price(viewModel.price)}"
... />

...
  • Then we will run the app and if we select One Cupcake in the fragment, it will show the Subtotal 12.0. Now we will make a similar change for the pickup and summary fragments. In fragment_pickup.xml and fragment_summary.xml layouts, modify the text views to use the viewModel price property as well.

fragment_pickup.xml

...

<TextView
android:id="@+id/subtotal"
...
android:text="@{@string/subtotal_price(viewModel.price)}"
... />

...

fragment_summary.xml

<TextView
android:id="@+id/total"
...
android:text="@{@string/total_price(viewModel.price)}"
... />

That is all for Day79 ✅

Thanks for reading, See you tomorrow!

--

--

--

https://www.linkedin.com/in/itskushagra

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Integrate Social Media with Android app: Twitter: 2/3

Day73 of #100DaysOfCode

Android studio calculator

Android studio calculator

Reusability in Android (Kotlin) — Fragment with View Binding

Day56 of #100DaysOfCode

The 7 Best Free Android Apps to Find Your Friends via GPS

Shard your Android Espresso tests for faster execution in parallel

Day70 of #100DaysOfCode

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Kushagra Kesav

Kushagra Kesav

https://www.linkedin.com/in/itskushagra

More from Medium

Day71 of #100DaysOfCode

Day65 of #100DaysOfCode

Day77 of #100DaysOfCode

Day83 of #100DaysOfCode