How to use Compose inside Fragment?

Nurseyit Tursunkulov

The documentation describes how to create UI (Jetpack Compose https://developer.android.com/jetpack/compose) inside Activity.

class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContent {
        Text("Hello world!")
    }
}

}

But how can I use it inside fragment?

veritas1

setContent on ViewGroup is now deprecated.

The below is accurate as of Compose v1.0.0-alpha01.

For pure compose UI Fragment:

class ComposeUIFragment : Fragment() {

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return ComposeView(requireContext()).apply {
            setContent {
                Text(text = "Hello world.")
            }
        }
    }
}

For hybrid compose UI Fragment - add ComposeView to xml layout, then:

class ComposeUIFragment : Fragment() {

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_compose_ui, container, false).apply {
            findViewById<ComposeView>(R.id.composeView).setContent {
                Text(text = "Hello world.")
            }
        }
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

how to use getSharedPreferences in Fragment?

how to use setSupportActionBar in fragment

How to use SupportMapFragment inside a Fragment?

How to use method onNewIntent(Intent intent) inside a Fragment?

How to use Anko DSL inside a Fragment?

Is it legal to use a static fragment inside another fragment?

How to use setOnTouchListener in a fragment?

How to use database with fragment

Cannot use show dialog function inside fragment?

use Edit text to search in recyclerview inside fragment

Relay: use constant inside Fragment instead of graphql`...`

How to set BottomNavigationView inside Fragment

How can I use an OnClickListener inside of a fragment to swap between the other fragments

How to implement a viewpager inside of a fragment?

How to create a fragment inside a dialog?

How can I use startActivity() inside a fragment in button.onClick() in android

How to display GridView inside a Fragment?

Can I use MVP design pattern inside TabLayout Fragment, if yes how to do this?

How use onBackPressed() inside fragment to back a webview?

how to use TablLayout inside Fragment?

How to use getSupportFragmentManager in fragment?

How to use setByte in a fragment?

how to create custom adapter to use cardview for list view inside fragment

How can I access and use button of one fragment inside another fragment?

How i can use RecyclerView inside a fragment

How to use docker-compose links to access username and password details from inside another container

How to use mutableStateListOf in compose

How to use Datastore in Compose?

How do I use RecyclerView with an array of TextViews inside a fragment?