Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid programmatically add fragment - position in parent and layout
    primarykey
    data
    text
    <p>OK, I'm trying to create a universal app that shows 3 tabs in phone view and 3 columns in tablet landscape. As such I created several different XML layouts using Linear Layout.</p> <p>However, using fragments, I came across a problem - I want to remove the leftmost column when a selection is made in that fragment. I believed I could do this but it turns out I can't remove fragments declared in XML. So now, I'm tring to create the middle and right columns in XML:</p> <pre><code> &lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/menu_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" &gt; &lt;fragment android:id="@+id/classification_fragment" android:name="uk.colessoft.android.hilllist.fragments.MenuClassificationFragment" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" tools:layout="@layout/list_classifications" &gt; &lt;/fragment&gt; &lt;fragment android:id="@+id/map" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" class="com.google.android.gms.maps.SupportMapFragment" /&gt; &lt;/LinearLayout&gt; </code></pre> <p>Then add the first column using </p> <pre><code>FragmentManager fragmentManager = getSupportFragmentManager(); android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); MenuCountryFragment countryFragment=new MenuCountryFragment(); fragmentTransaction.add(R.id.menu_layout, countryFragment, "country_menu").commit(); </code></pre> <p>but I have two problems:</p> <p>a) The above code only puts the fragment at the right hand side of the Linear Layout and I can't specifiy position. It needs to be on the left.</p> <p>b) XML gave me the freedom to define different weights for the fragment based on screen size/orientation. Setting layout parameters programmatically in the Fragments seems a backward step. I got round this by putting a placeholder view in the xml that I read the layout params from this and assign them in onCreateView but its a hack.</p> <p>I know I can put the fragment in the correct place by adding it as a child of the first element in the xml but when I do this and remove the fragment it leaves a blank space.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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