0

I have a LinearLayout oriented vertically, and I'm trying to add three sub-views to it. One view is a small, 50px high view that just draws a box in it, the second view is a horizontal list of radio buttons, and the third view is a graph.

If I add the graph last, everything fits on screen, but if I add the graph first, it pushes everything off screen and it becomes the only view on the screen. The graph is a custom control of mine that wants to be as big as it can (its onMeasure function just returns the provided width and height), which I think is where the problem lies. But I want to keep it as big as possible, and have the layout fit the views regardless of their order added.

Is this possible? If so, how can I accomplish it? Ultimately, I'm trying to put the graph on top and the other two controls below it, but if I add the graph first it consumes the whole screen.

Here's the code I'm using to create the views. This is a school assignment and I can't use XML layouts for it. I've been assigned to make a control, which I've completed. I'm just trying to make a simple activity to show the control off.

public class MainActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        LinearLayout layout = new LinearLayout(this);
        layout.setOrientation(LinearLayout.VERTICAL);

        layout.addView(new MovingBox(this));

        RadioGroup radioGroup = new RadioGroup(this);
        radioGroup.setOrientation(RadioGroup.HORIZONTAL);
        radioGroup.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));

        RadioButton button = new RadioButton(this);
        button.setText("Light");
        button.setLayoutParams(new RadioGroup.LayoutParams(RadioGroup.LayoutParams.MATCH_PARENT, RadioGroup.LayoutParams.WRAP_CONTENT, 1));
        radioGroup.addView(button);

        button = new RadioButton(this);
        button.setText("Dark");
        button.setLayoutParams(new RadioGroup.LayoutParams(RadioGroup.LayoutParams.MATCH_PARENT, RadioGroup.LayoutParams.WRAP_CONTENT, 1));
        radioGroup.addView(button);

        button = new RadioButton(this);
        button.setText("Colorful");
        button.setLayoutParams(new RadioGroup.LayoutParams(RadioGroup.LayoutParams.MATCH_PARENT, RadioGroup.LayoutParams.WRAP_CONTENT, 1));
        radioGroup.addView(button);

        layout.addView(radioGroup);

        // If I move these next to lines just below layout.setOrientation(...) it becomes the only thing visible
        TweenerControl tw = new TweenerControl(this);
        layout.addView(tw);

        setContentView(layout);
    }
}

Adding the graph last: (everything fits but I'd like the graph on top)

Adding the graph last

Adding the graph first: (only the graph is visible)

Adding the graph first

Tim
  • 35,413
  • 11
  • 95
  • 121
Cornstalks
  • 37,137
  • 18
  • 79
  • 144

1 Answers1

0

I've worked on this more, and the solution I've come up with is to not use LinearLayout and use RelativeLayout instead. RelativeLayout tries to position and size things so they properly fit, whereas LinearLayout just gets the size of things and lays them out in top to bottom order, where items higher in the LinearLayout don't care about the sizing of items lower in the LinearLayout.

Cornstalks
  • 37,137
  • 18
  • 79
  • 144