Question

I'm quite new to Android programming and stuck at the following Issue.

I developed a tab-based App by using (map-)Fragments.

Now I want to add Checkboxes dynamically (source: database) to one of the Tabs, but do not know how and to which object!

My basis-coding for the Fragment looks like this:

public class SettingsFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_set, container, false);
    return rootView;
}
}

And the aim is, to insert something like that (example from web):

    ScrollView sv = new ScrollView(this);
    LinearLayout ll = new LinearLayout(this);
    ll.setOrientation(LinearLayout.VERTICAL);
    sv.addView(ll);

    for(int i = 0; i < 20; i++) {
    CheckBox cb = new CheckBox(this);
    cb.setText("check");
    ll.addView(cb);
    }
    this.setContentView(sv);

One additional question: the Fragment-concept seems to me quite complex - is it usefull to use in context with tab-layout including maps, or is there another (more easy) approach?

Thanks in advance! Juergen

Was it helpful?

Solution

You can use this:

ScrollView sv = new ScrollView(getActivity());
sv .setLayoutParams(new LayoutParams(
                        LayoutParams.MATCH_PARENT,
                        LayoutParams.MATCH_PARENT));
LinearLayout ll = new LinearLayout(getActivity());
ll.setLayoutParams(new LayoutParams(
                        LayoutParams.MATCH_PARENT,
                        LayoutParams.WRAP_CONTENT));
ll.setOrientation(LinearLayout.VERTICAL);
sv.addView(ll);

for(int i = 0; i < 20; i++) {
CheckBox cb = new CheckBox(getActivity());
ll.setLayoutParams(new LayoutParams(
                        LayoutParams.WRAP_CONTENT,
                        LayoutParams.WRAP_CONTENT));
cb.setText("check");
ll.addView(cb);
}
setContentView(sv);

OTHER TIPS

   b.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        for(int i = 0; i < 20; i++) {
            CheckBox cb = new CheckBox(getApplicationContext());
            cb.setText("I'm dynamic!");
            ll.addView(cb);
        }
    }
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top