I have two HorizontalScrollViews in my layout and I want to move them at the same. So if I touch scrollview a I want to move scrollview b at the same time and vice versa. I have done some searching around and haven't found a good solution. I have gotten it close by setting an onTouchListener() and trying to manually move each but I can't get it to move smoothly. Any ideas?
I was able to figure out what I needed to do. I noticed that there is a protected method called onScrollChanged() in the ScrollView and HorizontalScrollView classes. So I subclassed HorizontalScrollView and implemented that method manually. From there I just created a callback that gets called when onScrollChanged() is invoked. Works like a charm. Why Google didn't make that method public is beyond me.
Related
I'm trying to create an overlay that is triggered when a button is pressed. This overlay is supposed to allow the user to add their contact and I was wondering how can I use fragments to get this effect like you can see in this mockup.
I am in a dilemna over using fragments is the right choice. My reasoning being that I only need to have it do one task that is adding contacts, and thus I do not need a whole activity.
This is what I have on the main activity right now
I would really appreciate any help on understanding how to make this work.
You can use a DialogFragment.
It behaves like a normal Fragment for the most part. Here is a guide for a basic implementation https://guides.codepath.com/android/using-dialogfragment
They appear automatically in the center of the screen. To get it a bit lower like in your mockup you can change where it is in the window. Here is an answer showing such Position of DialogFragment in Android
I'm developing an Android App with Xamarin.Android and try to update a View about 50 times per second. I used the RelativeLayout.LayoutParams to change the Margin of the View, so that it is moving around depending on the accelerometer values. Unfortunately this does not work well, because it seems like android does not redraw fast enought. Is there an alternative for LayoutParams, or how can I force Android to redraw my View faster?
The View is only a small ImageView. It should not be a lot of work to draw it to it's new position. So I don't know why this is a problem for Android.
Thanks in advance!
Ok, it seems like the problem was not the android performance. Had some thread based mistakes in my code. Sorry for that. By the way I like to tell you what the solution was.
I wrote a new class extending RelativeLayout and override the onDraw() Method. This class wrapped my other stuff which is needed for calculation and movement. To move the View I also had to call postInvalidate() Method.
Thanks!
I just do a simple project where I try to show/hide a layout on the top of a LinearLayout with TranslateAnimation. There was a flicker because when I call onAnimationEnd(), the animation wasn't finished for 0.1sec.
Example:
#Override
public void onAnimationEnd(Animation animation) {
retractableLayout.setVisibility(View.GONE);
}
When I search on stackoverflow, I found there's another way to do it. With ObjectAnimator. After using it, my animation was fine without a View.GONE
What is the difference between TranslateAnimation and ObjectAnimator? Is one of them is deprecated and they do the same thing or there's a time when one or the other is better.
Here's a github repo with the 2 versions (https://github.com/charlesvigneault/AAA_Test1)
Thanks
The difference is mainly that if you use a TranslateAnimation, the view which you are animating does not really leave its original position on the screen, it just makes it look like it is moving. So the view basically doesnt change its coordinates.
Check this video about View Animations :
https://www.youtube.com/watch?v=_UWXqFBF86U
If you use an ObjectAnimator the view really changes its actual position.
TranslateAnimation is not deprecated, you can still find it on Lollipop, but for most cases I can recommend a class called ViewPropertyAnimator , which many people still dont seem to know about, it is probably the easiest and most straight forward way to animate a view, and can also save you a lot of code. Heres an example :
retractableLayout.animate()
.translationX(toX)
.translationY(toY)
.setDuration(duration)
.setInterpolator(interpolator)
.setStartDelay(startDelay);
You can also set a listener etc., be sure to check the available methods.
And check out this really helpful video :
https://www.youtube.com/watch?v=3UbJhmkeSig
TranslateAnimation is depracated since android 3.0 and ObjectAnimator is the way to go. Object animator is much more flexible as it allows you to "animate" any object property that has proper setter and getter implemented. Check official android dev guide
http://developer.android.com/guide/topics/graphics/overview.html
I have defined my Relative Layout using the drag and drop tool in Eclipse, so all of my buttons are laid out how I wish. My issue is when I set the onClick listener, that calls a method in another class. So to be able to redraw items on screen, I need to access the layout manager so I can add and remove buttons from the screen as well as update textViews. I have done all of this in a demo I made in Java, and I used a JPanel with GridBagConstraints. Now that I am moving to Android, a system I haven't done much development in, I am at the point where I have to learn some new stuff. For example in my demo I made I could do this:
grid.remove(trueButton);
grid.add(falseButton);
grid.remove(textField);
grid.add(backButton);
Essentially I want to be able to do the same sort of thing in my Android app. If you guys need more info I can provide, I wasn't really sure how much would be needed since I am looking at really just where to start. Everything has been declared in the XML since the drag and drop part of Eclipse does that all for me. It is just the Java part that is giving me some issue.
Why not just setVisibility of the buttons you wish to hide/show? Same with the TextViews.
You can set visibility to 'GONE' and it will be as if the view has been removed (taking up no space in the layout and not responding to touch events.).
Is there a way to create an onChildAdded or onItemAdded listener to a GridView in android? I can't seem to find one online. Right now my approach is lacking, I feel that it should be improved. I have a worker thread running that remembers the number of children that the GridView has, then if it has more it calls a method that handles the event and if there are less then it calls another method. I'm sure there has to be a better way, but I don't know of one.
Any suggestions are welcome.
You should write your own Adapter, extending BaseAdapter. Check this out.