OK, I'm trying to create a universal application that displays 3 tabs in the form of a phone and 3 columns in a tablet. Thus, I created several different XML layouts using Linear Layout.
However, using fragments, I ran into a problem - I want to delete the leftmost column when a selection is made in this fragment. I figured I could do this, but it turned out that I could not remove the fragments declared in XML. So now I'm trying to create the middle and right columns in XML:
<?xml version="1.0" encoding="utf-8"?> <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" > <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" > </fragment> <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" /> </LinearLayout>
Then add the first column using
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();
but I have two problems:
a) The above code only places the snippet on the right side of the line layout, and I cannot specify the position. It should be on the left.
b) XML gave me the freedom to define different weights for a fragment based on screen size / orientation. Setting the layout options programmatically in Fragments looks like a step back. I got around this by putting a placeholder view in the xml in which I read the layout options from this and assigned them to the onCreateView, but it was hacked.
I know that I can put the fragment in the right place by adding it as a child of the first element in xml, but when I do this and delete the fragment, it leaves empty space.
source share