Search code examples
androidandroid-layoutandroid-fragmentsandroid-fragmentactivityandroid-actionbar-tabs

How to populate a fragment layout dynamically?


I have a fragment with his class:

public class FormatShortFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

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

        return rootView;
    }
}

And this is his layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
    </LinearLayout>
</RelativeLayout>

This fragment is shown by a FragmentPageAdapter. The layout will grow when an user add an item from server (the origin of the item doesn't matter), and this item will be an horizontal linearlayout, with textview and edittext (item_name, item_value). This value can be modifiable, so must be an edittext, also, this edittext will receive values periodically from server, so can be updated automatically.

After explain this, how can I add this linearLayouts and his subviews to the GUI dynamically? and when should I update the interface?

Thanks a lot, any help is good received, I'm really lost on this on this themes.

EDITED WITH MODIFICATIONS:

I have modified the code in this way:

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

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

    return rootView;
}
@Override
public void onResume() {
    super.onResume();
    LinearLayout parentLinear = (LinearLayout) getActivity().findViewById(R.id.short_container);
    for (int i = 1; i<10; i++ ){
        LinearLayout innerLinear = (LinearLayout) new LinearLayout(getActivity());
        TextView itemName = new TextView(getActivity());
        itemName.setText("Nombre");
        EditText itemValue = new EditText(getActivity());
        itemValue.setText("Valor");
        innerLinear.addView(itemName);
        innerLinear.addView(itemValue);
        parentLinear.addView(innerLinear);
    }
}

@Override
public void onStart() {
    super.onStart();
}

@Override
public void onStop() {
    super.onStop();
}

And the fragment layout has been modified on this way:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">

<LinearLayout
    android:id="@+id/short_container"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
</LinearLayout>
</RelativeLayout>

Now the layout is growing in each for iteration.

In this point i have another problems, I hope you can help me with this. - How can I generate a LayouParams object with attributes like gravity or weigth? - How can I pass data from MainActivity to the Fragment? - And how can i force the Fragment view to be raloaded with new data?

Also i need to know how bind a clickListener event to programatically linearLayouts added, i need modify some values on server depending on the name and the edited value.

Thanks a lot for this, you are saving my life.


Solution

  • you Can easily Update UI of Fragment on Button click or any event just Like in Activity. You can get any view by

     getActivity().findViewById() 
    

    See Example, It will help you to undersatand:-

    public class FragmentTab3 extends SherlockFragment {
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragmenttab3, container, false);
            return rootView;
        }
    
        @Override
        public void onResume() {
            super.onResume();
        }
    
    
        @Override
        public void onStart() {
            super.onStart();
    
    
        LinearLayout linear = (LinearLayout) getActivity().findViewById(R.id.linear);
            Button button = new Button(getActivity());
            button.setText("Click Me");
            linear.addView(button);
        }
    
        @Override
        public void onStop() {
            super.onStop();
        }
    }