Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#3 add rows and columns #2

Open
wants to merge 3 commits into
base: #1_composable_function_info
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ package com.lahsuak.apps.jetpackcomposebasic
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.foundation.layout.*
import androidx.compose.material3.*
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.lahsuak.apps.jetpackcomposebasic.ui.theme.JetPackComposeBasicTheme

class MainActivity : ComponentActivity() {
Expand All @@ -18,33 +17,69 @@ class MainActivity : ComponentActivity() {
setContent {
JetPackComposeBasicTheme {
// A surface container using the 'background' color from the theme
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
Greeting("Android")
}
MyApp(modifier = Modifier.fillMaxSize())
}
}
}
}
/***
Composable functions :

@Composable
fun MyApp(
modifier: Modifier = Modifier,
names: List<String> = listOf("World", "Kaushal")
) {
Column(modifier = modifier.padding(vertical = 4.dp)) {
for (name in names) {
Greeting(name)
}
}
}

/**
* Composable functions :
A composable function is a regular function annotated with @Composable.
This enables your function to call other @Composable functions within it.
You can see how the Greeting function is marked as @Composable.
This function will produce a piece of UI hierarchy displaying the given input,
String. Text is a composable function provided by the library.
***/
**/

/**
* 1. Column is Vertical linear layout of view system
* 2. Row is Horizontal linear layout of view system
* 3. Box is FrameLayout of view system
*/

/**
* Button is a composable provided by the material3 package
which takes a composable as the last argument.
Since trailing lambdas can be moved outside of the parentheses,
you can add any content to the button as a child. For example, a Text
*/

@Composable
fun Greeting(name: String) {
Text(text = "Hello $name!")
Surface(
color = MaterialTheme.colorScheme.primary,
modifier = Modifier.padding(horizontal = 8.dp, vertical = 4.dp)
) {
Row(modifier = Modifier.padding(24.dp)) {
Column(modifier = Modifier.weight(1f)) {
Text(text = "Hello,")
Text(text = name)
}
ElevatedButton(onClick = { }) {
Text(text = "Show more")
}
}

}
}

@Preview(showBackground = true)
@Composable
fun DefaultPreview() {
JetPackComposeBasicTheme {
Greeting("Android")
MyApp()
}
}