Musterknabe
Musterknabe

Reputation: 6081

Add layouts to a fragment programmaticcaly

Introduction I want to have different fragments depending on the user's choice from the navigation drawer. For every item in the navigation drawer is a different site that should called. I want to achieve this by fragments. The main fragment will get displayed first by doing the following in the onCreate-method

if ( savedInstanceState == null ) {
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    Fragment fragment = MainFragment.passList(hashMap);
    ft.replace(R.id.content_frame, new MainFragment());
    ft.commit();
}

As you can see I'm passing data (a hashmap) to a method of the fragment which works perfectly. The data then gets put into string arrays. This works and the data can also be used in the onCreate-method of the fragment.

This is the xml of the main activity

    <android.support.v4.widget.DrawerLayout 

    xmlns:android="http://schemas.android.com/apk/res/android"
       android:id="@+id/drawer_layout"
       android:layout_width="match_parent"
       android:layout_height="match_parent" >

 <FrameLayout
            android:id="@+id/content_frame"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    <ListView
       android:id="@+id/left_drawer"
       android:layout_width="240dp"
       android:layout_height="match_parent"
       android:layout_gravity="start"
       android:choiceMode="singleChoice"
       android:divider="@android:color/darker_gray"
       android:dividerHeight="0.1dp" />

</android.support.v4.widget.DrawerLayout>

And this is the XML of the main fragment

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mainRelativeLayout"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:background="?android:attr/activatedBackgroundIndicator"
   android:gravity="center_vertical"
   android:paddingRight="10dp"
   >
<ScrollView
        android:id="@+id/mainScrollView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1.03" >

        <LinearLayout
            android:id="@+id/mainListView"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >
        </LinearLayout>
    </ScrollView>
    </RelativeLayout>

What I want to achieve now is to display data in the scroll view. But the question I have, is how do I populate that and how do I initliaze it?

Normally I would go and just create them like that - because findViewById doesn't work in a Fragment (Have to write the method by my own)

ScrollView sv = new ScrollView(getActivity());
LinearLayout ll = new LinearLayout ( getActivity() );

sv.addView(ll); 
ll.addView(SAMPLETEXTVIEW);

View mainView = inflater
            .inflate(R.layout.main_fragment, container, false);
    return mainView;

but that just leaves me with a blank screen and no actual text added. How can I achieve this? The data is there - I know that, because it gets printed out with System.out.println - I just need a way to display it. Thx in advance!

EDIT

When I do it like that

View rootView = inflater.inflate(R.layout.main_fragment, container, false);

    ScrollView sv = (ScrollView) rootView.findViewById(R.id.mainScrollView);
    LinearLayout ll = (LinearLayout) rootView.findViewById(R.id.mainListView);

    sv.addView(ll); 

I get just an IllegalStateException that ScrollView can only host one direct child... But I don't get it. The linearlayout is the only thing that is the direct child. Why is there a problem?

Here is the complete onCreateView-method

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.main_fragment, container, false);

    ScrollView sv = (ScrollView) rootView.findViewById(R.id.mainScrollView);
    LinearLayout ll = (LinearLayout) rootView.findViewById(R.id.mainListView);

    sv.addView(ll); 
    source      = new TextView[hashMapSize];


    for (int i = 0; i < hashMapSize; i++) {

        source[i]       = new TextView(getActivity());
source      [i].setText( "Source: "     + sourceArr     [i] );
ll.addView(source[i]

    }
    return rootView;
}

Upvotes: 0

Views: 116

Answers (1)

linakis
linakis

Reputation: 1231

The reason you get the IllegalStateException is because you inflate your R.layout.main_fragment which already contains your ScrollView that already has one direct child (declared in you layout xml).

You shouldn't add in code with sv.addView(ll) it's already there. All you need to do is get the references as you already do with findViewById.

Remove the sv.addView(ll); and you should be fine.

Upvotes: 1

Related Questions