1

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.

icbytes
  • 1,831
  • 1
  • 17
  • 27
  • I believe you get null from getView() because you don't provide View in onCreateView in Fragment.Try to return simple FrameLayout and add you layout to it. – Bracadabra Oct 31 '13 at 13:52
  • What's the difference between your last question and this one? – user Oct 31 '13 at 13:54
  • This question only focuses on Layout of fragment. As I create everything dynamically, i must not use any xml afaik. The former question focussed on "when to place subelements" into fragment layout. If You want, I had ask this question prior to the other one. – icbytes Oct 31 '13 at 13:56
  • @ Vang: What Do You mean ? I do not provide view in onCreateView ? How should I do this? I do not have any id to be extracted by the inflater in order to return the identified "view". – icbytes Oct 31 '13 at 13:59

2 Answers2

5
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);
    }
}
Jay Rathod
  • 11,131
  • 6
  • 34
  • 58
MagicBeamer
  • 256
  • 1
  • 2
  • THX very much, the button gets added... but i do not see it. By the way, i do not have any layout inside my fragment..... how can I add one during the instantiation process ? – icbytes Oct 31 '13 at 16:16
  • Let me try this tomorrow, then i will react – icbytes Oct 31 '13 at 18:20
  • I still cannot get it to work.I do not see my fragment. I noticed,that onViewCreated is called due to setContentView of main activity(fragment activity). And in onViewCreated I am adding a layout to the fragment and buttons with layout.I also set blue bgcolor in onViewCreated.Before onViewCreated is called,I entirely delete the backstack of the fragmentmanager and only insert the reference to the instantiated fragment. I am wondering,that the order is not right. So I went to onCreate of main activity and tried to instantiate a frgament without xml layout there. Does anybody have a example ? – icbytes Nov 01 '13 at 10:45
  • No, I did it, no need anymore. But I have other problems. The fragment layout is of type absolute ( i will work with absolute, no need to comment about deprecation ). I create it with new LayoutParams. There is nothing really happening when the onViewCreated is called. No Button is being created, no background-color is changed, I cannot change the size of the fragment.... is suppose, i am too late, i surely will have to notify the fragmentactivity to redraw itself. perhaps i also should us another Eventhandler of fragmentactivity – icbytes Nov 01 '13 at 11:02
  • All right. Works. BUT one must force a redraw, i did it via detach and attach again immediately after adding to fragment manager. Would like to know if it can be done otherwise. – icbytes Nov 03 '13 at 09:52
0

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

}
prototype0815
  • 592
  • 2
  • 7
  • 24