Question

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.

Was it helpful?

Solution

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);
    }
}

OTHER TIPS

I got the same Problem and to my amzement I found a solution! Create a blank XML layout file like this...

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
</FrameLayout>

In the fragment where your layout is dynamically created, inflate this blank layout XML file.

  @Override
  public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup     container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.blank_layout, container, false);
    return view;

Afterwords, in onViewCreated() method you can create your layout dynamically.

  @Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
  super.onViewCreated(view, savedInstanceState);

// This will create the LinearLayout
LinearLayout ll = new LinearLayout(mContext);
ll.setOrientation(LinearLayout.VERTICAL);

// Configuring the width and height of the linear layout.
LinearLayout.LayoutParams llLP = new LinearLayout.LayoutParams(
        //android:layout_width="match_parent" an in xml
        LinearLayout.LayoutParams.MATCH_PARENT,
        //android:layout_height="wrap_content"
        LinearLayout.LayoutParams.MATCH_PARENT);
ll.setLayoutParams(llLP);

TextView tv = new TextView(mContext);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
        LinearLayout.LayoutParams.MATCH_PARENT,
        LinearLayout.LayoutParams.WRAP_CONTENT);

tv.setLayoutParams(lp);
//android:text="@string/c4r"
tv.setText("Hello android !");
//android:padding="@dimen/padding_medium"
tv.setPadding(8, 8, 8, 8);
ll.addView(tv);

ViewGroup viewGroup = (ViewGroup) view;
viewGroup.addView(ll);
}

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top