Marcus Gabilheri
Marcus Gabilheri

Reputation: 1289

Layouts inside fragment not rendering

I have an Fragment Layout which loads fine but the elements of the layout are been added programatically because I don't know how many tabs I need for this layout.

My problem is that I can get the child count of the Linear Layout and they are all supposed to be added but nothing is displayed :(

Here is what I tried:

Here is the code for my onCreateView method:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    view = inflater.inflate(R.layout.descriptive_tables_tabs, null, true);
    tabsLayout = (LinearLayout) view.findViewById(R.id.myTabsRow);
    args = this.getArguments();
    toLoadTabs = (FrameLayout) view.findViewById(R.id.toLoadTable);
    tabButtons = new ArrayList<Button>();
    tabTitles = args.getStringArrayList("tabTitles");
    Log.i(LOG_TAG, "Tabs Title Size: " + tabTitles.size());
    float weight = 1.0f / tabTitles.size();
    Log.i(LOG_TAG, "WEIGHT: " + weight);
    for (String s : tabTitles) {
        Button mButton = (Button) inflater.inflate(R.layout.tab_button, null, true);
        View separator = inflater.inflate(R.layout.separator, null, true);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, weight);
        mButton.setLayoutParams(params);
        mButton.setText(s);
        tabButtons.add(mButton);
        Log.i(LOG_TAG, "Tabs: " + s);
        tabsLayout.addView(mButton);
        tabsLayout.addView(separator);
    }
    Log.i(LOG_TAG, "Layout Childs: " + tabsLayout.getChildCount());
    return view;
}

Here is the LOGCAT info that shows that the data is being passed and the childs exist..

06-02 17:47:37.314  14000-14000/com.example.sbirafphase2 I/DESCRIPTIVE TABLES FRAGMENT﹕ Tabs Title Size: 8
06-02 17:47:37.314  14000-14000/com.example.sbirafphase2 I/DESCRIPTIVE TABLES FRAGMENT﹕ WEIGHT: 0.125
06-02 17:47:37.319  14000-14000/com.example.sbirafphase2 I/DESCRIPTIVE TABLES FRAGMENT﹕ Tabs: FlyingHours
06-02 17:47:37.319  14000-14000/com.example.sbirafphase2 I/DESCRIPTIVE TABLES FRAGMENT﹕ Tabs: QuantityPerAssembly
06-02 17:47:37.319  14000-14000/com.example.sbirafphase2 I/DESCRIPTIVE TABLES FRAGMENT﹕ Tabs: UseFactor
06-02 17:47:37.319  14000-14000/com.example.sbirafphase2 I/DESCRIPTIVE TABLES FRAGMENT﹕ Tabs: Failures
06-02 17:47:37.319  14000-14000/com.example.sbirafphase2 I/DESCRIPTIVE TABLES FRAGMENT﹕ Tabs: ManHours
06-02 17:47:37.324  14000-14000/com.example.sbirafphase2 I/DESCRIPTIVE TABLES FRAGMENT﹕ Tabs: Aborts
06-02 17:47:37.324  14000-14000/com.example.sbirafphase2 I/DESCRIPTIVE TABLES FRAGMENT﹕ Tabs: MeanTimeBetweenFailures
06-02 17:47:37.324  14000-14000/com.example.sbirafphase2 I/DESCRIPTIVE TABLES FRAGMENT﹕ Tabs: RepairAndMaintenanceCritical
06-02 17:47:37.324  14000-14000/com.example.sbirafphase2 I/DESCRIPTIVE TABLES FRAGMENT﹕ Layout Childs: 16

Here is a screenshot of the problem, the gray area is where my tabs are supposed to go :( Here is a screenshot of the problem

Thanks!

EDIT - Adding the XML layouts that are been used:

descriptive_tables_tab.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="100">

    <LinearLayout
        android:id="@+id/myTabsRow"
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="7"
        android:background="@color/light_bg"
        android:orientation="horizontal"
        android:weightSum="1">

    </LinearLayout>

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="93"
        android:background="@color/light_bg">

        <FrameLayout
            android:id="@+id/loadTabFrag"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/light_bg"
            android:orientation="horizontal"></FrameLayout>
    </ScrollView>
</LinearLayout>

And here is the tab_button.xml

<?xml version="1.0" encoding="utf-8"?>
<Button
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="0dip"
    android:layout_height="match_parent"
    android:background="@drawable/tab_button_selector"
    android:gravity="center_vertical|center_horizontal"
    android:onClick="changeTab"
    android:paddingLeft="15dip"
    android:layout_weight="0.1"
    android:text="@string/model_information"
    android:textColor="@color/dark_blue_bg"
    android:textSize="@dimen/tab_title" />

EDIT: The layout itself is not rendering at all. The background of the tabs row should be white and match the rest of the fragment.

Upvotes: 0

Views: 732

Answers (2)

wvdz
wvdz

Reputation: 16641

It's hard to tell without the xml of R.layout.descriptive_tables_tabs, but the problem must be with R.id.myTabsRow.

For instance, if width was set to wrap_content, it would be invisible. This is because the child views are set to 0 width with a weight. This will only claim the space available, which will be 0 in this case.

Edit:

From the added xml, it seems that the problem is indeed in R.id.myTabsRow:

android:layout_height="0dip"

Setting this to wrap_content should solve it.

Upvotes: 0

Marcus Gabilheri
Marcus Gabilheri

Reputation: 1289

So I finally figured it out.... For some reason the Layout was only accepting one child (or was only rendering it because the child count was 16....) SO what I did was use a TableLayout for the the R.id.myTabsRow;

And inside my Fragment onCreateView I created a TableRow and added all the childs to the TableRow and after the loop I added the row to the TableLayout and now everything is rendering fine!!

Here is my final Working code in case it is helpful to someone:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    args = this.getArguments();
    tabTitles = args.getStringArrayList("tabTitles");
    view = inflater.inflate(R.layout.descriptive_tables_tabs, null);
    tabsLayout = (TableLayout) view.findViewById(R.id.myTabsRow);

    toLoadTabs = (FrameLayout) view.findViewById(R.id.toLoadTable);
    tabButtons = new ArrayList<Button>();
    //ArrayList<DescriptiveTablesFragment> tables = (DescriptiveTablesFragment) args.getParcelableArrayList("tables");

    Log.i(LOG_TAG, "Tabs Title Size: " + tabTitles.size());
    float weight = 1f;
    Log.i(LOG_TAG, "WEIGHT: " + weight);

    TableRow myRow = new TableRow(getActivity());
    myRow.setWeightSum((float) tabTitles.size());
    TableRow.LayoutParams params = new TableRow.LayoutParams(0, TableRow.LayoutParams.MATCH_PARENT, weight);
    for (String s : tabTitles) {
        Button mButton = (Button) inflater.inflate(R.layout.tab_button, null);
        View separator = inflater.inflate(R.layout.separator, null);
        mButton.setText(s);
        mButton.setLayoutParams(params);
        tabButtons.add(mButton);
        myRow.addView(mButton);
        myRow.addView(separator);
    }
    tabsLayout.addView(myRow, new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.MATCH_PARENT));
    return view;
}

Upvotes: 1

Related Questions