Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>At some point, I suppose you will add your programatically created LinearLayout to some root layout that you defined in .xml. This is just a suggestion of mine and probably one of many solutions, but it works: Simply <strong>set an ID for the programatically created layout</strong>, and add it to the root layout that you defined in .xml, and then <strong>use the set ID to add the Fragment.</strong></p> <p>It could look like this:</p> <pre><code>LinearLayout rowLayout = new LinearLayout(); rowLayout.setId(whateveryouwantasid); // add rowLayout to the root layout somewhere here FragmentManager fragMan = getFragmentManager(); FragmentTransaction fragTransaction = fragMan.beginTransaction(); Fragment myFrag = new ImageFragment(); fragTransaction.add(rowLayout.getId(), myFrag , "fragment" + fragCount); fragTransaction.commit(); </code></pre> <p>Simply <strong>choose whatever Integer value you want</strong> for the ID:</p> <pre><code>rowLayout.setId(12345); </code></pre> <p>If you are using the above line of code not just once, it would probably be smart to <strong>figure out a way to create unique-IDs</strong>, in order to <strong>avoid duplicates</strong>.</p> <p><strong>UPDATE:</strong></p> <p><em>Here is the full code of how it should be done:</em> (this code is tested and works) I am adding two Fragments to a LinearLayout with horizontal orientation, resulting in the Fragments being aligned next to each other. Please also be aware, that <strong>I used a fixed height and width</strong> of 200dp, so that <strong>one Fragment does not use the full screen</strong> as it would with "match_parent".</p> <p><strong>MainActivity.java:</strong></p> <pre><code>public class MainActivity extends Activity { @SuppressLint("NewApi") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); LinearLayout fragContainer = (LinearLayout) findViewById(R.id.llFragmentContainer); LinearLayout ll = new LinearLayout(this); ll.setOrientation(LinearLayout.HORIZONTAL); ll.setId(12345); getFragmentManager().beginTransaction().add(ll.getId(), TestFragment.newInstance("I am frag 1"), "someTag1").commit(); getFragmentManager().beginTransaction().add(ll.getId(), TestFragment.newInstance("I am frag 2"), "someTag2").commit(); fragContainer.addView(ll); } } </code></pre> <p><strong>TestFragment.java:</strong></p> <pre><code>public class TestFragment extends Fragment { public static TestFragment newInstance(String text) { TestFragment f = new TestFragment(); Bundle b = new Bundle(); b.putString("text", text); f.setArguments(b); return f; } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.fragment, container, false); ((TextView) v.findViewById(R.id.tvFragText)).setText(getArguments().getString("text")); return v; } } </code></pre> <p><strong>activity_main.xml:</strong></p> <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/rlMain" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="5dp" tools:context=".MainActivity" &gt; &lt;TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /&gt; &lt;LinearLayout android:id="@+id/llFragmentContainer" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignLeft="@+id/textView1" android:layout_below="@+id/textView1" android:layout_marginTop="19dp" android:orientation="vertical" &gt; &lt;/LinearLayout&gt; &lt;/RelativeLayout&gt; </code></pre> <p><strong>fragment.xml:</strong></p> <pre><code> &lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="200dp" android:layout_height="200dp" &gt; &lt;TextView android:id="@+id/tvFragText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:text="" /&gt; &lt;/RelativeLayout&gt; </code></pre> <p>And this is the result of the above code: (the two Fragments are aligned next to each other) <img src="https://i.stack.imgur.com/7kDaB.png" alt="result"></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload