Search code examples
androidgraphandroid-linearlayout

Building LinearLayout programmatically


I'm trying to get my layout to look like this:

enter image description here

My class containing this code extends Linelayout itself and adds the different elements with addView

    setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

        LinearLayout line = new LinearLayout(context);
    line.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
    viewVerLabels = new VerLabelsView(context);

            // add y axis linearlayout & graphview
    line.addView(viewVerLabels);
    line.addView(new GraphViewContentView(context), new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, 1));
    line.setOrientation(LinearLayout.HORIZONTAL);

            // add to main linearlayout
    addView(line);

    LinearLayout line2 = new LinearLayout(context);
    line2.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
    viewHorLabels = new HorLabelsView(context);
    setOrientation(LinearLayout.VERTICAL);
            // add x axis labels
    line2.addView(viewHorLabels);
            // add to main linearlayout
    addView(line2);

My XML file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/graph2"
    android:layout_width="fill_parent"
    android:layout_height="250dp"
    android:orientation="vertical" />

With this code I get the Y axis labels and the graph to appear, but the X axis labels are missing

I can't seem to get what I want, I've tried several combinations, what am I doing wrong


Solution

  • Here's my code that allowed me to get round the problem

            graphViewStyle = new GraphViewStyle();
            paint = new Paint();
            graphSeries = new ArrayList<GraphViewSeries>();
    
            setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
            setOrientation(LinearLayout.VERTICAL);
    
            LinearLayout line = new LinearLayout(context);
            line.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
            line.setOrientation(LinearLayout.HORIZONTAL);
    
            LinearLayout line2 = new LinearLayout(context);
            line2.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
            line2.setOrientation(LinearLayout.HORIZONTAL);
    
            txt = new TextView(mContext);
            addView(txt);
    
            GraphViewContentView graphContentView = new GraphViewContentView(context);
            graphContentView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, 260, 1));
            line.addView(graphContentView);
            viewVerLabels = new VerLabelsView(context);
            viewVerLabels.setLayoutParams(new LayoutParams(30, 260));
            viewHorLabels = new HorLabelsView(context);
            viewHorLabels.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, 15));
    
            line.addView(viewVerLabels);
    
            addView(line);
    
            line2.addView(viewHorLabels);
            TextView v = new TextView(context);
            v.setLayoutParams(new LayoutParams(30, 15));
            line2.addView(v);
    
            addView(line2);