Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android 3 - Adding a Fragment to a LinearLayout: fill_parent does not work

I am trying to add a fragment programmatically to a LinearLayout, but the fragment does not stretch its content across the whole layouts height. It acts like wrap_content instead of fill_parent.

On the other side the fill_parent works on the fragment's width. How can I change this behaviour?

DashboardActivity:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <fragment
        android:id="@+id/fragment_dashboard"
        android:layout_width="5dp"
        android:layout_height="fill_parent"
        android:layout_marginLeft="20dp"
        android:layout_weight="1"
        android:name="de.upb.cs.ginkgo.tablet.ui.fragments.DashboardFragment" >
        <!-- Preview: layout=@layout/fragment_dashboard -->
        </fragment>

    <LinearLayout
        android:id="@+id/rightfrag"
        android:layout_width="450dp" 
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="15dp"
        android:layout_marginRight="10dp" >
    </LinearLayout>

</LinearLayout>

Its onCreate method:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_dashboard);
    //-- Instantiate Fragmentstuff
    FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    ActivitystreamFragment asf = new ActivitystreamFragment();
    fragmentTransaction.add(R.id.rightfrag, asf);
    fragmentTransaction.commit();
    } 

The ActivitystreamFragment XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1">

    <ListView
        android:id="@+id/android:list"
        android:layout_width="0dp" 
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:drawSelectorOnTop="true"
        android:background="@drawable/back_rightfrag"
         >
</ListView>
</LinearLayout>

And its createView:

 public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_activitystream, null);
        return root;
        }
like image 880
caiuspb Avatar asked Jan 26 '12 14:01

caiuspb


People also ask

How add and remove fragments?

Adding and removing fragments. To add a fragment to a FragmentManager , call add() on the transaction. This method receives the ID of the container for the fragment, as well as the class name of the fragment you wish to add.

How does fragment work in Android?

A fragment has its own layout and its own behaviour with its own life cycle callbacks. You can add or remove fragments in an activity while the activity is running. You can combine multiple fragments in a single activity to build a multi-pane UI. A fragment can be used in multiple activities.

How do you align text in LinearLayout?

If in linearlayout your orientation vertical, you can put the textview in the "horizontal center" by android:layout_gravity="center" . For centering textview vertically you need to set layout_height of textview to match_parent and set android:gravity to "center".

What is LinearLayout in Android with example?

LinearLayout is a view group that aligns all children in a single direction, vertically or horizontally. You can specify the layout direction with the android:orientation attribute. Note: For better performance and tooling support, you should instead build your layout with ConstraintLayout.


1 Answers

Ok - this one was pretty dumb. I changed my container from a LinearLayout to a FrameLayout and voilà: it's working.

like image 101
caiuspb Avatar answered Sep 23 '22 23:09

caiuspb