Skip to main content

Jetpack Compose with remember and mutableStateOf

 Jetpack Compose is a modern declarative UI Toolkit . In jetpack's declarative approach widgets are stateless and does not expose getter or setter function . So we cannot update UI with button.setText(String) or img.setImageBitmap().... 

In Compose we build UI by defining set of Composable function. Composable function take in data and emit UI elements. So in Compose  only way to  update the UI is by calling the same Composable function with new Arguments. This Argument represents UI State. State in an app is any value that changes over time , this includes simple class variable to Room Database. Any time a State is updated we need to call the Composable with new State to update the UI. This is called ReComposition

This Demo Jetpack Compose app will help you understand the basics of Jetpack Compose.

Screenshots of this App

 
This is our MainActivity
package com.arun.androidtutsforu.democompose
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.*
import androidx.compose.material.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.getValue
import androidx.compose.runtime.setValue
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.unit.dp
import androidx.compose.ui.unit.sp
import com.arun.androidtutsforu.democompose.ui.theme.DemoComposeTheme
class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            DemoComposeTheme {
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colors.background
                ) {
                    CalculateScreen()
                }
            }
        }
    }
}
@Composable
fun CalculateScreen(){
    var firstNum by remember { mutableStateOf("")}
    var secondNum by remember { mutableStateOf("")}
    val num1 = firstNum.toIntOrNull()?:0
    val num2 = secondNum.toIntOrNull()?:0
    val sum = num1+num2
    Column(
        modifier = Modifier.padding(16.dp),
        horizontalAlignment = Alignment.CenterHorizontally
        ) {
        Text(
            text = "JetPack Compose Demo",
            fontWeight = FontWeight.Bold,
            fontSize = 20.sp,
            color = Color.Blue
        )
        Spacer(modifier = Modifier.height(32.dp))
        TextField(
            value =firstNum ,
            onValueChange ={firstNum = it} ,
            label = { Text(text = "Enter First Num")}
        )
        Spacer(modifier = Modifier.height(16.dp))
        TextField(
            value = secondNum,
            onValueChange = {secondNum=it},
            label = { Text(text = "Enter Second Num")}
        )
        Spacer(modifier = Modifier.height(16.dp))
        Text(
            text = "The sum is $sum",
            fontWeight = FontWeight.Bold,
            fontSize = 20.sp
        )
    }
}
 @Composable
fun CalculateScreen(){
   ....
   ....
   }
This is our Composable function . In Compose we build UI by defining set of Composable function
Column(modifier = Modifier.padding(16.dp),
        horizontalAlignment = Alignment.CenterHorizontally
       ) {
        ....
        ....
        }
we use Column to place elements vertically on the screen
var firstNum by remember { mutableStateOf("")}
var secondNum by remember { mutableStateOf("")}

mutableStateOf is an Observable type integrated with android runtime.So when we define any state  as mutableStateOf , any changes to that state will schedule recomposition of every composable function that reads that state.

Composable uses remember API to store an object in memory . Stored value is returned during recomoposition. We use remember and mutableStateof to update the UI.

val value = remember{mutableStateof(default)
val value by remember{muableStateof(default)

when we use by delegates we use the following imports

import androidx.compose.runtime.getValue
import androidx.compose.runtime.setValue
our var firstNum,secondNum are defined as mutableStateOf with default value " ". So when its value changes in 
onValueChange={firstNum=it}.
It will schedule recomposition and updates our UI.
TextField(
            value =firstNum ,
            onValueChange ={firstNum = it} ,
            label = { Text(text = "Enter First Num")}
        )
        ...
        TextField(
            value = secondNum,
            onValueChange = {secondNum=it},
            label = { Text(text = "Enter Second Num")}
        )
remember helps us retain the State across recomposition . But when configuration changes happens (rotation of the device ) and on process death whole activity  restarts and all the state will be lost. So We must use rememberSeaveble instead of remember , rememberSeaveble will survive configuration changes and process death . rememberSaveable automatically saves any value that can be saved in a Bundle. For other values we can pass a custom saver objects
class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            DemoComposeTheme {
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colors.background
                ) {
                    CalculateScreen()
                }
           }
        }
    }
}
@Composable
fun CalculateScreen(){
    var firstNum by rememberSaveable{ mutableStateOf("")}
    var secondNum by rememberSaveable { mutableStateOf("")}
    val num1 = firstNum.toIntOrNull()?:0
    val num2 = secondNum.toIntOrNull()?:0
    val sum = num1+num2
    Column(
        modifier = Modifier.padding(16.dp),
        horizontalAlignment = Alignment.CenterHorizontally
        ) {
        Text(
            text = "JetPack Compose Demo",
            fontWeight = FontWeight.Bold,
            fontSize = 20.sp,
            color = Color.Blue
        )
        Spacer(modifier = Modifier.height(32.dp))
        TextField(
            value =firstNum ,
            onValueChange ={firstNum = it} ,
            label = { Text(text = "Enter First Num")}
        )
        Spacer(modifier = Modifier.height(16.dp))
        TextField(
            value = secondNum,
            onValueChange = {secondNum=it},
            label = { Text(text = "Enter Second Num")}
        )
        Spacer(modifier = Modifier.height(16.dp))
        Text(
            text = "The sum is $sum",
            fontWeight = FontWeight.Bold,
            fontSize = 20.sp
        )
    }
}
You can Download full Source code of this App From my github



Comments

Popular posts

Android List View using Custom Adapter and SQLite

following is a simple applicaton to create ListView using  Custom adapter.screenshot of the application  is like this . ListView is not used in android Anymore . We use  RecyclerView  and  CardView   in Android RecyclerView Demo is available on the following link http://androidtuts4u.blogspot.in/2017/04/android-recyclerview-example.html RecyclerView with Cardview Example is available on the following link http://androidtuts4u.blogspot.in/2017/09/android-card-view-example.html The ListView below the submit button is populated using Custom Adapter.Data is stored and retrieved using SQLite databsase. you can download the source code of this project from  google drive   https://drive.google.com/folderview?id=0BySLpWhqmbbdUXE5aTNhazludjQ&usp=sharing click on the above link ->sign into  your  google account ->add this to your google drive -> open it in google drive and download it. To create a simple application like this 1.  Create a class which extends  

Android CardView And SQLite example

SQLite Database Android provides several options to save persistent application data. SQlite database is ideal for saving repeating and structured data . Using Sqlite we can save structured data in a private database.  This is a simple application showing use of Sqlite database in android . This example shows how to perform Insert , select , update and delete operation in  SQlite database Screenshots of our sample application                                                                                                                      This is a Registration app. New user can register by clicking registration button . Registered  users are shown in cards below that button . we can update or delete registered users by clicking overflow menu in each card. 1.First we need to create database for our application .    a. Create a contract class .Contract class contains constants that defines names of Tables and columns of our database. Contract class allows us

How to install eclipse tar.gz file in ubuntu 12.04

eclipse-SDK-3.7-linux-gtk-x86_64.tar.gz  file can be installed in ubuntu 12.04  like this  1. dowload latest version of eclipse from here   2. extract the eclipse-SDK-3.7-linux-gtk-x86_64.tar.gz file 3. move extracted file to /opt/  directory       mv eclipse /opt/      sudo chown -R root:root eclipse      sudo chmod -R +r eclipse 4. create eclipse exectable in your path      sudo touch /usr/bin/eclipse    sudo chmod 755 /usr/bin/eclipse    sudo nano /usr/bin/eclipse 5. copy this into nano      #!/bin/sh   #export MOZILLA_FIVE_HOME="/usr/lib/mozilla/"   export ECLIPSE_HOME="/opt/eclipse"    $ECLIPSE_HOME/eclipse $* 6. save file using ctrl+o  exit with ctrl+x 7. create gnome menu item        sudo nano /usr/share/applications/eclipse.desktop 8. copy this into nano          [Desktop Entry]    Encoding=UTF-8    Name=Eclipse    Comment=Eclipse IDE    Exec=eclipse    Icon=/opt/eclipse/icon.xpm    Terminal=false    Type=A