Horizontal and Vertical linear layout inside a scroll view - android

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.

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
}

Related

Creating a layout Multiple times in Android

How to create a whole layout (Relative/Linear) multiple times in Android? I want the same layout to be created multiple times inside a horizontal scroll view.
You can use RecyclerView for Horizontal scrolling-
or-
Take horizontal scrollview reference in java code by findViewById.
Create one other xml for view which you want to display multiple
time.
inflate that view by getlayoutinflator. Create a loop in
view.
create a linearlayout at runtime and add those view to it by add
view
and add linearlayout to horizontal scroll view. by addview()
take a idea and modify the below code
scrollview = findViewByID(scrollview);
LinearLayout ll = new LinearLayout(this);
for(your loop){
View v= getLayoutInflator().inflate(R.layout.xml);
ll.addView(v);
}
scrollview.addView(ll);
Either you need to add inflated child views to the root view like below
RelativeLayout rootView = (RelativeLayout)findViewById(R.id.rootView);
View child = getLayoutInflater().inflate(R.layout.child, null);
rootView.addView(child);
OR you can define and include that layout multiple times inside other.
Check this link http://developer.android.com/training/improving-layouts/reusing-layouts.html
Include your reusable layout like this
<include
android:layout_width="fill_parent"
android:layout_height="wrap_content"
layout="#layout/reusabelLayout" />

How to merge the three views into a blank view and display?

I want to set a ScrollView for some views. I need to merge some view into one view and add to ScrollView.
The three views are listView, custorm view and Gallery.
Who knows how to merge the three views into a blank view and display?
In XML you can structure your app like the following snippet:
<ScrollView>
<LinearLayout>
<CustomView/>
<ListView/>
<Gallery/>
</LinearLayout>
</ScrollView>
Alternatively in code, you can do it like so:
ScrollView SV = (ScrollView) findViewById(R.id.my_scrollview);
LinearLayout LL = (LinearLayout) findViewById(R.id.my_linear_layout);
LL.addView(my_customView)
LL.addView(my_istView)
LL.addView(my_gallery)
SV.addView(LL);
hope that gives you some pointers in how to achieve what you'd like to
You can create those objects and assign them inflated elements. I mean, if the listView is in listView.xml you can inflate it with getLayoutInflater().inflate(R.layout.listView,null) and put in a ListView object. Then you can add it to a ViewGroup.
Inflate the ScrollView, you can create programmatically too. ScrollView scroll=new ScrollView(this). Don't forget adding LayoutParams to the view if you do it programmatically.
ViewGroup scrollView = (ViewGroup) getLayoutInflater().inflate(R.layout.scrollView, null);
Then you inflate or create the other views the same way. Or you can get it from an activity with findViewById(R.id.idOfView1).
Finally just add the views you've created, inflated... with scrollView.addView(View child).
You need a ViewGroup to add views as childs.

Programmatically Position items in activity. NO XML

I have an Activity that has all the display elements added dynamically. Theres no xml for the acvtivity at all.
The Activity consists of the following controls:
RelativeLayout (Layout object that all the child views sit in)
TextView (Title for the page, sits at top of the RelativeLayout)
ScrollView (Scrollable area that holds all the data controls)
LinearLayout (Layout object to hold the activity buttons)
I want to know how it is possible to define that the ScrollView sits below the Title TextView and above the LinearLayout button holder where the LinearLayout is set to the Activity Page bottom
I have tried using RelativeLayout.LayoutParams to set up rules but cannot seem to understand the way to do it. Any help or links to tutorials would be apreciated
I have included my Code to see if someone can help
// declare the items for display
RelativeLayout baseLayout = new RelativeLayout(this);
// add the customer name and number field.
// NOTE: We will always require this widget regardless of dialog design fields
tvCustomerNameNumber = new TextView(this);
tvCustomerNameNumber.setTextSize(20);
tvCustomerNameNumber.setText("Customer Name & Number");
// build up the linear layout of controls
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
// Scroll view.
// NOTE: We will always need this widget to enable us to scroll the page
// if too many items are added for the display size
ScrollView sv = new ScrollView(this);
sv.addView(ll);
// buttons
LinearLayout buttons = new LinearLayout(this);
buttons.setOrientation(LinearLayout.HORIZONTAL);
// button edit
Button edit = new Button(this);
edit.setId(EDIT_BUTTON_ID);
// button save
Button save = new Button(this);
save.setId(SAVE_BUTTON_ID);
// button cancel
Button cancel = new Button(this);
cancel.setId(CANCEL_BUTTON_ID);
// add each button to the button layout
buttons.addView(edit);
buttons.addView(save);
buttons.addView(cancel);
// Scroll view Layout parameters
RelativeLayout.LayoutParams scrollParams = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
scrollParams.addRule(RelativeLayout.BELOW, tvCustomerNameNumber.getId());
scrollParams.addRule(RelativeLayout.ABOVE, buttons.getId());
// buttons Layout parameters
RelativeLayout.LayoutParams buttonParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
buttonParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
buttonParams.addRule(RelativeLayout.BELOW, sv.getId());
// add the customer name number field to the base layout
baseLayout.addView(tvCustomerNameNumber);
// add the scroll view to the base layout
baseLayout.addView(sv); //, scrollParams);
// add the buttons to the base layout
baseLayout.addView(buttons, buttonParams);
// set the content view
this.setContentView(baseLayout);
Deva has already answered your question, but it sounds to me like you could define an xml layout as you have described above, inflate it and populate it dynamically programmatically... perhaps the layout would initially contain an empty LinearLayout, and/or no text set for the TextView, maybe even set everything to android:visibility="gone" and show it when you have added/updated all your views?
See the links below which might help you achieve this :
Programmatically adding items to a relative layout
How to programmatically add multiple LinearLayouts into one view and then add to ViewFlipper?

Can I add views to a scrollview without inflating/re-inflating layouts?

I would like to take an existing ScrollView with a view in it, and add more views, dynamically (at runtime) to the ScrollView container.
Is it possible to add these views without having to create a new layout and inflate it? If so, what's the general process for adding these views dynamically?
For the sake of this question, assume the views are TextView...
Thanks!
A ScrollView can only have one child, so it doesn't make sense to add more children to it directly. Lets say your ScrollView has a LinearLayout inside of it, then you can add more views to the LinearLayout:
LinearLayout layout = findViewById(R.id.my_linear_layout);
TextView textView = new TextView(this);
layout.addView(textView);

Android: Add view onclick after the view itself

I have a LinearLayout View with a OnClickhandler and I want to add a View after the LinearLayout programatically when the OnClick event is fired.
public void onClick(View view) {
LayoutInflater li = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout info = (LinearLayout) li.inflate(R.layout.infolayer, null);
// view.addViewAfter(info)
}
info is the View i want to add. view is the View on which the click goes and after which I want to add info.
How can I do that?
IF u want add a view after the current linear layout then first get the id of the parent layout in which the linear layout is .
for example let u have the the Linear Layout with id "ll" in relative layout(having id parentlayout) and on a button click u want add the text view under the liner layout
public void onClick(View view) {
RelativeLayout rl=new RelativeLayout(this);
TextView tv=new TextView(this)
//set param value ur requirement
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.BELOW,R.id.ll);
}
Assuming you have a known amount of linearlayouts could you just place them inside the XML resource and mark them as 'GONE'. When the event occurs make them visible. When they are marked as gone they shouldnt be taking any screen space up.
You can insert through coding and you don't need to inflate. You can create a view of any type calling the constructor and passing the context. A reference to the context can be stored on the view as a field, when the view is being constructed. This way you can always create your view on the fly.
To add the view to the LinearLayout, you just need to call addView. And afterwards, if you would want to remove it, just call removeView.
But the onClick event is inside the LinearLayout object? This might be a problem because the views inside the the LinearLayout might consume the event before it reaches your method. See this post to learn about that.

Categories

Resources