Adding dynamic views in a fragment

Have an empty xml layout with just a RelativeLayout called “mainLayout”(or what ever you want to call it). That way you can append any dynamically generated controls

 

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

    Button myButton = new Button(Builtprofile.context);
    myButton.setText("Press me");
    myButton.setBackgroundColor(Color.YELLOW);

    RelativeLayout.LayoutParams buttonParams = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT, 
                RelativeLayout.LayoutParams.WRAP_CONTENT);

    buttonParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
    buttonParams.addRule(RelativeLayout.CENTER_VERTICAL);

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

    RelativeLayout myLayout = (RelativeLayout)rootView.findViewById(R.id.mainLayout);
    myLayout.setBackgroundColor(Color.BLUE);

    myLayout.addView(myButton, buttonParams);

    return rootView;
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s