I have an activity where users can drag and drop around some buttons.
All my draggable views are inside a RelativeLayout and to drag them around I change their top and left margins.
Everything works perfect, except that when I drag the views close to the right and bottom edge of the screen, they shrink instead of being drawn outside the screen.
Is there any way for having the same behaviour on the all edges of the screen (currently only works fine on the top and left edge).
I guess I can have a left and top negative margin. But when I have left margin > screen width or top margin > screen height the images shrink.
To implement custom positioning of views inside a ViewGroup the best way is to implement your custom ViewGroup overriding onLayout().
It is not that difficult and I explained how to do it here. (the question is newer and has more visibility)
Edit:
You might also want to look into the Drag and Drop Api here. Thanks to Zainodis for the comment.
Related
I am trying to develop a zoomable scrollview with seeing both the scrollbars visible. But when implementing I face the following issues.
To enable both Orientation, use scrollview and horizontal scrollview. In this case only the vertical scrollbar is visible in the viewport and the horizontal scrollbar is available at the bottom of the content. We need to scroll to the end to see it. But I could see apps like Gmail, chrome in Android where both the scrollbars are visible in the viewport. How is this possible?
When applying scale to the contents, the contents are scaled and clipped inside the scrollview. It requires content resizing. When resizing, the pivot value of the contents are changed and pinch to zoom is not applied at the required touch pointers. But many android apps do this flawlessly.
Can anyone suggest or provide me a clue to achieve the above two?
Solution with Xamarin Android will be better. However, any ideas are welcome.
Item 1 and 2 are difficult to answer without seeing your code.
But
I've implemented a zoomable and scrollable Table with Fixed headers (see https://github.com/Zardozz/FixedHeaderTableLayout might give you some hints on how I overcame similar problems)
For scrollbars I extended a FrameLayout for mine, this is what implements the scrollbars as the top level parent ViewGroup, thus the scrollbars are always on the bottom and right of the outside edge and stay on the screen.
This was done by setting setWillNotDraw(false); in the ViewGroup and by implementing computeHorizontalScrollRange and computeHorizontalScrollOffset methods (as well as the vertical ones)
For Zooming I measure the children ViewGroups (The sub table parts) by overriding their onMeasure to int measureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
Therefore the children ViewGroups are sized to be as big as they want to be and not clipped to the parents size, thus when you scale the view it has something that is normally offscreen to draw when a scale factor is applied.
Update:
Forgot to say that I only scale the child views by overriding drawChild method in the parent.
I am trying to get a bar at the bottom of the screen where ads can be placed and have found no way of doing so. What I am trying to do is pretty much this (below)
You can see that there is a gap for adverts to fit in at the bottom, meaning that the view of the game doesn't actually take up the entire screen.
How can I achieve this? I am looking for maybe a way to scale the view to change its height but I don't know how
You put both in a relative layout. Make the bar aligned to the parent bottom, then make the game view align to the top of the bar.
I am using DrawerLayout and I have reduced the dragging area horizontally via reflection as someone suggested. It's a hack, I know, but it works.
Is there a way to reduce the dragging area vertically ? Say only top half of or bottom half of the screen allowed to drag ?
Is it possible to move a button or any other element in relative layout to the absolute edge of the sreen? If I drag a certain element to the edge of the layout, eclipse will automatically place it a few dp away from the edge. It'll basically just create a small space between a button and the end of the creen. Is it possible to bypass that?
I know buttons aren't meant to be that close to the edge of the screen anyway, but I need it for a certain thing.
The normal button has a little padding by default, because the buttons backgroundimage is a 9-patch image with padding left, right and on bottom. That's why you see the space between button and screen.
To solve this you can use a custom button. For example change the backgroundimage of button like this android:background="#drawable/image".
I have a RelativeLayout where I add and position Views (Buttons, TextViews) by changing their top and left margins. I can position them half outside the layout on the top and left side, but when I try to move them half outside the layout on the bottom or right side, they change shape to stay inside the layout.
How can I make the views fall outside the screen instead of resize them self?
Giving them a width does not solve it.
You can't do it using RelativeLayout, which always need to fit the screen size.
You can use ScrollView wich extends a FrameLayout and can be larger then the screen.
Regards.