It seems like I have a problem with java code where I want to use LayoutParams in while loop.
So in this code, I thought it might be like this but not working.
If is width and height of the image > 0 , then start looping by subtracting height and width till 0.
Does anyone have some fix for this?
Here is the code:
public void run() {
ViewGroup.LayoutParams params = myBall.getLayoutParams();
while ((params.height > 0) && (params.width > 0)) {
params.width--;
params.height--;
myBall.setLayoutParams(params);
}}
Ok, try this
Create a folder named anim in your res and add anim_out.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true" >
<scale
xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="250"
android:fromXScale="1"
android:fromYScale="1"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="0"
android:toYScale="0" >
</scale>
<alpha
android:duration="250"
android:fromAlpha="1"
android:toAlpha="0" />
</set>
do this in your main java,
//Your view could be any View or ViewGroup
View view = (View) findViewById(R.id.view1);
view.setAnimation(AnimationUtils.loadAnimation(context,
R.anim.anim_out));
view.setVisibility(View.GONE);
Try this and tell me :). Happy coding
Try doing it like this:
public void run() {
while ((myBall.getLayoutParams().height > 0) && (myBall.getLayoutParams().width > 0))
{
myBall.getLayoutParams().width--;
myBall.getLayoutParams().height--;
}
}
Let me know if it did the trick.
Related
How to hide/show view when scrolling up/down android like Foodpanda app
I want to hide/show view (linear or relative layout) when ScrollView is up/down like this above gif.
But my app I do not use Recyclerview or list view (just textview).
How can I create it?
Thanks!
Add scroll listener to the RecylerView
If the user is scrolling down - then start translation animation UPWARDS
If the user is scrolling up - then start translation animation DOWNWARDS
Anim translation UPWARDS:- (trans_upwards.xml)
<?xml version="1.0" encoding="utf-8"?>
<set
xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/linear_interpolator"
android:fillAfter="true">
<translate
android:fromYDelta="0%p"
android:toYDelta="100%p"
android:duration="300"
/>
</set>
Anim translation DOWNWARDS:-(trans_downwards.xml)
<?xml version="1.0" encoding="utf-8"?>
<set
xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/linear_interpolator"
android:fillAfter="true">
<translate
android:fromYDelta="100%p"
android:toYDelta="0%p"
android:duration="300"
/>
</set>
Add scroll listener to recyclerView (and also do a checking)
boolean check_ScrollingUp = false;
mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
if (dy > 0) {
// Scrolling up
if(check_ScrollingUp)
{
YourView.startAnimation(AnimationUtils.loadAnimation(context,R.anim.trans_downwards));
check_ScrollingUp = false;
}
} else {
// User scrolls down
if(!check_ScrollingUp )
{
YourView
.startAnimation(AnimationUtils
.loadAnimation(context,R.anim.trans_upwards));
check_ScrollingUp = true;
}
}
}
#Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
}
});
I made an application which use the ActionBarCompat
I created the tabs using the SlidingTabLayout class.
the class is this:
SlidingTabLayout.java
but I can not change the color of the tabs...
my viewpager fragment is this:
<swmovil.fyb.SlidingTabLayout
android:id="#+id/mTabs"
android:layout_width="match_parent"
android:layout_height="48dip" />
<android.support.v4.view.ViewPager
android:id="#+id/mPager"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="1"
android:background="#color/white" />
the application works great, but i canĀ“t change the color text of the tabs...
I made the application after seeing the following example:
rudsonlive/Navigation-Drawer-ViewPager-ActionBarCompat
How can i change the text color of the tabs text ?
thanks !!!
1) First of all create color folder under res (/res/color)
2) create xml file selector.xml under /res/color folder
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:color="#android:color/white" />
<item android:state_focused="true" android:color="#android:color/white" />
<item android:state_pressed="true" android:color="#android:color/white" />
<item android:color="#504f4f" />
</selector>
3) Then in the populateTabStrip() method in SlidingTabLayout put this
tabTitleView.setTextColor(getResources().getColorStateList(R.color.selector));
now you have a selector and you can change the color of the text on any event you want
if that is not working add the following lines of code.
a) in populateTabStrip() method at the end add this
if (i == mViewPager.getCurrentItem()) {
tabView.setSelected(true);
}
and b) change the onPageSelected() method to this
#Override
public void onPageSelected(int position) {
if (mScrollState == ViewPager.SCROLL_STATE_IDLE) {
mTabStrip.onViewPagerPageChanged(position, 0f);
scrollToTab(position, 0);
}
for (int i = 0; i < mTabStrip.getChildCount(); i++) {
mTabStrip.getChildAt(i).setSelected(position == i);
}
if (mViewPagerPageChangeListener != null) {
mViewPagerPageChangeListener.onPageSelected(position);
}
}
Open your file SlidingTabLayout.java (the default one from Google IO) and find the function populateTabStrip() , then after this code
mTabStrip.addView(tabView);
if (i == mViewPager.getCurrentItem()) {
tabView.setSelected(true);
}
add the following line:
int color = ContextCompat.getColor(tabView.getContext(), R.color.grey);
tabTitleView.setTextColor(color);
Replace R.color.grey with your preferred color.
You should be able to see the TextView the class is using.
tabTitleView.setTextColor(getResources().getColor(R.color.white));
In my class, the TextView was tabTitleView. If you are using the default example provided by Google, you will find it under populateTabStrip function.
copy code of slidingtablayout and slidingtabstrip and put it in a java file.then make a customtab_title.xml in your layout folder and a selector.xml file in your drawable folder.
`
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp"
>
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:textColor="#drawable/slidingtab_title_color"/>
</LinearLayout>
selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:color="#color/unpressed" />
<item android:state_focused="true" android:color="#color/unpressed" />
<item android:state_pressed="true" android:color="#color/unpressed" />
<item android:color="#android:color/black" />
</selector>
And in your mainactivity or where u r showing your tabs add one line of code - tabs.setCustomTabView(R.layout.customtab_title, R.id.textView2);
here tabs is slidingtablayout tabs;
to change indicator color add -
tabs.setSelectedIndicatorColors(getResources().getColor(R.color.unpressed));
#Override
public void onPageSelected(int position) {
for (int i = 0; i < mTabStrip.getChildCount(); i++) {
TextView tv = (TextView) mTabStrip.getChildAt(i);
if (i==position)
tv.setTextColor(getResources().getColorStateList(R.color.white));
else
tv.setTextColor(getResources().getColorStateList(R.color.tab_text_color));
}
this may be help you
Unfortunately this class doesn't support customizing the tab text color without editing the code and always uses the default text color of the theme. You'll have to patch the class to allow setting the tabs text color by code or by style attribute.
One alternative is to use the PagerSlidingTabStrip library.
Looking at the code for the SlidingTabLayout...You can set a custom tab view, which allows you to control the content of the tab and set a custom tab text color. Have a look at slidingTabLayout.setCustomTabView(int layoutResId, int textViewId).
I use Panayiotis Irakleous solution but I think it is better to avoid looping part in onPageSelected procedure.
The steps are the same, you need to add an int class member (example: mCurrentTabIndex) to save current tab index.
In steps 3.a, you need to add
mCurrentTabIndex = i;
So it will be like this:
if (i == mViewPager.getCurrentItem()) {
tabView.setSelected(true);
mCurrentTabIndex = i;
}
Last, in steps 3.b, replace the looping part to this:
mTabStrip.getChildAt(mCurrentTabIndex).setSelected(false);
mTabStrip.getChildAt(position).setSelected(true);
mCurrentTabIndex = position;
So the code will be like this:
#Override
public void onPageSelected(int position) {
if (mScrollState == ViewPager.SCROLL_STATE_IDLE) {
mTabStrip.onViewPagerPageChanged(position, 0f);
scrollToTab(position, 0);
}
mTabStrip.getChildAt(mCurrentTabIndex).setSelected(false);
mTabStrip.getChildAt(position).setSelected(true);
mCurrentTabIndex = position;
if (mViewPagerPageChangeListener != null) {
mViewPagerPageChangeListener.onPageSelected(position);
}
}
Could anyone tell me how they have got RotateDrawable to work whether it be from code or XML or both? The documentation on animating Drawables is pretty poor and animation only seems to work for images. I want to be able to animate all drawables. When i tried to get a RotateDrawble from XML is just causes an exception. What is the correct function to find a RotateDrawable from XML?
here's a nice solution for putting a rotated drawable for an imageView:
Drawable getRotateDrawable(final Bitmap b, final float angle) {
final BitmapDrawable drawable = new BitmapDrawable(getResources(), b) {
#Override
public void draw(final Canvas canvas) {
canvas.save();
canvas.rotate(angle, b.getWidth() / 2, b.getHeight() / 2);
super.draw(canvas);
canvas.restore();
}
};
return drawable;
}
usage:
Bitmap b=...
float angle=...
final Drawable rotatedDrawable = getRotateDrawable(b,angle);
root.setImageDrawable(rotatedDrawable);
another alternative:
private Drawable getRotateDrawable(final Drawable d, final float angle) {
final Drawable[] arD = { d };
return new LayerDrawable(arD) {
#Override
public void draw(final Canvas canvas) {
canvas.save();
canvas.rotate(angle, d.getBounds().width() / 2, d.getBounds().height() / 2);
super.draw(canvas);
canvas.restore();
}
};
}
also, if you wish to rotate the bitmap, but afraid of OOM, you can use an NDK solution i've made here
You have to animate the "level" property, where 0 is the start value and 10000 is the end value.
The below example animates from start to finish, you can reverse the animation easily with this method.
final RotateDrawable rotateDrawable = ...
ObjectAnimator.ofInt(rotateDrawable, "level", 0, 10000).start();
I would like to add a full example of animating a progress icon on ImageView, it is based on Mark Hetherington answer.
So my animation looks as follows:
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:pivotX="50%"
android:pivotY="50%"
android:fromDegrees="0"
android:toDegrees="-360"
android:duration="100"
android:drawable="#drawable/ic_loop_black_24dp"
/>
icon comes from https://material.io/icons/
then my layout contains an ImageView as follows:
<ImageView
android:id="#+id/progress"
android:layout_marginTop="0dp"
android:layout_marginLeft="-3dp"
android:layout_width="30dp"
android:layout_height="30dp"
android:visibility="gone"
android:scaleType="fitCenter"
android:background="#drawable/progress_anim"
android:layout_gravity="center_horizontal|center_vertical"
/>
and finally in code when I need to show animation I do:
RotateDrawable rotateDrawable = ((RotateDrawable)progressImage.getBackground());
ObjectAnimator anim = ObjectAnimator.ofInt(rotateDrawable, "level", 0, 10000);
anim.setDuration(1000);
anim.setRepeatCount(ValueAnimator.INFINITE);
anim.start();
RotateDrawable does not seem to be animated. Instead, you have to use setLevel to change the rotation of the drawable.
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="#drawable/your_drawable"
android:fromDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:toDegrees="360" />
And set the level will rotate the drawable:
final ImageView image = (ImageView)findViewById(R.id.imageView1);
final RotateDrawable drawable = (RotateDrawable)image.getDrawable();
drawable.setLevel(500);
The following code returns a Drawable wrapper that rotates another Drawable programmatically:
Drawable rotateDrawable(Drawable d, final float angle) {
// Use LayerDrawable, because it's simpler than RotateDrawable.
Drawable[] arD = {
d
};
return new LayerDrawable(arD) {
#Override
public void draw(Canvas canvas) {
canvas.save();
canvas.rotate(angle);
super.draw(canvas);
canvas.restore();
}
};
}
I haven't worked with a RotateDrawable, but if you're simply trying to animate rotation on a graphic, you don't need it. Drawables with a 'level' like RotateDrawable are meant to convey information rather than animate views.
The following code rotates an ImageView around its center:
ImageView myImageView = (ImageView)findViewById(R.id.my_imageview);
AnimationSet animSet = new AnimationSet(true);
animSet.setInterpolator(new DecelerateInterpolator());
animSet.setFillAfter(true);
animSet.setFillEnabled(true);
final RotateAnimation animRotate = new RotateAnimation(0.0f, -90.0f,
RotateAnimation.RELATIVE_TO_SELF, 0.5f,
RotateAnimation.RELATIVE_TO_SELF, 0.5f);
animRotate.setDuration(1500);
animRotate.setFillAfter(true);
animSet.addAnimation(animRotate);
myImageView.startAnimation(animSet);
Since you're trying to use Almero's Android Gesture Detectors, I decided to do the same in order to find an appropriate solution:
public class MainActivity extends Activity {
private RotateGestureDetector mRotateDetector;
private float mRotationDegrees = 0.f;
private static final float ROTATION_RATIO = 2;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRotateDetector = new RotateGestureDetector(getApplicationContext(), new RotateListener());
}
#Override
public boolean onTouchEvent(MotionEvent event) {
mRotateDetector.onTouchEvent(event);
return super.onTouchEvent(event);
}
private class RotateListener extends RotateGestureDetector.SimpleOnRotateGestureListener {
#Override
public boolean onRotate(RotateGestureDetector detector) {
mRotationDegrees -= detector.getRotationDegreesDelta();
ImageView v = (ImageView) findViewById(R.id.imageView);
// For NineOldAndroids library only!
ViewHelper.setRotation(v, mRotationDegrees * ROTATION_RATIO);
// For HONEYCOMB and later only!
v.setRotation(mRotationDegrees * ROTATION_RATIO);
return true;
}
}
}
It works fine for me (I'm able to rotate the ImageView with two-finger rotation gesture.
NOTE: Don't forget to chose appropriate rotation method call. I commented both of them to draw your attention.
ROTATION_RATIO is just a multiplier to speed-up a rotation response on my fingers movement.
You can use any rotation axis (setRotation(), setRotationX() and setRotationY()) methods for a View.
To enable support of this code on Android devices with API Level lower than 11 (pre-Honeycomb devices) you may want to engage NineOldAndroid library.
You could manually call RotatedDrawable.setLevel() to rotate the drawable, or you could read the code of ProgressBar, the indeterminate drawable is a LayerDrawable whose children were RotatedDrawable, like this one:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<rotate
android:drawable="#drawable/spinner_48_outer_holo"
android:pivotX="50%"
android:pivotY="50%"
android:fromDegrees="0"
android:toDegrees="1080" />
</item>
<item>
<rotate
android:drawable="#drawable/spinner_48_inner_holo"
android:pivotX="50%"
android:pivotY="50%"
android:fromDegrees="720"
android:toDegrees="0" />
</item>
</layer-list>
The rotate animation was driven by ProgressBar's onDraw method.
This is a good working example. Param duration is used to animate it.
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="4000"
android:fromDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:toDegrees="720" >
<shape
android:innerRadius="20dp"
android:shape="ring"
android:thickness="4dp"
android:useLevel="false" >
<size
android:height="48dp"
android:width="48dp" />
<gradient
android:centerY="0.5"
android:endColor="#android:color/white"
android:startColor="#00ffffff"
android:type="sweep"
android:useLevel="false" />
</shape>
</rotate>
If you want to rotate drawable forever you can use animated-rotate tag in drawable xml like this.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<animated-rotate android:drawable="#drawable/ic_refresh" android:pivotX="50%" android:pivotY="50%" />
</item>
</selector>
Currently I'm trying to implement something in my app where I really don't know where to start.
Have a look at this little image:
You can find the app in play store here: https://play.google.com/store/apps/details?id=com.imano.euro2012.row
In my app, I have a listview and when i tap on an item I want to slide in the black activity in to about 3/4. In that activity I want to have some llistview item specific options.
Any one knows how to solve this?
Solution:
Thanks to Imran-Khan I got it working.
But I think this code is not perfect. I'm not sure if the width and height calculation in the first half of the showPopup() method is correct. And in my solution the popup has on the bottom and on the right a little margin. I don't know right now why this happens. Maybe someone can help...
Here is what I did so far:
First I added the method showpopup(long selectedItem) to my listview:
lv_timer.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parentView, View childView, int position, long id) {
showPopup(id);
}
});
and the method itself:
private void showPopup(long selectedItem) {
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
int popupWidth = (width / 4) * 3;
int popupHeight = height;
LinearLayout viewGroup = (LinearLayout) findViewById(R.id.ll_timer_prop);
LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = layoutInflater.inflate(R.layout.timer_properties, viewGroup);
final PopupWindow popup = new PopupWindow(this);
popup.setContentView(layout);
popup.setWidth(popupWidth);
popup.setHeight(popupHeight);
popup.setFocusable(true);
popup.showAtLocation(layout, Gravity.NO_GRAVITY, width - (width / 4 * 3), 0);
TextView tv_item = (TextView) layout.findViewById(R.id.tv_item);
tv_item.setText("Clicked Item ID: " + selectedItem);
}
This is working fine for me.
for the slide in part I've found this thread: PopupWindow animation not working
I added
popup.setAnimationStyle(R.style.AnimationPopup);
before the showAtLocation() call, created a res/anim directory an created two XML files in it: popup_show.xml and popup_hide.xml
popup_show.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale
android:fromXScale="0.0" android:toXScale="1.0"
android:fromYScale="1.0" android:toYScale="1.0"
android:pivotX="100%" android:pivotY="0%"
android:duration="#android:integer/config_shortAnimTime"
/>
<alpha
android:interpolator="#android:anim/decelerate_interpolator"
android:fromAlpha="0.0" android:toAlpha="1.0"
android:duration="#android:integer/config_shortAnimTime"
/>
</set>
popup_hide.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale
android:fromXScale="1.0" android:toXScale="0.0"
android:fromYScale="1.0" android:toYScale="1.0"
android:pivotX="100%" android:pivotY="0%"
android:duration="#android:integer/config_shortAnimTime"
/>
<alpha
android:interpolator="#android:anim/decelerate_interpolator"
android:fromAlpha="1.0" android:toAlpha="0.0"
android:duration="#android:integer/config_shortAnimTime"
/>
</set>
You can create this view using custom PopupWindow
see this tutorial for Creating Custom PopupWindow
How to create popups in Android
You can use QuickAction For such behaviour, Like shown on third screen
Edit
Sorry I have not paid attention for the fact that you want a sliding from right to left effect, But I think you can try to customise slidindrawer in Android API to make slide from right to left.
Funny, I'm actually looking at the same problem right now.
I've found this topic:
Android Facebook style slide
Some nice examples in there.
I would like to try out different animation styles for a popup window using setAnimationStyle(), but I am struggling to understand the documentation.
developer.android.com, says: "animation style to use when the popup appears and disappears. Set to -1 for the default animation, 0 for no animation, or a resource identifier for an explicit animation."
It does not give any examples, or tell me what choices of resource are available. I suspect the best animation for my purposes will be sliding in from the right... does that exist as an option? Are these things I can select from a list or do I have to somehow create my own?
EDIT: My current code for making my popup window is something like this (simplified):
public void completed_dialog()
{
runOnUiThread(new Runnable()
{
public void run()
{
View layout = inflater.inflate(R.layout.endofgame, null, false);
Button b1 = (Button) layout.findViewById(R.id.pu_menu);
Button b2 = (Button) layout.findViewById(R.id.pu_repeat);
Button b3 = (Button) layout.findViewById(R.id.pu_next);
b1.setBackgroundResource(R.drawable.custom_menu_but);
b2.setBackgroundResource(R.drawable.custom_repeat_but);
b3.setBackgroundResource(R.drawable.custom_next_but);
b1.setOnClickListener(menu_button_click_listener);
b2.setOnClickListener(repeat_button_click_listener);
b3.setOnClickListener(next_button_click_listener);
final PopupWindow pw = new PopupWindow(layout, canvas_width,
canvas_width /2, true);
pw.setAnimationStyle(android.R.style.Animation_Dialog);
pw.showAtLocation(game_frame_layout, Gravity.CENTER, 0, 0);
}
}
}
I have now put the following in a file res/anim/test.xml :
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale
android:fromXScale="0.3" android:toXScale="1.0"
android:fromYScale="0.3" android:toYScale="1.0"
android:pivotX="0%" android:pivotY="0%"
android:duration="#android:integer/config_shortAnimTime"
/>
<alpha
android:interpolator="#android:anim/decelerate_interpolator"
android:fromAlpha="0.0" android:toAlpha="1.0"
android:duration="#android:integer/config_shortAnimTime"
/>
</set>
The original code with pw.setAnimationStyle(android.R.style.Animation_Dialog); worked fine, in as much as the dialog appeared with the standard animation - but if I swapped that line with pw.setAnimationStyle(R.anim.test); there is no animation. I have no idea why.
this is working example of setAnimationStyle() on my end:
res/anim/in.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/decelerate_interpolator">
<translate
android:fromYDelta="854"
android:toYDelta="0"
android:duration="1000"/>
</set>
res/anim/out.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:interpolator="#android:anim/decelerate_interpolator"
android:fromYDelta="0"
android:toYDelta="854"
android:duration="10000"
/>
</set>
layout/main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/layout"
>
<Button
android:id="#+id/btn_show"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="show"
/>
</RelativeLayout >
layout/popup.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#cccccc"
>
<Button
android:id="#+id/btn_dismiss"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="dismiss"/>
</LinearLayout>
values/styles.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="PopupAnimation" parent="android:Animation" mce_bogus="1">
<item name="android:windowEnterAnimation">#anim/in</item>
<item name="android:windowExitAnimation">#anim/out</item>
</style>
</resources>
values/strings.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, MainActivity!</string>
<string name="app_name">Popupwindow</string>
</resources>
MainActivity.java :
public class MainActivity extends Activity {
/** Called when the activity is first created. */
boolean flag =false;
PopupWindow popupWindow;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init();
}
public void init() {
Button btn_show = (Button) findViewById(R.id.btn_show);
LayoutInflater inflater = LayoutInflater.from(this);
View layout = inflater.inflate(R.layout.popup, null);
popupWindow =new PopupWindow(layout, LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT);
Button btn_dismiss = (Button) layout.findViewById(R.id.btn_dismiss);
btn_dismiss.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
openMenu();
}
});
btn_show.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
openMenu();
}
});
}
public void btn_showOnClicked() {
openMenu();
}
public void btn_dismissOnClicked() {
openMenu();
}
public void openMenu() {
if (!flag) {
popupWindow.setAnimationStyle(R.style.PopupAnimation);
popupWindow.showAtLocation(findViewById(R.id.btn_show), Gravity.NO_GRAVITY, 0, 0);
popupWindow.setFocusable(true);
popupWindow.update();
flag =true;
} else {
popupWindow.dismiss();
popupWindow.setFocusable(false);
flag =false;
}
}
}
and in your code just add your anim file to style as:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="test" parent="android:Animation" mce_bogus="1">
<item name="android:windowEnterAnimation">#anim/test</item>
</style>
</resources>
and in code:
pw.setAnimationStyle(android.R.style.test);
Thanks & Happy Coding!!!
You can see the options in the documentation of R.style. But there aren't many though. If you want more than that you will need to create it yourself. See basic guide here.
A few more examples of custom made animations you may find at the simple-quickactions google code site.
As per your comment, I think you should specify the id of your style as R.style.Animation_test.