I am trying to change the height of the view programmatically.
What i have tried is -
RelativeLayout rlOne;
rlOne = (RelativeLayout) findViewById(R.id.rlOne);
And on some click event i am changing the height using the LayoutParams.
rlOne.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, NEWHEIGHT));
The height is increased but the transition is not smooth.
How can i achieve a smooth transition from view's original height to view's new height?
Can i slow down the transition ?
Basically the idea is that, you calculate the new height (the height after your view increasing) view first and write an thread, increase the height of you old view (the height of your view before increasing) pixel by pixel(may be 5-10 pixels at a time) until it reaches the new height. Hope the idea could help.
Play with from/to/speed values
ScaleAnimation scaleAnimation = new ScaleAnimation(from, to, from, to, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
rlOne.setAnimation(scaleAnimation);
Source:
https://stackoverflow.com/a/1624689/1276374
You could write a for loop, where the view's height is increased by very little, than a pause is made before increasing the counter. In this way, the user will have the impression of a smooth transition.
Use
LinearLayout linearLayout = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
Related
I have an activity like below screen.
View 1 have width and height as match_parent respectively. View 2 have width match_parent and fixed (200 dp) height.
On a button click I change the visibility (from visible to gone) of View 2. As soon as I hide View 2, View 1 comes in full screen instantly or with a jerk. I want the View 1 to gradually expand to full screen with animation instead of instantly.
How can I animate this layout size change?
You can set android:animateLayoutChanges="true" on the parent layout. This works from API 11 and onward.
You need to use scale animation for view1 and fade out for view2 and then use Animatorset to run both at the same time. For example for a simple scale animation
ScaleAnimation anim = new ScaleAnimation(fXscale, toXscale, fYscale, tYscale, Animation.RELATIVE_TO_SELF, (float)0.5, Animation.RELATIVE_TO_SELF, (float)0.5);//fix to fit your needs.
I'm changing the width of a grid view based on its column width and number of columns (which works):
gridView.setLayoutParams(new LinearLayout.LayoutParams(gridManipulation.getColumnWidth() * (int)Math.sqrt(str.length) + 10, LayoutParams.FILL_PARENT));
gridView.setGravity(Gravity.CENTER);
I've tried re-centering it in the parent view but its not working, does anyone know why, its still centred as if it still has the width parameters before my dynamic change.
Set the Gravity as LayoutParams property then set that LayoutParams to your GridView as follows...
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(gridManipulation.getColumnWidth() * (int)Math.sqrt(str.length) + 10, LayoutParams.FILL_PARENT);
params.gravity = Gravity.CENTER;
gridView.setLayoutParams(params);
you cant center it if its bigger than its parent, which is never bigger than the screen, unless you set it manually, which will cause the system to render data outside the screen and slow you way down.....
is it expanding verticaly or horizontally or both? my recomendation would be to put it in a scrollview.
i have a view in linear layout. the app is music notation which is a series of musical bars which line wrap so they always less than width of screen (no horizontal scrolling). there is a vertical scrollbar when the music score gets too big for the view. so far so good. i can scroll up and down fine.
now i'm implementing pinch zoom, i set up an onScale in a ScaleGestureDetector, save the scale factor, invalidate and in onDraw i use the scale factor like this canvas.scale(mScaleFactor, mScaleFactor);
it scales the view ok except its not doing exactly what i want. i want the drawing area of the canvas to scale as it does but the scrollbar to stay in place, but zooming sends it off to the side, out of view.
any idea how to scale the view but not the scrollbar?
BTW, i can recalculate where to line wrap my music bars by applying the scalefactor to the width, its really just a problem with the scrollbar display.
can anyone help? thanks
I figured out the way. I needed to decouple the drawing of the scrollbar from the drawing of the main music notation view. that way I can zoom the main notation view without affecting the scroll bar.
so I use linear layout to create 2 views -
layout.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT,
100));
layout.setOrientation(LinearLayout.HORIZONTAL);
Globals.mMainView = new MainView(context);
Globals.mMainView.setLayoutParams(new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 60));
layout.addView(Globals.mMainView);
Globals.mScrollbarView = new ScrollbarView(context);
Globals.mScrollbarView.setLayoutParams(new LinearLayout.LayoutParams(20, LinearLayout.LayoutParams.WRAP_CONTENT, 0));
layout.addView(Globals.mScrollbarView);
then I remove display of scrollbars from MainView and put them in ScrollbarView.
then I use onScroll events in ScrollbarView to control scrolling in MainView. I have to remember to return the actual display length of MainView from ScrollbarView.computeVerticalScrollRange so the scroll bar size shows up ok.
since I have reference to both views saved as globals, its easy for them to talk to each other as necessary.
I have a RelativeLayout (vertical) defined to include 3 Views. The top and bottom views are 16 pixels high, and display text. The middle view displays graphics for a game application.
At various points during game play, the app runs a TranslateAnimation that effectively "scrolls" the middle view up or down. This keeps the game character in the game "centered" in the middle view.
My problem is that when the animation runs, the translation causes the top or bottom views to be overwritten. This lasts only for the duration of the animation. When the animcation completes, everything is fine and the top/bottom views display their text as expected. What is surprizing to me is that the translation animation displays content outside of the (middle) view in which it is running. My desire is that when the translation slides the display up/down, that the animation only affects the middle view and does not obscure content in the top or bottom views.
The following is the code I am using to create the layout. The custom views derive directly from View and display content in their onDraw(Canvas) methods.
RelativeLayout rl = new RelativeLayout(this);
topView = new MyTextView(this);
topView.setId(1);
middleView = new MyGameView(this);
middleView.setId(2);
bottomView = new MyTextView(this);
bottomView.setId(3);
final int textSize = 16;
RelativeLayout.LayoutParams lpTop = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, textSize);
lpTop.addRule(RelativeLayout.ALIGN_PARENT_TOP);
RelativeLayout.LayoutParams lpBottom = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, textSize);
lpBottom.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
RelativeLayout.LayoutParams lpMiddle = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
lpMiddle.addRule(RelativeLayout.BELOW, 1);
lpMiddle.addRule(RelativeLayout.ABOVE, 3);
rl.addView(topView, lpTop);
rl.addView(middleView, lpMiddle);
rl.addView(bottomView, lpBottom);
setContentView(rl);
The code in middleView that launches the animation is as follows:
TranslateAnimation ta = new TranslateAnimation(0, 0, 0, yDistance);
ta.setDuration(500);
startAnimation(ta);
I have tried a number of things to get this animation to behave as desired, but have been unable to do so. Hopefully someone out there can help. Thanks in advance.
Hi I'd like to programatically increase the height allocated to a TextView, and have the activity layout redrawn accordingly (the text view has a maximum height until the user clicks it, then it takes up all height required, wrap_content).
setHeight() isn't working, even coupled with invalidate() or postInvalidate(). I am able to change the contents of the TextBox with setText() but it isn't altering the existing specified height.
Android 1.5 under the 1.6 SDK.
Didn't test that , but try to create new Layout params and assign it to a view
This is for a button, but idea is same.
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.weight = 0;
shareBtn.setPadding(50, 0, 50, 0);
shareBtn.setLayoutParams(params);