How to get Metrics from a ScrollView ? I need it to put my Views (Button, TextView) inside my scrollview and i want to set x and y position. Anybody have an idea ?
Thank you guys
First of all, you can't put more than one view into a scroll view.
And you shouldn't use absolute positioning by manually setting x and y.
Try to wrap your views into some layout (looks like it should be LinearLayout with vertical orientation in your case).
Check this: http://developer.android.com/guide/topics/ui/declaring-layout.html
Related
I am new to Android and currently having some issues with layout. Here is my problem:
View view = (View)findViewById(R.id.MiddleMan);
view.layout(240,358,240+view.getWidth(),358+view.getHeight());
width is 2 dp, height is match_parent. This view is inside a relative_layout, which is inside a linear_layout. This coordinates are relative to linear_layout
When I run, instead of being exactly at this coordinate, it keeps sticking to the right edge of the parent. Can anyone show me how I can deal with this problem, pls ? Appreciate a lot
I have a scrollview and need to move 100 DP up when button click event is happening.
I'm using scrollTo (0,1) but not working properly.
scrollTo(0,1) will bring you to height 1 of the ScrollView layout.
As far as I know, you cannot scrollTo using arbitrary amount of pixels in ScrollView (pls correct me if I'm wrong).
You could try having a View inside the ScrollView as a "guide" for your ScrollView where does it need to stop.
Example:
//inside your button onClick
yourScrollView.scrollTo(0, someView.getBottom());
This will make your ScrollView scrolls to the bottom of a View that you can put 100dp above.
Hope it solves your problem.
Try something like this. Hope it will work
it px = (int) (100 * Resources.getSystem().getDisplayMetrics().density)
yourScrollView.scrollTo(0, px);
When I have buttons on the xml page, moving or enlarging them is very frustrating because i can't make them go where I want. Sometimes if I want to move something to the right it'll instead move somewhere else randomly or nothing will happen. Is this because of the relativelayout? And what's the difference between relative and linear layouts?
1) RelativeLayout - Views inside a RelativeLayout are relative to the position of other children in the RelativeLayout.
2) LinearLayout - Views inside the LinearLayout are in a linear pattern either horizontal or vertical.
I personally rarely use RelativeLayout unless it is inside a LinearLayout and I need to position a single view in an odd place against the horizontal or vertical aspect of LinearLayout. RelativeLayout can be confusing because if you change the dimensions of one view in it you have to adjust the dimensions of all other views that are relative to that view.
In framelayout, if I want to put a view in the position that starts from 1/2 of the screen width. How can I do it? Thanks.
You can accomplish that in multiple ways. It only depends if you really need to stick with the FrameLayout.
Two easiest would be those:
FrameLayout - you would need to know whats the width of the layout (so if your layout is wrap_content it might get tricky). Set your view's gravity to left, and set its margin_left to half of the layout's width.
LinearLayout - either put it inside your FrameLayout (not very good practice), or (if you can) swap it with your FrameLayout. Set it's orientation to horizontal, add dummy view with width = 0dp, weight = 1. Then add your view with the same values for width and weight.
I have a GridView populated with ImageButtons at runtime. Some buttons are smaller than others and they hug the top of their row; I want them centered in their row. I think I could accomplish this with a margin (or maybe even gravity) but programmatically I don't see any way to do this.
The usual advice is to use LayoutParams, but the layout params available via GridView's LayoutParams are very limited and don't include margin or gravity.
Thanks in advance for any suggestions!
If your images are a part of a linearlayout of any other layout view, on the image, you could use something like :
<View
android:gravity="center-vertical"/>
This will handle the view to be in the center of the vertical stretch. If you use :
android:layout_gravity
then it will position it according to its parent.