Checking child is going out of bound in LinearLayout - android

I am dynamically adding children in LinearLayout horizontally. I am adding child on button click. But after adding some children, say 4, others children go out of screen as these are being added horizontally. How can I check that child is going beyond screen in LinearLayout, and I need to create new layout? Or what can I do with the LinearLayout so that when it goes out of screen, it wraps itself?
Any help? Thanks in advance.

Just put your LinearLayout inside HorizontalScrollView, it will do all the necessary work for you:
<?xml version="1.0" encoding="utf-8"?>
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</LinearLayout>
</HorizontalScrollView>
EDIT if you want your views to be wrapped inside the parent view, please take a look here Line-breaking widget layout for Android

Related

Scrollview multiple LinearLayouts

So I have multiple linearlayouts (4) and I would like to have them all in a scrollview. These layouts are used for input fields that are separated by column/category Names, Ages, Occupation, and Country of Residence.
Each one of these has their own linearlayout. Is there a way to make them all scrollable simultaneously (since scrollview only supports 1 child)?
Just have one LinearLayout that contains all other LInearLayouts
From Docs
... meaning you should place one child in it containing the entire contents to scroll; this child may itself be a layout manager with a complex hierarchy of objects
This means that it can have only one direct child (the main LinearLayout) but that one child can have its own children.
ScrollView can have only one direct child
so use one Layout that will Contain all your Layouts
Like
<ScrollView .....>
<LinearLayout .....>
.............Your All other Layouts .................
</LinearLayout>
</ScrollView>
You have to take your time and do it one at a time. As Tarsem said, you can put one layout inside this scrollview
<ScrollView .....>
<LinearLayout .....>
//Inside this layout, you can another scroll view.
<ScrollView .....>
<LinearLayout .....>
.............All your other Layouts .................
//continue adding them until you add your last scroll view. if that
is the desire layout.
</LinearLayout>
</ScrollView>
</LinearLayout>
</ScrollView>
<ScrollView
<LinearLayout // Your Main Layout
<LinearLayout // Child Layout 1st
</LinearLayout>
<LinearLayout // Child Layout 2nd
</LinearLayout>
<LinearLayout // Child Layout 3rd
</LinearLayout>
</LinearLayout>
</ScrollView>
Try to use about format.
Hope it helps.
Regards

switching between layouts using setvisibility on android not working

I'm using a screen that I want to switch between layouts on the click of a button. I want both layouts to occupy the full height of the screen, when the button on the first layout is pressed, i want this layout to disappear and the other layout to take it's place, and then same on the next layout.
<LinearLayout
android:id="#+id/layoutFirst"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
..... views
</LinearLayout>
<LinearLayout
android:id="#+id/layoutSecond"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
..... views
</LinearLayout>
On each layout I have a button. I want this button to switch so the other layout occupies the full screen, but nothing happens. The code in the on-click event for the first screen is the following
LinearLayout layoutFirst = (LinearLayout) this.findViewById(R.id.layoutFirst);
layoutFirst.setVisibility(View.GONE);
LinearLayout layoutSecond = (LinearLayout) this.findViewById(R.id.layoutSecond);
layoutSecond.setVisibility(View.VISIBLE);
Just in case the problem was to do with the fill_parent, I also tried this with the height being set to wrap_content. Initially I can see both layouts on the screen, when I press the button, the layout still does nothing.
Can anyone tell me if I am doing something wrong or if there's a way to solve this issue. Any help would be greatly appreciated it
Put the two LinearLayouts inside a FrameLayout:
<FrameLayout
android:id="#+id/framelayout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="#+id/layoutFirst"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:visibility="gone">
</LinearLayout>
<LinearLayout
android:id="#+id/layoutSecond"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
</LinearLayout>
</FrameLayout>
Give the LinearLayouts different ids!
Is your parent layout a "RelativeLayout" ? => not necessary here.
Try also the solution proposed in this quite similar thread how to hide linearlayout from java code?
Update:
/*global or add final keyword if not global in order to use inside listener */
LinearLayout layoutFirst;
LinearLayout layoutSecond;
/*end global*/
/*in onCreate(Bundle) method*/
layoutFirst = (LinearLayout) this.findViewById(R.id.layoutFirst);
layoutSecond = (LinearLayout) this.findViewById(R.id.layoutSecond);
/*and then in your listener, alternatively*/
layoutFirst.setVisibility(View.GONE);
layoutSecond.setVisibility(View.VISIBLE);

Adjust gridview between two views within a layout in android

In my application, I have a layout which includes a LinearLayout--> Top, GridView--> Middle and a set of views which should be aligned to the bottom.
1. Layout when gridview is not having much content
2. Layout when grid view contents are increased
3. Layout as how it should be shown when Gridview content is very large
Presently my gridview is expanding when the contents in it is increasing that the lower level contents are not visible in the screen.
I don't want to assign specific height to the grid view as it is looking weird in screens with different dimensions.
Is there any way that the gridview expands only upto the bottom views? My parent layout is LinearLayout. I have tried out with relative layout, but it is not working.
I too faced the same problem and found the solution by modifying the respective xmls as below:
Use RelativeLayout as Parent Layout.
Place the Header Content in RelativeLayout
Use ScrollView for the GridView which changes dynamically.
Place the Footer Layout after ScrollView
as shown below:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="#+id/txtTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<ScrollView
android:id="#+id/scro1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#id/txtTextView" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<GridView
android:id="#+id/scro1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
</ScrollView>
</RelativeLayout>
android:layout_below attribute in ScrollView makes the difference.

2 layouts nested in a scroll view(one of them a table layout) - Android?

I am wondering is it possible to have 2 nested layouts in android? I have table layout in a scroll view. I want to add another layout type(not sure which one yet) but everyone I try crashes.
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
</TableLayout>
<TableLayout
android:id="#+id/tblLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
.... rows here ....
</TableLayout>
</ScrollView>
this will crash on the SetContentView(Resource.Layout.MyLayout);
"This is the next statement to execute when this thread returns from
the current function."
If I just have one layout in the ScrollView it will load.
Scrollview documentation is sometimes interesting:
http://developer.android.com/reference/android/widget/ScrollView.html
Layout container for a view hierarchy that can be scrolled by the
user, allowing it to be larger than the physical display. A ScrollView
is a FrameLayout, meaning you should place one child in it containing
the entire contents to scroll; this child may itself be a layout
manager with a complex hierarchy of objects. A child that is often
used is a LinearLayout in a vertical orientation, presenting a
vertical array of top-level items that the user can scroll through.
As they suggest, you should put a LinearLayout that include both of your TableLayout
Try surrounding the two tables with a Layout like this;
<ScrollView>
<RelativeLayout>
<TableLayout>
</TableLayout>
<TableLayout>
</TableLayout>
<RelativeLayout>
</ScrollView>

Using a nested LinearLayout

SOLVED: The layout_height parameter was set to Match_parent in the buttonbar definition. Changed to wrap_content.
I'm currently working on a new App which has a series of buttons at the top of the main screen. the "buttonBar" XML defines a linearLayout and is later nested within another linearLayout.
The buttons appear fine and work however if I then put a text view beneath the include statement the text does not appear. I think that it is actually appearing behind the buttons. I assumed that because it was within a parent linearLayout that it would appear after the included (nested) nested layout.
please could someone explain why this is not occurring and point me in the right direction to solve it.
much appreciated,
M
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<include layout="#layout/buttonheader"/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:id="#+id/textView1"
android:textColor="#ffffff">
</TextView>
</LinearLayout>
Set height and width of the included layout buttonheader
SO that you can see this included layout in your layout

Categories

Resources