Android jetpack composable button example

import android.os.Bundle
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.material.Button
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.tooling.preview.Preview

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            ComposableBtn("Click me")
        }
    }
}

@Composable
fun ComposableBtn(text: String) {
    val context = LocalContext.current
    Button(
        onClick = {
            Toast.makeText(context, "Hello composable button", Toast.LENGTH_LONG).show()
        },
    )
    {
        Text(text = text)
    }
}

@Preview(showBackground = true)
@Composable
fun ComposableBtnPreview() {
    ComposableBtn("Click me")
}

Search within Codexpedia

Custom Search

Search the entire web

Custom Search