I have a listview with custom listadapter, which populates the listview with a checkbox and some textviews. When the user selects a checkbox, I need a button bar to slide into the view from the bottom of the screen and sit there. I have made the buttons-bar and can make it appear and disappear from screen by changing it's visibility to "gone" and "visible", but it does not do these with slide-in and slide-out effect. How do I make it do those animations??
How to make a buttons bar slide in from bottom upon clicking checkbox in ListView?
You want to use Animation xml resources.
Here is an example of an Animation xml that will 'slide' the object up from the bottom of the screen to where ever its place is within your layout:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromYDelta="100%p" android:toYDelta="0%p" android:duration="300"/>
</set>
You will put that in your res/anim folder then use this java code to animate your view:
slideUpIn = AnimationUtils.loadAnimation(this, R.anim.slide_up_in);
yourButtonBarView.startAnimation(slideUpIn);
You'll want to put the startAnimation call inside where you get the callback that your CheckBox has been checked.
Thank you! It worked nicely partially. I said partially because my buttonBarView's visibility has been set to "gone" initially. When checkbox is checked, and as soon as I set it to "visible", a black bar becomes visible at the bottom of the screen, and the button bar is later (depending on the set translate duration) sliding into this black bar. This does not look smooth. I am trying to set the y-coordinate of the top of my buttonBarView to equal to the screen height in pixels, so that I dont have to use setVisibility() method at all but dont know how to do it. Is there any better way?? –
Trysail
Im not sure about that. I haven't seen it do anything like that before. Perhaps try starting the animation before you make it visible. Or maybe make it start out as View.INVISIBLE instead of View.GONE. –
Illuse
Below is the runtime code implementation, modify accordingly if you need it for other purposes.
RelativeLayout rl = new RelativeLayout(this);
ImageButton btnBar = new ImageButton(this);
RelativeLayout.LayoutParams btnParams = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, 80);
btnParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
btnBar.setLayoutParams(btnParams);
btnBar.setBackgroundColor(Color.RED); // test with red background
TranslateAnimation a = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT, 0,
Animation.RELATIVE_TO_PARENT, 0,
Animation.RELATIVE_TO_PARENT, (float)100,
Animation.RELATIVE_TO_PARENT, (float)0);
a.setDuration(1000);
btnBar.startAnimation(a); // add animation while start
rl.addView(btnBar);
setContentView(rl);
© 2022 - 2024 — McMap. All rights reserved.