Moving a view with a finger - android

I began to study Android not so long ago and have a question relating to which approach I should use to solve the simple task. Let's suppose I have a view (maybe, a button) and I want user to be able to move it across the screen with a finger. Until AbsoluteLayout was deprecated, the right approach seamed obvious. I would've just changed position of my view based on corresponding events. But what is right now?

Create a custom view of your own and add a onTouch event listener. It is very simple. Explained very well here.

If you're trying to move your views in order to navigate through your application or page through images in a gallery, Android provides a collection of widgets to do things like that. If you're trying to, say, pan across a large image, maybe this will help: Image in Canvas with touch events

Im using FrameLayout and update marginLeft and marginTop
You can add multiple children to frame layout, though this is not recommended because of multiple resolutions issues.
from the android docs:
FrameLayout is designed to block out an area on the screen to display
a single item. Generally, FrameLayout should be used to hold a single
child view, because it can be difficult to organize child views in a
way that's scalable to different screen sizes without the children
overlapping each other. You can, however, add multiple children to a
FrameLayout and control their position within the FrameLayout by
assigning gravity to each child, using the android:layout_gravity
attribute.
This "warning" is not applicable in your case because you are explicitly setting the margins according to the user touch event.

Related

Is there compulsion of using `FrameLayout` for loading fragments?

I have seen that most of the people use FrameLayout for loading Fragments.My question is Why FrameLayout ? and why not the others like LinearLayout,RelativeLayout or ConstraintLayout.Most of the answers on StackOverflow says FrameLayout is designed to block out an area on the screen to display a single item. But the others can also block the whole screen if the its height and width set to match_parent.What is the difference ?Why most of the people choose FrameLayout if others can do the same job ?
Thanks in advanceCheers
You can use anything (LinearLayout, RelativeLayout or ConstraintLayout).
FrameLayout is just the most basic ViewGroup that provides the least functionality. It is ideal to use if you only need to hold a single child - in this case, a fragment.
FrameLayout To load child one above another, like cards inside a frame, we can place one above another or anywhere inside the frame.
Designed to display a stack of child View controls. Multiple view controls can be added to this layout. This can be used to show multiple controls within the same screen space.
LinearLayout Designed to display child View controls in a single row or column. This is a very handy layout method for creating forms.
RelativeLayout Designed to display child View controls in relation to each other. For instance, you can set a control to be positioned “above” or “below” or “to the left of” or “to the right of” another control, referred to by its unique identifier. You can also align child View controls relative to the parent edges.
For more information, please check this
https://developer.android.com/guide/topics/ui/declaring-layout#CommonLayouts
I hope it's helpful to you!
I'm not so much good at programming but I can give you some reasons.
First, have a look at official docs.
In order to replace one fragment with another, the activity's layout includes an empty FrameLayout that acts as the fragment container. https://developer.android.com/training/basics/fragments/fragment-ui#AddAtRuntime
This means for switching between multiple fragments we have to use FrameLayout. Why Framelayout then? Another look at the official docs.
FrameLayout is designed to block out an area on the screen to display a single item. Generally, FrameLayout should be used to hold a single child view, because it can be difficult to organize child views in a way that's scalable to different screen sizes without the children overlapping each other. You can, however, add multiple children to a FrameLayout and control their position within the FrameLayout by assigning gravity to each child, using the android:layout_gravity attribute.
https://developer.android.com/reference/android/widget/FrameLayout.html
It can block out an area of the screen to display a single item. Other layouts could do the same thing too. So Frame?
Because FrameLayout has one characteristic that other layouts don't have.
FrameLayout can hold its child one above another, like a deck of cards. In a deck of cards, one card is placed above other.
FrameLayout does the same job. When you use FrameLayout as a fragment container it holds the child fragment one above other as your code wants. Then it shows one and left others behind it, you switch back to other fragments then it comes above and others go behind again.
That's all I know.

What is the purpose of FrameLayout and in which scenario should we use it?

What purpose does FrameLayout serve in Android? Is there any specific scenario for which it is designed for?
In my Android application, I have a scenario where I have to show two ImageViews, one over the other. This is a .png image file with a 9-patch drawable over this image.
Which ViewGroup should I use for this purpose: RelativeLayout or FrameLayout?
I also want to know the different scenarios that each of the ViewGroups should be used for.
I don't recall how i got to this question but here is my answer that could help anyone:
As you can see here
FrameLayout is designed to block out an area on the screen to display a single item. Generally, FrameLayout should be used to hold a single child view, because it can be difficult to organize child views in a way that's scalable to different screen sizes without the children overlapping each other.
In general is not useful to have 2 views one over the other because you're going to have overdraw effect (an area of the screen that is getting drawn by the gpu more than once which is really useless).
If you really have to, then you need to use the onDraw method (you can see an example here) and Canvas API (have a look here) and the clipRect method (you can see an example here)
Regarding FrameLayout or RelativeLayout, if you want to keep things simple or your activity has already enough nested layouts, better use FrameLayout. In 2017, there is the constraint layout which could be of some help as well.
Yes, you can use a FrameLayout in your scenario.

How to enable dragging multiple ImageViews?

After I have added multiple ImageViews to a LinearLayout and implementing the onTouch event, the touch is enabled only for the last added ImageView and all the previous ImageViews don'. What is the possible solution ?
I have tried the code from the android developer blog:
http://android-developers.blogspot.com/2010/06/making-sense-of-multitouch.html
Multi-touch in that example is designed for multi-touching a single View, not for touching multiple views at the same time.
This might help: Android firing onTouch event for multiple ImageViews
The solution you might be looking for, if you want to give the effect of multi-touching ImageViews, is placing another view on top of those views which allows you to capture all the touch events and pass the appropriate action to the view below it.
In my experience it has been much easier to use a SurfaceView and render the images to the view myself but this wouldn't be appropriate if there are other behaviours of View which you want to take advantage of.
Android Launcher app supports moveable views using the touch interface. The Launcher object model includes objects like DragLayer, DragSource, DragController, and DropTarget.
Using this concepts there is a nice tutorial for Moving Views In Android.
Look at that, I think its what you needed.

Which layout can provide free drag and drop insertion?

I'm using Eclipse graphical layout, and i want to Drag and Drop components to the layout and i want them to stay at the exact position that i dragged them.
Relative Layout is the closet thing but it changes the location of the components after couple insertion.
thanks.
Rami.
i want them to stay at the exact position that i dragged them.
No, you don't. Android devices come with screens in many different sized and resolutions, so fixed positioning of components is a recipe for making the application unusable to users with a different screen that you.
The point of layouts is that they can adjust to different screen sizes. Learn to use them properly.
You're looking for AbsoluteLayout, but it is deprecated.
RelativeLayout is the way to go, just pay some attention to the hints drawed on-screen when you place your elements.
You should be careful also to avoid circular references otherwise your items will be scattered randomly over the layout. (Example: Textview A on right of TextView B; TextView B on left of TextView A)
Be careful also when you change your element's id because the IDE does not update automatically all the references and thus your layout gets scattered again. When you update an ID you must update all its references in the XML file by hand.
Actually absolute layout is the answer what u want ,but that is deprecated.Relative layout does what you want.But it arrange its child based on parent position.If you drag a text view ,then other view will depend on this text view.If you have inserted 2 view,then third view will depend on the parent(2 views).But in between if u disturb any of the view ,it may affect other views.So do things systematically
In Java what we can typically use is GroupLayout. You may see what the equivalent is for android.

Is there any way to have UI elements slightly overlap (one on top of one another) without using absolute layouts?

Lets say I have a list on screen, which I always want to be usable. I also want a small image or textview to slightly overlap the listview. Is anything this possible without using absolute layout parameters?
I've never seen it in any android app or tutorial, but there are things like this in many iPhone apps. It adds a nice touch.
(Also, I don't have any code to show because I'm not sure where to start)
Relative Layouts also allow things to overlap. Views declared later in the xml will be on top. I believe that aligning view edges and use of margins should allow you to achieve this affect without great difficulty.
You could use RelativeLayout and set for example android:layout_marginTop="-50dip" android:layout_below="#id/my_list".
As well as RelativeLayouts, you can also use FrameLayouts to stack objects. Other than the z-order (last object declared = highest z-order), the child objects don't depend on the positioning of other objects in the group, so you can just set margins or gravity to position them.
So in your instance, just declare a TextView after your ListView, and position it wherever you want. It won't interfere with the ListView's positioning, and it will sit on top.

Categories

Resources