Iriminage
Iriminage

Reputation: 177

How to add fragments to activity programatically without XML

I have an activity without xml layout. Now I'm adding one fragment into activity by:

protected void onCreate(Bundle savedInstanceState) {
    profileFragment = new ProfileFragment();
    getSupportFragmentManager().beginTransaction().add(android.R.id.content, profileFragment).commit(); 
}

First thing is that I dont know what is R.id.content, because i didn't declare it, so where it is from?

Second, how could I add next 2 fragments into activity and place it where I want without xml layout for main activity (first fragment on top, and next 2 fragments side by side below first fragment)?

Upvotes: 3

Views: 1340

Answers (1)

Blackbelt
Blackbelt

Reputation: 157457

First thing is that I dont know what is R.id.content, because i didn't declare it, so where it is from?

it is not R.id.content but android.R.id.content, and it is part of the framework

Second, how could I add next 2 fragments into activity and place it where I want without xml layout for main activity (first fragment on top, and next 2 fragments side by side below first fragment)?

you can create the container(s) for the fragment(s) programmatically (you need to tell Android where do you want the Fragments placed)

Upvotes: 3

Related Questions