I use the following user interface:
A parent relative layout parentLayout with the dimensions 800x600 (width x height)
A second relative layout childLayout, which is a child of the parent layout.
It has the dimensions 800x1000, i.e. it is larger than the parent layout.
parentLayout.addView(childLayout);
My goal: Scrolling childLayout by using childLayout.scrollTo(x,y).
When I use childLayout.scrollTo(x,y), Android scrolls childLayout but doesn't refresh (redraw) it. The effect is, that childLayout is cut to the same dimensions as parentLayout.
Unfortunately, the following solutions don't solve the problem:
childLayout.scrollTo(x,y);
childLayout.invalidate();
childLayout.requestLayout();
Any ideas how to solve this problem?
You can use parentLayout as ScrollView instead of RelativeLayout and keep childLayout as RelativeLayout.
Related
I'm subclassing HorizontalScrollView (which is a FrameLayout) and adding a RelativeLayout to it programmatically.
The FrameLayout correctly fills the parent view, but RelativeLayout inside doesn't show up.
MainActivity::OnCreate()
setContentView(R.layout.activity_main);
CustomHorizontalScrollView custom = new CustomHorizontalScrollView(this);
ViewGroup contentView = (ViewGroup) findViewById(R.id.content);
contentView.addView(custom,new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 180));
CustomHorizontalScrollView::Constructor()
this.setBackgroundColor(Color.MAGENTA);
relativeLayout = new RelativeLayout(context);
relativeLayout.setBackgroundColor(Color.BLACK);
this.addView(relativeLayout, new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));
Result:
The RelativeLayout should be black and should fill parent but is not.
The weird thing is that if I specify width and height in pixels instead using MATCH_PARENT, the RelativeLayout appears.
this.addView(relativeLayout, new FrameLayout.LayoutParams(90, 90));
Does that mean that I can't use MATCH_PARENT when programmatically adding a RelativeLayout to a FrameLayout? Or am I missing something? Or maybe HorizontalScrollView only supports having a child with fixed width and height?
I do not find strict info in Android SDK API Reference, but actually the HorizontalScrollView is a "Layout container for a view hierarchy that can be scrolled by the user, allowing it to be larger than the physical display."
So, why do you need a HorizontalScrollView if its unique child must have the same width?
Likely, you can try the following:
this.addView(relativeLayout, new FrameLayout.LayoutParams(90, FrameLayout.LayoutParams.MATCH_PARENT));
... and the RelativeLayout will appear.
But maybe the following makes more sense:
this.addView(relativeLayout, new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.MATCH_PARENT));
Then the RelativeLayout will be large enough to contain its children and you'll can scroll it using the HorizontalScrollView.
The HorizontalScrollView has the property fillViewport that you can set to true if you want
that the HorizontalScrollView "stretch its content to fill the viewport": I think it can be useful only when the content is less large than the HorizontalScrollView, but (I repeat), if the content is ALWAYS less large than the HorizontalScrollView, then the HorizontalScrollView is useless.
Hi have main Linear Layout inside that I have these view with same tree ScrollView,LinearLayout and then Relative Layout ,I am trying to set Relative Layout Margin that is most child View but its Margin not doing anything,I have all of these view with are with height and width as Fill_Parent .Anyone can guide how I can solve this issue thing ?
I have a ScrollView that contains a LinearLayout.
In code I add items to the LinearLayout and it nicely expands.
When I remove, in code, all the items from the LinearLayout, the LinearLayout stays the same size, it's empty though.
How do I get the ScrollView to reclaim the height that the LinearLayout used when it had items and shrink back the LinearLayout to use the least height as it is now empty?
I have tried (in C#):
linearLayout.RemoveAllViewsInLayout();
linearLayout.PostInvalidate();
scrollView.PostInvalidate();
Java or C# answers are fine.
I only have Java experience with Android, so I would try using:
linearLayout.requestLayout();
This should cause another layout pass, and assuming your LinearLayout is set to WRAP_CONTENT it will measure all the children and size itself to fit the smallest space that the children occupy (or minHeight if you have that set and it is larger).
This trick seems to work. The result is that the linearlayout ids resized and the scrollview as well:
linearLayout.RemoveAllViewsInLayout();
linearLayout.Visibility = ViewStates.Gone;
scrollView.SmoothScrollTo(0, 0);
linearLayout.Visibility = ViewStates.Visible;
There must be a better way, but this works.
My android page has 10 EditText and 10 TextView. but there is no space in my screen in the Graphical Layout. i just added 5 only. im using Scroll layout. how to add additional 5 items in the screen without reducing the items height. Is there any coding here.?
<ScrollView>
<LinearLayout>
<TextView/>..
....
..
</LinearLayout>
</ScrollView>
You can add multiple items inside a LinearLayout. Since ScrollView is scrollable it won't affect the dimensions of the Views inside. You can add as many views as you need without worrying about screen size or View size..
You should add a LinearLayout as the only child inside your ScrollView. Then get a reference to that LinearLayout :
mLayout=(LinearLayout)findViewById(R.id.myLayout);
and then add Views dynamically from code using :
EditText et=new EditText(...);
//....
mLayout.addView(et);
A ScrollView can have only one direct child, so you need to put all the other Views in a Layout, such as LinearLayout and put that layout in ScrollView
Make your ScrollView's height as match_parent and the inner LinearLayout's height as wrap_content. The LinearLayout will stretch according to the number of children inside it and if the height exceeds the height of the ScrollView, the overflow can be seen by scrolling. If the ScrollView and inner Layout both have same height or if ScrollView have larger height than inner Layout, the scrolling won't happen for obvious reason.
I'm trying to have a ScrollView centered top to bottom within its parent, but I'd also like the ScrollView to be no taller than the height of it's child. The child's height is variable. If there is room, the ScrollView should stretch to be exactly the height of it's child. Any suggestion on how to set up this layout?
Put the ScrollView inside a android:layout_height="match_parent" RelativeLayout.
set the ScrollView to android:layout_centerVertical="true".
Set the ScrollView's height to android:layout_height="wrap_content".
This should get you a centered scrollview that is up to but no taller than the relativelayout that contains it.
Out of curiosity, why do you not want the scrollview set to fill_parent/fillViewport? I would suggest doing that, and simply centering the content of the scrollview instead.