Android LinearLayout Visibility issues with Animation - android

I am trying to swap two LinearLayouts by setting their visibility properties to "VISIBLE" and "GONE" respectively. I am also using an animation while the layouts are being swapped. The animation completes successfully and I see the correct layout. However, the previous Layout which has its visibility property set to "GONE" still receives clicks even though it's not visible. At the same time, the layout which is "VISIBLE" receives clicks only when clicked in area where the "GONE" layout isn't clickable. I am also calling the requestFocus method on the "VISIBLE" layout. But it doesn't help.
Moreover, if I skip the animation part and just set the visibility properties, everything works correctly.
What am I missing here?

If you are animating widgets, you need to make changes to the layout to have the results "stick". Just using the fillAfter stuff will give you some of the effects you see -- it is drawing them in the new location but they aren't really in the new location.
So, I would start by turning off any fill* settings (e.g., fillAfter) in your animation. See what your animation behaves like then. Most likely, it visually will now depict what the clicks tell you.
Then, set up an AnimationListener to get control when the animation ends, and at that point, make real changes to the widgets and their containers to affect your end positions.
Here is a sample project that demonstrates what I mean, albeit in an overly complicated fashion, since I am animating a custom View rather than an off-the-shelf widget.

Related

Setting the visibility for a linear layout(view group)

When I set setVisibility(View.GONE) or setVisibility(View.VISIBLE) for a linear layout it seems that it does not changes the visibility of the view(saying view group would be more accurate). At various post in SO, it has been suggested that get the count for the particular linear layout and change the visibility of each child one by one. Well that can work but it also increases the time taken to process that piece of code. And when there are various view involve, it will be increase to many folds. Same goes for setEnabled(true) or setEnabled(false).
My question is there a alternative for this, as directly changing the visibility is definitely not working? And if not can anybody explain to me why android choose to keep it this way, i.e developer cannot directly change the state(visibility/enabling) of the view group.

Can components be interacted with when their visibility is set to GONE?

I could not find anything that straight out answers this question. If I set the component (such as a Button, ListView, EditText) in the xml with a visibility of GONE, am I still able to interact with those components even though they are invisible? I tested it out with a Button and it seems to be no but I want to make sure.
When the visibility is set to GONE, the component is removed from the view hierarchy and no space is set aside for it. Moreover, there is no way for the user to interact with it (However, changes made to the View by the programmer while it is GONE become manifest when the visibility is changed to VISIBLE).
When the visibility is set to INVISIBLE, the component is still actually present in the view hierarchy, and space is calculated for it.

Android Drag and Drop and visibility of component

I have a Button in a fragment that the visibility property of the button is set to "GONE". And i want to set the visibility to "VISIBLE" when Drag and Drop operation starts. and also set the visibility to gone when drag and drop operation ends, How can i do this ?
Edit: i used the View.OnDragListener but when the visibility is set to "GONE" or "INVISIBLE" it will not call the View.OnDraglistener at all.
Thanks.
I faced this issue as well. Had a few images, which could be reordered, and also an area, on which when the image was dropped, it was deleted. All images had a specific drop listener, and the deletion area had a different drop listener (because of its purpose). Everything worked fine, and used all the standard API, but I wanted the drop area to appear only when the drag started and to disappear when the drag ends (or the item was dropped on it for deletion). And here this "bug" (I guess) appeared: if the visibility of the view is changed (initially its invisible, then make it visible when needed, then invisible again). It appears that if the visibility of the view is altered, the listener is canceled/missing/not working, and setting/resetting it doesn't change anything.
The solution for me was to set its transparency (alpha)
view.setAlpha(0); //transparent thus invisible
view.setAlhpa(1f); //opaque, visible
Wanted to share this in case someone will face this as well.
By the way, use INVISIBLE if you want to hide a view, GONE has different consequences http://developer.android.com/reference/android/view/View.html
Use onDrag Listener, here is the Android documentation on it
http://developer.android.com/reference/android/view/View.OnDragListener.html
and here is a good example:
http://www.vogella.com/tutorials/AndroidDragAndDrop/article.html

Android Animation - Button stays clickable

I am making a game in which I have 5 buttons, looking like clouds, falling from the "sky".
That means that when my activity starts, 'clouds' cannot be seen, since the marginTop is set to -100dp.
From that position they start falling down untill they get lost on the bottom side of the screen.
The thing is, I need those buttons to be clickable, during the process of animation.
So far, I found some documentation about how I can make the buttons clickable AFTER the animation ends. But I don't need that. I need to be able to click on the buttons through the animation time itself.
NOTE: I need something that works with versions before 3.0.
Anybody has any link to documentation or some example or anything ?
After doing some research, I found out that there are two types of animations:
View Animation and Property Animation.
The view animation can only animate View objects. It also lack a variety of animations, since it can do only stuff as scale, rotate, move... It cannot change background color, for example.
Also, the disadvantage of the View Animation is that it only change the position of where the View object is DRAWN. Physically, it still stays in the same position.
That's why the button is un-clickable, after the View Animation is finished upon it.
Property Animation, in the other hand, can animate both View and non-View objects and it doesn't have constraints as the View Animation.
When objects are moved, for example, with the property animation, they are not just drawn on some other position on the screen, but they are actually MOVED there.
Now, Property Animation is a lot more complex to write than the View Animation, so if you don't really need all the advantages of the Property Animation, it is suggested to use View Animation.
Source:
Property vs View Animation
Tutorial and SupportLybrary up to API 1:
nineoldandroids
You can change the buttons to imageViews and then do
imageView.setOnClickListener(myListener)
then set myListener to do whatever you previously wanted to happen on the buttons onClick. Your activity will have to implement OnClickListener
Added bonus: you can make the images look like clouds :)

Replacing views dynamically without changing activity

I would like to know how to go about doing this small problem that I am encountering while making a video player app.
On clicking the first control(the rectangular icon) in the above image the following view must be displayed instead of it which I am quite unsure as to how to do it. Here is what it is replaced by
Also please note, by any chance the activity should not be changed. I have been able to design the views individually but having problem changing them at runtime when user clicks. Could someone go about explaining as to how it can be done or provide some suitable links to achieve my goal. Thanks.
For something as simple as this you can just change the visibility of the views.
view.setVisibility(View.INVISIBLE)
Or the more effective:
view.setVisibility(View.GONE)
Do that on the views you want gone, I suggest a wrapper class. It's either this or changing the contentView as describded below.
this.setContentView(R.layout.newLayoutToUse);
However, I have a feeling there is a better way to do what you want. It's overkill to load a complete new layout if you just want to change the image of some buttons or imageviews.
This might be a stupid solution, 'cause i'm terribly tired right now, but why not use the bringToFront() method on the View that you want to display in the front? Display them both in front of each other, maybe in a RelativeLayout, and then swap between them as you wish.
They are small objects, so don't consume memory. I don't see why this shouldn't work.
OR
Place them above one another, so they overlap and then make the above view visible/invisible depending on which one you need to display.
OR
just remembered I read somewhere that you can scroll through a ScrollView automatically from code. So display both Views in a ScrollView in succession and when pressing the button or whatever, you scroll down to make the next menu visible. When pres back, you scroll up to make the previous thing available. Should work, and might also make a nice animation between changing of the menus.

Categories

Resources