Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jetpack UI Compose. How to create FloatingActionButton?

I want to create a FloatingActionButton placed at the bottom right in the activity using jetpack compose.
Can anyone help me with an example for the above scenario?

like image 325
affan ahmad Avatar asked Oct 24 '19 18:10

affan ahmad


People also ask

What is the declarative UI pattern of jetpack compose?

Key points of declarative UI pattern of Jetpack compose: Accelerate development : It is fully compatible with your existing code. Less Code : It does more with less code and avoids entire classes of bugs, so code is simple and easy to maintain. Powerful Tools : You can create attractive apps with access to the Android platform APIs.

How to get started with Jetpack compose for Android?

To get started with Jetpack Compose, a couple of Gradle dependencies need to be imported. Also need to add the compose flag as true, in the buildFeatures block within android: As Jetpack Compose exposes a programmatic way to build user interfaces, you won’t be using any XML.

What are the components of jetpack?

Jetpack Compose Jetpack Compose Overview Components Components Cards Cards Card Dialogs Dialogs AlertDialog Menus Menus DropdownMenu Progress indicators Progress indicators LinearProgressIndicator CircularProgressIndicator Selection controls Selection controls Checkbox RadioButton Switch Sliders Sliders Slider Snackbars Snackbars

How to create a popularflowerslist in jetpack?

Normally, to make a list you’d use something like a RecyclerView. Similar to it, a LazyRow is used in Jetpack Compose. It is a horizontal scrolling list that only composes and lays out the currently visible items. Now putting all together, creating PopularFlowersList ().


3 Answers

With 1.0.x to create a FloatingActionButton or the ExtendedFloatingActionButton you can use something like:

val onClick = { /* Do something */ }

//Simple FAB
FloatingActionButton(onClick = onClick) {
    Icon(Icons.Filled.Add,"")
}

//FAB custom color
FloatingActionButton(
    onClick = onClick,
    backgroundColor = Color.Blue,
    contentColor = Color.White
){
    Icon(Icons.Filled.Add,"")
}

//Square FAB
FloatingActionButton(
    onClick = onClick,
    shape = RectangleShape
){
    Icon(Icons.Filled.Add,"")
}

//EXTENDED FAB
ExtendedFloatingActionButton(
    text = {  Text(text = "EXTENDED FAB") },
    onClick = onClick,
    icon ={ Icon(Icons.Filled.Add,"")}
)

//EXTENDED FAB WITHOUT ICON
ExtendedFloatingActionButton(
    text = {
        Text(text = "EXTENDED FAB")
    },
    onClick = onClick
)

enter image description here

Example:

Scaffold(topBar = { } ,
    //floatingActionButtonPosition = FabPosition.End,
    floatingActionButton = {
        FloatingActionButton(
            onClick = {}
        ) {
            Icon(Icons.Filled.Add,"")
        }
    }
    , content = {
        //....
    })

enter image description here

like image 104
Gabriele Mariotti Avatar answered Oct 13 '22 03:10

Gabriele Mariotti


Use Scaffold

Scaffold provides slots for the most common top-level Material components, such as TopAppBar, BottomAppBar, FloatingActionButton, and Drawer. By using Scaffold, it's easy to make sure these components are properly positioned and work together correctly.

Syntax

@Composable
fun HomeContent() {
    Scaffold(
        topBar = {/**/ },
        drawerContent = {/**/ },
        bottomBar = {/**/ },
        floatingActionButton = {/**/ },
        snackbarHost = {/**/ },
        content = {/**/ }
    )
}

Example

@Composable
fun HomeContent() {
    Scaffold(
        topBar = {
            TopAppBar(title = { Text("Title") })
        },
        floatingActionButton = {
            FloatingActionButton(
                onClick = { /*TODO*/ },
                backgroundColor = Color.Red,
                content = {
                    Icon(
                        painter = painterResource(id = R.drawable.ic_add),
                        contentDescription = null,
                        tint = Color.White
                    )
                }
            )
        },
        content = {
            Surface(modifier = Modifier.padding(24.dp)) {
                Text(
                    text = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.",
                    fontSize = 16.sp,
                )
            }
        }
    )
}

Output

enter image description here

like image 11
Ngima Sherpa Avatar answered Oct 13 '22 04:10

Ngima Sherpa


You can create FloatingActionButton try below @Compose function use your drawable icon.

@Composable
fun MyFloatingActionButton() {
      val icon = +imageResource(R.drawable.ic_add_icon)
            FloatingActionButton(icon = icon, color = Color.Red, elevation = 8.dp)
}
like image 6
Anas Mehar Avatar answered Oct 13 '22 02:10

Anas Mehar