Add views( buttons, labels, etc ) to a dynamic fragment without using any ressource xml

icbytes picture icbytes · Oct 31, 2013 · Viewed 8.4k times · Source

First of all I would like to say, that I want to say "Hello in here".

Requirements:

I should make it possible to create a client application which gets metadata about controls from a database. This application should be able to switch from one view (with subviews, like buttons for example ) to another view.

Status:

I created a relatively huge development oo model, using interfaces, and subclasses (of button, for example), which all implement special own interfaces in order to react properly to my demands. I read about fragment, fragmentactivity and fragments, i must use v4 compatibility classes, so my activity inherits from FragmentActivity and implements some special own interfaces. I am now at the point, where a controller class, which is the only reference in my FragmentActivity class, does many things and finally should make the fragment visible.

I also have already collected those subviews(buttons, labels, textviews ) in a collection, and each runtime created fragment should now "place its subviews" onto the screen. Remember, my custom view "Fragment" inherits from Fragment and implements some special things.

My fragment is getting created at runtime, so I do not have any xml which defines any layout.

Questions:

a) is it possible for me working without an xml-layout and apply any layout to the fragment programatically and at runtime ? My first attempts using an Layout declared globally in my custom fragment class did not succeed because I wanted to call getView() during several states BUT always got null. Therefore I must ask

b) question b ( only when a == true ) when and how can I receive the correct view from getView in order to set the layout programatically ?

THX in advance.

Answer

MagicBeamer picture MagicBeamer · Oct 31, 2013
package com.example.test;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;


public class FragmentExample extends android.app.Fragment
{

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState)
    {
        // TODO Auto-generated method stub
        super.onViewCreated(view, savedInstanceState);

        //Set a linearLayout to add buttons
        LinearLayout linearLayout = new LinearLayout(getActivity());
        // Set the layout full width, full height
        LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        linearLayout.setLayoutParams(params);
        linearLayout.setOrientation(LinearLayout.HORIZONTAL); //or VERTICAL

        Button button = new Button(getActivity());
        //For buttons visibility, you must set the layout params in order to give some width and height: 
        LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        button.setLayoutParams(params);

        Button button2 = new Button(getActivity());
                button2.setLayoutParams(params);
        //... and other views

        ViewGroup viewGroup = (ViewGroup) view;

        linearLayout.addView(button);
        linearLayout.addView(button2);

        viewGroup.addView(linearLayout);
    }
}