android - Horizontal and Vertical linear layout inside a scroll view


Question: 

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.




3 Answers: 

your parent ViewGroup must be one. you are using 2 times setContentView and this is your mistake. use only one and add other linearlayout as child in another one. I think it's better that you try to do in in xml and then write your code like that.

 

basically you've got the setContentView wrong...
i would strongly suggest to define the layout in xml - and then set it just once with the following:

setContentView(R.layout.my_xml_layout);

this way - you normally do have the possibility to see your layout, and it's (in my opinion) easyer to define a layout (especially if you'll decide to change this one day).

if you wan't to do it in code however, what you'll have to do is something like the following (untested - but should work)

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);
verticalLayout.addView(horizontalLayout); //add the Horizontal-View to the parent-grid

//Declaring the scroll view
ScrollView scrollView= new ScrollView(this); 
scrollView.addView(verticalLayout);
//set the scroll view
verticalLayout.addView(scrollView); //add the scrollview to the parent-grid
//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);
setContentView(verticalLayout);
}

edit: sorry - had a few errors in the first time - i edited it now, it should work like this.

 

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
}
 

More Articles


mfc - How to make a floating CMFCToolbar nonmovable

First I make my CMFCToolbar permanent withm_wndMyToolBar.SetPermament(TRUE); // it removes the CLOSE Button Now, how can I achieve that the user can not move the permanent CMFCToolbar?I have tried to subclass the CMFCToolbar, but the ON_WM_MOUSEMOVE & ON_WM_NCMOUSEMOVE are never called inside t

java - I want to change the color of Toolbar of the application on scrolling

I have a toolbar that change color on scrolling basicly color is transparent and when scrolling it change color I want to set only one color instead of transparent and let one color be shownI have tried to change color on toolbar xml and display scroling in activitythis my toolbar.xml<?xml versio

visual c++ - OnInitMenuPopup does not init correctly when called from a toolbar button that was a POPUP menu in the App main menu bar

To make a long history short, imagine my main menu is a CMFCMenuBar which menu is defined by:IDR_MAINFRAME MENUBEGIN POPUP "&File" BEGIN MENUITEM "New", ID_FILE_NEW MENUITEM "Open", ID_FILE_OPEN MENUITEM "Save", ID_FILE_SAVE


ios - How to create toolbar in separate xib file and separate class and use it in multiple view controllers?

How to create a toolbar in a separated xib file and separate class and use it in multiple view controllers?

android - Single Activity and Multiple Fragment with different toolbar layouts

I have an app with Single Activity multiple Fragments architecture in my app. The problem is, there is one listing fragment with normal Toolbar, from here user can click and go to Details screen. Now in Details screen, I want the Activity to become Fullscreen with CoordinatorLayout and Collapsing To

WPF Frame Source Refreshing the Loaded Page

I've come across a strange scenario where a frame refuses to refresh its content.I can kinda understand what's happening but the solution is not coming to me.I have a page that has a frame (Frame1) and several buttons. When I click on a button a page is loaded into the frame. This works perfectly in


Android: pin TabLayout to top of Scrollview

I was looking at the twitter app on my phone.You can see that when a user scrolls up, the tabLayout actually just pins itself onto the bottom of the toolbar nicely and does not move at all.I thought maybe they did it by just putting all of the top part of the app (the profile picture, the profile wa

material design - android lollipop toolbar: how to hide/show the toolbar while scrolling?

I'm using the new toolbar widget introduced in the appcompat / support-v7. I would like to hide/show the toolbar depending on if the user is scrolling up/down the page, just like in the new Google's playstore app or NewsStand app. Is there something built into the toolbar widget for this or should I

wpf - Canvas control is not available in Windows form application?

WPF Canvas control is not available in Windows form application. Am using Visual Studio 2010. Framework 4.0. Is there way to add these controls to VS2010 toolbox?

android - How to get a progress bar under the ActionBar in Material Design?

The Material Design specifications for Progress and activity specifically show in several areas a horizontal progress indicator of only a few pixels (dips) thick below whatever it is we are calling the ActionBar these days (probably Toolbar).However, I don't see any way to actually implement that po