Search code examples
androidscrollviewandroid-linearlayout

Horizontal and Vertical linear layout inside a scroll view


I am trying to get a combination of Views established. They must constantly have a Button and Edittext box at the top horizontally next to each other and below that a vertical list of Textviews. The vertical list should be enclosed in a ScrollView to allow the user to scroll down through the TextViews (The Button and EditText at the top should still be visible while this is happening).

protected void initLayout() {
    // Declaring the vertical layout
    verticalLayout=new LinearLayout(this);
    verticalLayout.setOrientation(LinearLayout.VERTICAL);
            //Declaring the horizontal layout
    horizontalLayout=new LinearLayout(this);
    horizontalLayout.setOrientation(LinearLayout.HORIZONTAL);
            //set the main view as horizontal at the top
    setContentView(horizontalLayout);
            //Declaring the scroll view
    ScrollView scrollView= new ScrollView(this); 
    scrollView.addView(verticalLayout);
            //set the scroll view
    setContentView(scrollView);
    //declare and add button to horizontal view
    theButton= new Button(this);
    theButton.setText("Add Joke");
    horizontalLayout.addView(theButton);
    //declare and add edittext to horizontal view
    theEditText= new EditText(this);
    theEditText.setLayoutParams(new LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT));
    horizontalLayout.addView(theEditText);
}

I believe I might be going wrong with the setContentView but amn't completely sure.


Solution

  • I found a solution to the problem. I had to encapsulate both the horizontal layout and vertical layout inside of another linear layout and set that as the root view.

    protected void initLayout() {
        // Declaring the vertical layout
        verticalLayout=new LinearLayout(this);
        verticalLayout.setOrientation(LinearLayout.VERTICAL);
    
        //Declaring the horizontal layout
        horizontalLayout=new LinearLayout(this);
        horizontalLayout.setOrientation(LinearLayout.HORIZONTAL);
    
        //***SOLUTION*** Create a view to group the other views in
        groupLayout=new LinearLayout(this);
        groupLayout.setOrientation(LinearLayout.VERTICAL);//vertical as the horizontal view is on top of the vertical view
    
        //Declaring the scroll view
        ScrollView scrollView= new ScrollView(this); 
        scrollView.addView(verticalLayout);//the vertical layout is the only view that should be scrollable and therefore added to the scrollview
    
        //declare and add button to horizontal view
        theButton= new Button(this);
        theButton.setText("Add Joke");
        horizontalLayout.addView(theButton);
        //declare and add edittext to horizontal view
        theEditText= new EditText(this);
        theEditText.setLayoutParams(new LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT));
        horizontalLayout.addView(theEditText);
    
        //***SOLUTION*** attach the views to the group view
        groupLayout.addView(horizontalLayout); //the layout displayed at the top of the group layout
        groupLayout.addView(scrollView); //the layout below the top (scrollview already contains the vertical view)
    
        setContentView(groupLayout);//assign the group layout to the content
    }