How to move scroll view up 100 pixels when button is clicked - android

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);

Related

Move button back to origin after dragging/moving it

I have an ImageButton that I need aligned to the bottom of the screen, and when I click and drag it, it moves with my finger by a certain distance, beyond which it will not move further from the origin but continue following the direction my finger is from the origin.
When I let go, the button has to slide back to the origin.
What I have managed to do so far is to have the button follow my finger via this answer: Moving buttons via Touch
However, how should I get the origin of the button at initialization for it to bounce back after I release?
Originally I implemented the alignment of the button by having a android:layout_gravity="center|bottom" to place it at the bottom, but it somehow messes with the position of the button relative to my finger (It follows my finger movements but it's off center).
Hence, I used mButton.setY() and mButton.setX() to place it at the bottom of the screen, but it seemed hackish to use the screen dimensions to place it. Can't think of a better way.
You can set the width and height of your layout in XML using
layout_width and layout_height.
Then in your MainActivity, instantiate the layout (I will use LinearLayout as an example) and get its width and height.
LinearLayout l = (LinearLayout)findviewbyid(R.id.l1);
int height = l.getHeight();
int width = l.getWidth();
Now, whenever you release the button, you can set your its position in the following way:
mButton.setY(height - 50)
This will position the ImageButton 50 pixels above the bottom of your layout. You can also set the x position of the button as you want in the same way. Also, you should probably store height - 50 as a global variable (origin) that you can call from anywhere in your code.

How to set Weight to make bottom view occupy remaining space Android

I want to model my layout screen in the following 2 parts.
Basic Information related Linear layout with expandable view initially 20 % of screen height.
Bottom List view occupy Remaining Space.
But When the top view is expanded then i want to make it height upto 50 % and the bottom listview should start then after.
I have attached image for reference.
I have tried setting the layout_weight of top and bottom layout. But not able to find out exact solution. May be i am missing some points.
Let me know if you need more information from my end.
Thanks in Advance.
regards,
Rakesh
Try to remove the current weight from the top view and in the listview set android:layout_weight="1" and android:layout_height="0dp". This makes the ListView to take whatever height left in the screen.

getMetrics() from a Scrollview?

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

Multiple imageview on touch action listener

I have 4 views in the same activity. I want to move each Imageview on touch in whole screen, but it allow me to move in its width height.
Each image view's layout height and width is wrap_content.
It works fine with one image view with fill_parent.
Its explained in this example.

Get Center point in my app android

In my app Horizontalscrollview is present. It consist of LinearLayout in it, to that layout i have added number of buttons to it.ViewFlipper is also present in it. As i flip the layout i want to move the horizontalscrollview the respective button should get to center position.
My layout(s) and number of button(s) are same. for 1st layout 1st button should be at center location?
thnx for any help....
Hmm, okay, just to point out, when you get to the first or last position (possibly first + 1, last - 1 as well, depending on the button size), you won't be able to have it in the center, as you can't overscroll with just a HorizontalScrollView. With that in mind, for your general case (you can handle the edge cases however you like -- I'd suggest just leaving it scrolled as far as you can, and giving the selected button some sort of highlight) you should be able to do something like this:
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int screenWidth = metrics.widthPixels;
//get an instance of your LinearLayout, HSV, and ViewFlipper
LinearLayout ll = (LinearLayout)findViewById(R.id.my_linear_layout);
ViewFlipper vf = (ViewFlipper)findViewById(R.id.my_view_flipper);
HorizontalScrollView hsv = (HorizontalScrollView)findViewById(R.id.my_hsv);
//as you've said, there should always be an equal number of Buttons as
//Views in the ViewFlipper. This code assumes that is always true.
View v = ll.getChildAt(vf.getDisplayedChild());
//assuming smoothScrollTo scrolls to the left side of the screen,
//which I think it does, we want to scroll to the Button's center
//point, minus (shifted to the right) by half the screen width
int scrollTo = v.getLeft() + (v.getWidth() - screenWidth) / 2);
//handle overflow for the edge cases
if (scrollTo < 0) scrollTo = 0;
else if (scrollTo > hsv.getWidth()) scrollTo = hsv.getWidth();
hsv.smoothScrollTo(scrollTo);
Untested, and I'm prone to small syntax errors, but the general idea may help. :)
Get the size of Display, and divide by 2, then subtract your button width divided by 2 ?
But it really sounds like you should have the buttons in its own view, then merge the views on top of each other.
<merge>
<HorizontalScrollView .../>
<Button ... />
</merge>

Categories

Resources