Anko: Add example of embedded fragment in activity

Created on 12 May 2015  路  6Comments  路  Source: Kotlin/anko

I searched a bit and I couldn't seem to figure out how to embed a fragment in my activity.

For example I want to do something like this.

frameLayout {
    linearLayout {
        baselineAligned = false
        orientation = LinearLayout.HORIZONTAL

        fragment {
            name = "FragmentClass"
        }.layoutParams(width = matchParent, height = matchParent)

    }.layoutParams(width = matchParent, height = matchParent)
}
fixed question

All 6 comments

Is there a workaround for this? Would a custom inline function that accomplishes this be difficult to make?

This would be a nice little feature, since as far as I know, the only solution is a "long" piece of code (at least compared to how much anko tries to save us), see: http://stackoverflow.com/questions/18296868/how-to-add-a-fragment-to-a-programmatically-generated-layout

I think the problem is that this does not conform to just creating a view like the rest of the dsl...

But for those that are lazy like me, we are force to still keep some xml layouts around just because of this feature not being implemented...

Nice job to the anko team anyways for all the rest of the features they give us!!

Unfortunately, it's impossible to create a fragment in Android without an explicit class declaration.

@yanex I don't think anyone is saying to create a fragment without an explicit class declaration. In Android xml layouts, you can insert a <fragment> tag directly, instead of inserting a container element and then using FragmentManager to inject the fragment into the placeholder.

Both methods require explicit Fragment classes. I believe this issue was created because we couldn't figure out how to translate XML layouts with <fragment> tags into Anko language.

I included in my example of what it might look like while referencing the class.

I've created the new issue for this: #362.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

AndwareSsj picture AndwareSsj  路  4Comments

lupajz picture lupajz  路  3Comments

s949492225 picture s949492225  路  4Comments

SUPERCILEX picture SUPERCILEX  路  4Comments

SuleymanovTat picture SuleymanovTat  路  4Comments