Code Monkey home page Code Monkey logo

basic-android-kotlin-compose-training-practice-problems's Introduction

Practice Problems - Solution Code

Practice problems test learner's understanding of the concepts that they studied. The problems are themed around real-world use cases.

This repo contains solution to the practice problems. Following is the list of practice problems:

  • Compose Article

    For Learn Together app, build a screen which displays article about Jetpack Compose Tutorial

  • Task Completed

    For Task Manager app, build a screen which displays All tasks completed screen.

  • Compose Quadrant

    Build an app that displays the information about the Composable functions.

Consider the solutions as one way to solve the problems and feel free to experiment however you feel comfortable.

Pre-requisites

You need to know:

  • How to load the project from GitHub into Android Studio and run the app in Android Studio.

Recommendation:

  • Try to solve the problems before you check the solution.

basic-android-kotlin-compose-training-practice-problems's People

Contributors

johnshea avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

basic-android-kotlin-compose-training-practice-problems's Issues

Having only 2 instead of 4 quadrants please help!

`Here's my code
package com.example.composequadrantapp

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.example.composequadrantapp.ui.theme.ComposeQuadrantAppTheme

class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
ComposeQuadrantAppTheme {
// A surface container using the 'background' color from the theme
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
ComposeQuadrantApp()
}
}
}
}
}

@composable
fun ComposeInfoCard(
title: String,
description: String,
modifier: Modifier = Modifier,
backgroundColor: Color
) {
Column(
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally,
modifier = Modifier
.padding(16.dp)
.fillMaxSize()
.background(backgroundColor)) {
Text(
text = title,
fontWeight = FontWeight.Bold,
modifier = Modifier.padding(
bottom = 16.dp
)
)
Text(
text = description,
textAlign = TextAlign.Justify
)
}
}
@composable
fun ComposeQuadrantApp() {
Column(Modifier.fillMaxWidth()) {
Row(modifier = Modifier.weight(1f)) {
ComposeInfoCard(
title = stringResource(R.string.title_one),
description = stringResource(R.string.description_one),
backgroundColor = Color(0xFFEADDFF),
modifier = Modifier.weight(1f)
)
ComposeInfoCard(
title = stringResource(id = R.string.title_two),
description = stringResource(id = R.string.description_two),
backgroundColor = Color(0xFFD0BCFF),
modifier = Modifier.weight(1f)
)
}
Row(Modifier.weight(1f)) {
ComposeInfoCard(
title = stringResource(id = R.string.title_three),
description = stringResource(id = R.string.description_three),
backgroundColor = Color(0xFFB69DF8),
modifier = Modifier.weight(1f)
)
ComposeInfoCard(
title = stringResource(id = R.string.title_four),
description = stringResource(id = R.string.description_four),
backgroundColor = Color(0xFFF6EDFF),
modifier = Modifier.weight(1f)
)
}
}
}

@Preview(showBackground = true)
@composable
fun ComposeQuadrantAppPreview() {
ComposeQuadrantAppTheme {
ComposeQuadrantApp()
}
}`

Image Displays in Preview But Not in the Android Emulator view

I'm doing the Kotin course at https://developer.android.com/codelabs/basic-android-kotlin-compose-composables-practice-problems?continue=https%3A%2F%2Fdeveloper.android.com%2Fcourses%2Fpathways%2Fandroid-basics-compose-unit-1-pathway-3%23codelab-https%3A%2F%2Fdeveloper.android.com%2Fcodelabs%2Fbasic-android-kotlin-compose-composables-practice-problems#4.

The "Compose Article" exercise displayed its image without an issue, but not the "Task Manager" exercise. I copied your exact "Task Manager" code into Anroid Studio and ran the code. The @composable Preview showed the image and the text, but the Android emulators I chose showed only the text but not the image. Any idea why?

Test

In compose practice, only two quadrants' contents are displaying/complete four quadrants are not displaying in the output..

The use of modifier: Modifier.fillMaxSize() causing the output not displaying the full content.

I have tried every way possible to figure out what's going wrong. After trying different methods I found if we don't use modifier: Modifier.fillMaxWidth() in Column() as I did, then we will not face this error.

In the QuadrantCard() composable, you're using fillMaxSize() for the modifier, which might be causing the quadrants to occupy the entire available space. Since each QuadrantCard is placed inside a Row with a weight of 1f, the fillMaxSize() modifier might cause them to expand unnecessarily.
Remove the fillMaxSize() from the QuadrantCard modifier, as the Row with weight(1f) modifier already takes care of distributing the available space evenly among the quadrants.

Here is my code:

`package com.example.four_quadrantscreen

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.example.four_quadrantscreen.ui.theme.Four_quadrantScreenTheme
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.ui.unit.sp

class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
Four_quadrantScreenTheme {
// A surface container using the 'background' color from the theme
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
FourQuadrantPage()
}
}
}
}
}

@composable
fun FourQuadrantPage() {
Column(Modifier.fillMaxWidth()) {
Row(Modifier.weight(1f)) {
QuadrantCard(
titleOne = stringResource(R.string.T1) ,
description = stringResource(R.string.D1) ,
backgroundColor = Color(0xFFEADDFF),
modifier = Modifier.weight(1f)
)
QuadrantCard(
titleOne = stringResource(R.string.T2) ,
description = stringResource(R.string.D2) ,
backgroundColor = Color(0xFFD0BCFF),
modifier = Modifier.weight(1f)
)
}
Row(Modifier.weight(1f)) {
QuadrantCard(
titleOne = stringResource(R.string.T3) ,
description = stringResource(R.string.D3),
backgroundColor = Color(0xFFB69DF8),
modifier = Modifier.weight(1f)
)
QuadrantCard(
titleOne = stringResource(R.string.T4) ,
description = stringResource(R.string.D4),
backgroundColor = Color(0xFFF6EDFF),
modifier = Modifier.weight(1f)
)
}
}
}

@composable
private fun QuadrantCard(
titleOne: String,
description: String,
modifier: Modifier = Modifier,
backgroundColor: Color
) {
Column (
modifier = modifier
.background(backgroundColor)
.fillMaxSize()
.padding(16.dp),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
Text(
text = titleOne,
modifier = Modifier.padding(16.dp),
fontWeight = FontWeight.Bold,
fontSize = 13.sp
)
Text(
text = description,
textAlign = TextAlign.Justify
)
}
}

@Preview(showBackground = true)
@composable
fun GreetingPreview() {
Four_quadrantScreenTheme {
FourQuadrantPage()
}
}
`

With this code you get the result like here:

The output of my practice code.

2

Capture

Help! Only the two left quadrants appear.

With the code below only the left quadrants appear. I still can't figure out the solution.

package com.example.composequadrant

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.example.composequadrant.ui.theme.ComposeQuadrantTheme

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            ComposeQuadrantTheme {
                // A surface container using the 'background' color from the theme
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colorScheme.background
                ) {
                    Quadrants(
                        titleUpperRight = "Text composable",
                        textUpperRight = "Displays text and follows the recommended Material Design guidelines.",
                        colorUpperRight = Color(0xFFEADDFF),
                        titleUpperLeft = "Image composable",
                        textUpperLeft = "Creates a composable that lays out and draws a given Painter class object.",
                        colorUpperLeft = Color(0xFFD0BCFF),
                        titleBottomRight = "Row composable",
                        textBottomRight = "A layout composable that places its children in a horizontal sequence.",
                        colorBottomRight = Color(0xFFB69DF8),
                        titleBottomLeft = "Column composable",
                        textBottomLeft = "A layout composable that places its children in a vertical sequence.",
                        colorBottomLeft = Color(0xFFF6EDFF)
                    )
                }
            }
        }
    }
}

@Composable
fun ComposableInfoCard(title: String, description: String,
                        color: Color, modifier: Modifier = Modifier) {
    // Setup background, padding and size modifiers. Also, set the alignment to center - vertically as well
    // as horizontally
    Column(
        verticalArrangement = Arrangement.Center,
        horizontalAlignment = Alignment.CenterHorizontally,
        modifier = modifier
            .fillMaxSize()
            .background(color)
            .padding(16.dp)
    ) {
        Text(
            text = title,
            fontWeight = FontWeight.Bold,
            modifier = Modifier.padding(bottom = 16.dp)
        )
        Text(
            text = description,
            textAlign = TextAlign.Justify,
        )
    }
}

@Composable
fun Quadrants(titleUpperRight: String, textUpperRight: String, colorUpperRight: Color,
              titleUpperLeft: String, textUpperLeft: String, colorUpperLeft: Color,
              titleBottomRight: String, textBottomRight: String, colorBottomRight: Color,
              titleBottomLeft: String, textBottomLeft: String, colorBottomLeft: Color,
              modifier: Modifier = Modifier) {
    Column(modifier = modifier) {
        Row(modifier = Modifier.weight(1f)) {
            ComposableInfoCard(
                title = titleUpperRight,
                description = textUpperRight,
                color = colorUpperRight
            )
            ComposableInfoCard(
                title = titleUpperLeft,
                description = textUpperLeft,
                color = colorUpperLeft
            )
        }
        Row(modifier = Modifier.weight(1f)) {
            ComposableInfoCard(
                title = titleBottomRight,
                description = textBottomRight,
                color = colorBottomRight
                )
            ComposableInfoCard(
                title = titleBottomLeft,
                description = textBottomLeft,
                color = colorBottomLeft
                )
            }
        }

    }


@Preview(showBackground = true)
@Composable
fun QuadrantPreview() {
    ComposeQuadrantTheme {
        Quadrants("1,1,1",
            "1,1,2",
            Color(0xFFEADDFF),
            "1,2,1",
            "1,2,2",
            Color(0xFFD0BCFF),
            "1,2,1",
            "1,2,2",
            Color(0xFFB69DF8),
            "2,2,1",
            "2,2,2",
            Color(0xFFF6EDFF)
        )
    }
}

Compose Quadrant, no second column (only one element per row)

In developing a solution to the Compose Quadrant exercise, I followed the guidance that suggested using
Row(Modifier.weight(1f)) { ...
in order to create two equal-width elements in each row. This usage is supposed to set the width of each element in the row. However, when I hover the mouse over the word weight, I see that it is sizing element height and has ColumnScope. I do not see a second column in the Preview.

I need help determining the problem.

Here is my code:

package com.example.composequadrant

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.example.composequadrant.ui.theme.ComposeQuadrantTheme

class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
ComposeQuadrantTheme {
// A surface container using the 'background' color from the theme
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
//Greeting("Android")
}
}
}
}
}
@composable
fun QuadrantAssembly(heading11: String, content11: String, colour11: Long,
heading12: String, content12: String, colour12: Long,
heading21: String, content21: String, colour21: Long,
heading22: String, content22: String, colour22: Long,
modifier: Modifier = Modifier) {
Column(
verticalArrangement = Arrangement.Center,
modifier = Modifier
.padding(16.dp)
) {
Row(Modifier.weight(1f)) {
Quadrant(heading11, content11, colour11)
Quadrant(heading12, content12, colour12)
}
Row(Modifier.weight(1f)) {
Quadrant(heading21, content21, colour21)
Quadrant(heading22, content22, colour22)
}
}
}

@composable
fun Quadrant(heading: String, content: String, colour: Long, modifier: Modifier = Modifier) {
Surface(modifier = Modifier.fillMaxSize(), color = Color(colour)) {
Column(
verticalArrangement = Arrangement.Center,
modifier = modifier
) {
Text(
text = heading,
fontWeight = FontWeight.Bold,
modifier = Modifier
.align(alignment = Alignment.CenterHorizontally)
.padding(bottom = 16.dp)
)
Text(
text = content,
modifier = modifier,
textAlign = TextAlign.Justify
)
}
}
}

@Preview(showBackground = true)
@composable
fun QuadrantPreview() {
ComposeQuadrantTheme {
QuadrantAssembly(
stringResource(R.string.quad11_heading), stringResource(R.string.quad11_content), 0xFFEADDFF,
stringResource(R.string.quad12_heading), stringResource(R.string.quad12_content), 0xFFD0BCFF,
stringResource(R.string.quad21_heading), stringResource(R.string.quad21_content), 0xFFB69DF8,
stringResource(R.string.quad22_heading), stringResource(R.string.quad22_content), 0xFFF6EDFF
)
}
}

My code doesn't work

I am missing something in my code that doesn't allow for Compose Quadrant to come out 2 on top and 2 on the bottom but all side by side. What am I missing? Thanks!
package com.example.composableinfo

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.example.composableinfo.ui.theme.ComposableInfoTheme

class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
ComposableInfoTheme {
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background,
) {
ComposeInfoApp()
}
}
}
}
}

data class ComposableInfo(val title: String, val description: String)

@composable
fun ComposableCard(composableInfo: ComposableInfo) {
Card(
modifier = Modifier
.fillMaxSize()
.padding(16.dp)
.weight(1f)
.background(Color.White),
elevation = 4.dp
) {
Column(
modifier = Modifier
.padding(16.dp)
.fillMaxSize(),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center
) {
Text(
text = composableInfo.title,
fontWeight = FontWeight.Bold,
fontSize = 18.sp,
color = Color.Blue,
modifier = Modifier.padding(bottom = 16.dp)
)
Text(
text = composableInfo.description,
fontSize = 14.sp
)
}
}
}

@composable
fun ComposeInfoApp() {
val composables = listOf(
ComposableInfo(
title = "Text Composable",
description = "Displays text and follows the recommended Material Design guidelines."
),
ComposableInfo(
title = "Image Composable",
description = "Creates a composable that lays out and draws a given Painter class object."
),
ComposableInfo(
title = "Row Composable",
description = "A layout composable that places its children in a horizontal sequence."
),
ComposableInfo(
title = "Column Composable",
description = "A layout composable that places its children in a vertical sequence."
)
)

Column(
    modifier = Modifier.fillMaxSize()
) {
    Row(
        modifier = Modifier.fillMaxWidth().weight(1f)
    ) {
        composables.subList(0, 2).forEach { composable ->
            ComposableCard(composable)
        }
    }

    Row(
        modifier = Modifier.fillMaxWidth().weight(1f)
    ) {
        composables.subList(2, 4).forEach { composable ->
            ComposableCard(composable)
        }
    }
}

}

@Preview(showBackground = true)
@composable
fun ComposeInfoAppPreview() {
ComposableInfoTheme {
ComposeInfoApp()
}
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.