Skip to content

Instantly share code, notes, and snippets.

@djnotes
Last active October 26, 2022 07:23
Show Gist options
  • Save djnotes/fe4740ccb3b89620426dc35906ad33f8 to your computer and use it in GitHub Desktop.
Save djnotes/fe4740ccb3b89620426dc35906ad33f8 to your computer and use it in GitHub Desktop.
Simple login screen definition in Jetpack Compose
package com.droidcon.yourapp.ui.screens
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.outlined.AccountCircle
import androidx.compose.material.icons.outlined.Login
import androidx.compose.material3.Button
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.droidcon.yourapp.R
@Composable
fun Landing(onNavigate: (String) -> Unit) {
Column(
Modifier
.fillMaxSize(),
horizontalAlignment = Alignment.CenterHorizontally
) {
Image(
painter = painterResource(R.drawable.ic_launcher_foreground),
contentDescription = stringResource(R.string.compass)
)
Button(onClick = {
onNavigate("signup")
}) {
Icon(
imageVector = Icons.Outlined.AccountCircle,
contentDescription = Icons.Outlined.AccountCircle.name,
modifier = Modifier
.padding(horizontal = 8.dp)
)
Text(text = stringResource(R.string.signup))
}
Button(onClick = {
onNavigate("login/This is my tip of the day for Login")
}) {
Icon(
imageVector = Icons.Outlined.Login,
contentDescription = Icons.Outlined.Login.name,
modifier = Modifier
.padding(horizontal = 8.dp)
)
Text(text = stringResource(R.string.login))
}
}
}
@Preview
@Composable
fun LandingPreview() {
Landing(onNavigate = {})
}
package com.droidcon.yourapp.ui.screens
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.material3.Button
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.scale
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.droidcon.yourapp.R
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun Login(tip: String, onBackPress: () -> Unit, onNavigate: (String) -> Unit) {
var email by remember { mutableStateOf("") }
var password by remember { mutableStateOf("") }
Column(modifier = Modifier
.fillMaxSize()
) {
Text(
text = tip,
modifier = Modifier
.fillMaxWidth()
.padding(8.dp)
.border(2.dp, MaterialTheme.colorScheme.secondary, MaterialTheme.shapes.medium)
.padding(16.dp)
,
style = MaterialTheme.typography.headlineSmall
,
)
OutlinedTextField(
value = email, onValueChange = { email = it },
placeholder = {
Text(text = stringResource(R.string.email))
},
modifier = Modifier
.padding(8.dp)
.fillMaxWidth()
)
OutlinedTextField(
value = password, onValueChange = { password = it },
placeholder = {
Text(text = stringResource(R.string.password))
},
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Password),
modifier = Modifier
.padding(8.dp)
.fillMaxWidth()
)
Button(
onClick = {
// onNavigate("landing")
},
modifier = Modifier
.padding(8.dp)
.align(Alignment.CenterHorizontally)
) {
Text(stringResource(R.string.login))
}
Spacer(modifier = Modifier
.weight(1f)
)
IconButton(onClick = {
onBackPress()
},
modifier = Modifier
.weight(0.2f)
.fillMaxWidth()
) {
Icon(imageVector = Icons.Filled.ArrowBack,
contentDescription = Icons.Filled.ArrowBack.name,
modifier = Modifier
.align(Alignment.CenterHorizontally)
.scale(2f)
.background(color = MaterialTheme.colorScheme.secondary,
shape = CircleShape
)
)
}
}
}
@Preview
@Composable
fun LoginPreview() {
Login(tip = "Tip 2", onBackPress = {}, onNavigate = {})
}
package com.droidcon.yourapp.ui.screens
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.material3.Button
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.scale
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.droidcon.yourapp.R
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun Signup(tip: String, onBackPress: () -> Unit, onNavigate: (String) -> Unit) {
var email by remember{mutableStateOf("")}
var password by remember{mutableStateOf("")}
var passwordConfirm by remember{mutableStateOf("")}
Column(modifier = Modifier
.fillMaxSize()
) {
Text(text = tip, modifier = Modifier
.fillMaxWidth()
.padding(8.dp)
.border(2.dp, MaterialTheme.colorScheme.secondary, MaterialTheme.shapes.medium)
.padding(16.dp)
,
style = MaterialTheme.typography.headlineSmall
)
OutlinedTextField(value = email, onValueChange = { email = it },
placeholder = {
Text(text = stringResource(R.string.email))
},
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Email),
modifier = Modifier
.fillMaxWidth()
.padding(8.dp)
)
OutlinedTextField(
value = password, onValueChange = { password = it },
placeholder = {
Text(text = stringResource(R.string.password))
},
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Password),
modifier = Modifier
.fillMaxWidth()
.padding(8.dp)
)
OutlinedTextField(
value = passwordConfirm, onValueChange = { passwordConfirm = it },
placeholder = {
Text(text = stringResource(R.string.confirm_password))
},
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Password),
modifier = Modifier
.fillMaxWidth()
.padding(8.dp)
)
Button(
onClick = {
onNavigate("signup")
},
modifier = Modifier
.padding(8.dp)
.align(Alignment.CenterHorizontally)
) {
Text(stringResource(R.string.signup))
}
Spacer(modifier = Modifier
.weight(1f)
)
IconButton(onClick = {
onBackPress()
},
modifier = Modifier
.weight(0.2f)
.fillMaxWidth()
) {
Icon(imageVector = Icons.Filled.ArrowBack,
contentDescription = Icons.Filled.ArrowBack.name,
modifier = Modifier
.align(Alignment.CenterHorizontally)
.scale(2f)
.background(color = MaterialTheme.colorScheme.secondary,
shape = CircleShape
)
)
}
}
}
@Preview
@Composable
fun SignupPreview() {
Signup(tip = "Default Tip", onBackPress = {}, onNavigate = {})
}
<!-- Add under <resources> -->
<string name="login">Login</string>
<string name="signup">Signup</string>
<string name="compass">Compass</string>
<string name="placeholder">Place Holder</string>
<string name="email">Email</string>
<string name="password">Password</string>
<string name="confirm_password">Confirm Password</string>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment