Tuesday, January 30, 2018

How to add a fragment to an Activity without a container

Leave a Comment

Is that possible I can add a fragment view on the activity view without specifying "fragment" view component in activity's layout xml file? Which function should I look for?

9 Answers

Answers 1

Well, the UI of the fragment has to go somewhere. If you want the entire "content view" to be the fragment, add the fragment to android.R.id.content:

  @Override   protected void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);      if (getSupportFragmentManager().findFragmentById(android.R.id.content)==null) {       getSupportFragmentManager().beginTransaction()         .add(android.R.id.content, new ToDoRosterListFragment())         .commit();     }   } 

Otherwise, somewhere in the activity's view hierarchy, you need a container (usually a FrameLayout) in which to place the fragment's UI. Typically, we do that by putting the container in the layout resource.

Answers 2

@Override protected void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);          getSupportFragmentManager().beginTransaction()             .add(android.R.id.content, MyFragment.newInstance())             .commit();     //Some your code here } 

android.R.id.content is the container of entire app screen. It can be used with Fragment:

The android.R.id.content ID value indicates the ViewGroup of the entire content area of an Activity.

The code above will insert the View created by Fragment into the ViewGroup identified by android.R.id.content.

Answers 3

You need to have a layout in your activity to contain the fragment (preferably a FrameLayout).

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container" android:name="com.gdevelopers.movies.movies.FragmentMoreMovies" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior" /> 

Then in activity put this code.

@Override protected void onCreate(Bundle savedInstanceState) {   super.onCreate(savedInstanceState);    getSupportFragmentManager().beginTransaction()     .replace(R.id.container, new YourFragment())     .commit();   } } 

Answers 4

If you don't want to go with @CommonsWare answer, then you'll have to provide a container by code. then function you need is....

setContentView(View) 

Yep. If you check the activity class code, you'll see that setContentView can be called with an INT (layouts are identified with ints), or with Views. Therefore, you can create a viewgroup instance on the fly, keep a reference to it (you would need to do the same with an XML generated view), and add your fragments there. This is possible because XML files are just arguments which the view factory class, Inflater, uses to find which view subclasses has to instantiate, using a set of parameters provided in the XML. And obviously, you can do that by hand. Just pick whatever layout class you want to use, for example, FrameLayout, and:

public class Activity extends AppCompatActivity{  private FrameLayout root;  @Override public void onCreate(Bundle savedInstanceState){     super.onCreate(savedInstanceState);     root = new FrameLayout(this);   root.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));     setContentView(root); //go on }  } 

Answers 5

Simply, while creating a fragment we have to replace or add fragment's view with a view present in our application. To replace or add a fragment's, we normally add a Framelayout or any other layout view(as a fragmentview container) in activity or in a fragment.

Now, If you want to replace or add a fragment's view without adding a extra view container in your activity. You can simply do it by accessing the view's provided by AppCompatActivity or Activity.

Now, you can create a fragment, without adding a view container in your activity you can create as,

YourFragment fragment = new YourFragment(); transaction = getSupportFragmentManager().beginTransaction(); transaction.replace(android.R.id.content, fragment);  //here, android.R.id.content is a view on which your fragment's view is replaced transaction.commit(); 

Answers 6

use android container instead of custom container like

fragmentTransaction.replace(android.R.id.content,yourFragment); 

Answers 7

1.use getWindow().getDecorView() to get a DecorView(FramLayout)
2.add a container view to DecorView
3.add Fragment to the container view

Answers 8

LinearLayout llRoot = findViewById(R.id.parent);  FragmentManager fragMan = getSupportFragmentManager(); FragmentTransaction fragTransaction = fragMan.beginTransaction(); YourFragment yourFragment = (YourFragment)fragMan.findFragmentByTag("yourFragment"); if (yourFragment == null) {   yourFragment = new YourFragment(); } fragTransaction.replace(llRoot.getId(), yourFragment, "yourFragment"); fragTransaction.commit(); 

llRoot is a LinearLayout which contains different view object of your activity

Answers 9

If you don't want to allocate a specific place in the view to the fragment container you can always use the RelativeLayour. I guess without a container we cant place a fragment in a view.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment