I'm currently working on a dark theme transition by restarting the activity, but I stumbled upon a problem: the transitions don't work and I have no idea why.
The code below is the theme resources I currently use:
<style name="Theme.TestApp" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">#color/grey_light</item>
<item name="colorPrimaryVariant">#color/primary_light</item>
<item name="colorOnPrimary">#color/on_primary_light</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">#color/teal_200</item>
<item name="colorSecondaryVariant">#color/teal_700</item>
<item name="colorOnSecondary">#color/white</item>
<!-- Customize your theme here. -->
<item name="android:statusBarColor">#color/bar</item>
<item name="background">#color/grey_light</item>
<item name="android:textColor">#color/list_text</item>
<item name="android:windowSplashScreenBackground" tools:targetApi="s">#color/primary_light</item>
<item name="android:windowAnimationStyle">#style/WindowAnimation</item>
</style>
<style name="WindowAnimation">
<item name="android:windowEnterAnimation">#android:anim/fade_in</item>
<item name="android:windowExitAnimation">#android:anim/fade_out</item>
</style>
And this is the code I use to restart the activity:
myswitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
restart();
}else {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
restart();
}
}
});
}
public void restart () {
Intent i = new Intent(getApplicationContext(), Settings.class);
startActivity(i);
finish();
}
}
Apparently, creating my own animations solved my problem.
fade_in.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true">
<alpha
android:interpolator="#android:anim/accelerate_interpolator"
android:fromAlpha="0.0"
android:toAlpha="1.0"
android:duration="500" />
</set>
fade_out.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true">
<alpha
android:interpolator="#android:anim/accelerate_interpolator"
android:fromAlpha="1.0"
android:toAlpha="0.0"
android:duration="500" />
</set>
Related
I want to use custom animation on dialog but duration is not working
Dialog
class GiftDialog(context: Context) : Dialog(context, R.style.AppMaskStatusTheme) {
private val viewBinding: DialogGiftBinding by lazy { DialogGiftBinding.inflate(LayoutInflater.from(context)) }
init {
setContentView(viewBinding.root)
window?.let {
it.setBackgroundDrawableResource(R.color.transparent)
it.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT)
it.setWindowAnimations(R.style.DialogAnimation)
}
}
}
style
<style name="DialogAnimation">
<item name="android:windowEnterAnimation">#anim/slide_bottom_to_top</item>
<item name="android:windowExitAnimation">#anim/slide_top_to_bottom</item>
</style>
<style name="AppMaskStatusTheme" parent="Theme.AppCompat.Light">
<item name="android:statusBarColor">#color/dark</item>
</style>
slide_bottom_to_top
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="1000"//It doesn't work what I change
android:fromYDelta="0%p"
android:toYDelta="100%p" />
</set>
slide_top_to_bottom
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="1000"//It doesn't work what I change
android:fromYDelta="100%"
android:toXDelta="0" />
</set>
I was following the Google provided example of how to use AnimationDrawable with an ImageView. You can find it here: http://developer.android.com/guide/topics/graphics/drawable-animation.html
imageView.setBackgroundResource(R.drawable.animation);
AnimationDrawable animation = (AnimationDrawable)imageView.getBackground();
animation.start();
When I run it I get the error:
java.lang.ClassCastException: android.graphics.drawable.BitmapDrawable cannot be cast to android.graphics.drawable.AnimationDrawable
Google seems to think this should work, but if you cannot cast a BitmapDrawable to AnimationDrawable I am not sure how this is supposed to work?
I figured out the solution to this problem.
imageView.setImageDrawable(getResources().getDrawable(R.drawable.animation));
AnimationDrawable animation = (AnimationDrawable) imageView.getDrawable();
animation.start();
I have no idea why Google's documentation says to use the background, but using setImageDrawable and getDrawable works. Honestly it makes more sense it would work this way than the other way anyways.
I had the same problem. I know this thread is some month old, but maybe somebody what to read about my experience.
I dont know why, but google doesnt accept Spacemarks like "_" in his Picturenames while using it for animation. I uses names like "loading_frame1", and it doesnt work. I changed the names to something like "loadingframe1" and it works....
Before:
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false">
<item android:drawable="#drawable/loading_frame1" android:duration="100" />
<item android:drawable="#drawable/loading_frame2" android:duration="100" />
<item android:drawable="#drawable/loading_frame3" android:duration="100" />
<item android:drawable="#drawable/loading_frame4" android:duration="100" />
<item android:drawable="#drawable/loading_frame5" android:duration="100" />
<item android:drawable="#drawable/loading_frame6" android:duration="100" />
<item android:drawable="#drawable/loading_frame7" android:duration="100" />
<item android:drawable="#drawable/loading_frame8" android:duration="100" />
<item android:drawable="#drawable/loading_frame9" android:duration="100" />
<item android:drawable="#drawable/loading_frame10" android:duration="100" />
<item android:drawable="#drawable/loading_frame11" android:duration="100" />
<item android:drawable="#drawable/loading_frame12" android:duration="100" />
<item android:drawable="#drawable/loading_frame13" android:duration="100" />
<item android:drawable="#drawable/loading_frame14" android:duration="100" />
<item android:drawable="#drawable/loading_frame15" android:duration="100" />
<item android:drawable="#drawable/loading_frame16" android:duration="100" />
<item android:drawable="#drawable/loading_frame17" android:duration="100" />
<item android:drawable="#drawable/loading_frame18" android:duration="100" />
<item android:drawable="#drawable/loading_frame19" android:duration="100" />
<item android:drawable="#drawable/loading_frame20" android:duration="100" />
</animation-list>
After:
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false">
<item android:drawable="#drawable/loadingframe1" android:duration="100" />
<item android:drawable="#drawable/loadingframe2" android:duration="100" />
<item android:drawable="#drawable/loadingframe3" android:duration="100" />
<item android:drawable="#drawable/loadingframe4" android:duration="100" />
<item android:drawable="#drawable/loadingframe5" android:duration="100" />
<item android:drawable="#drawable/loadingframe6" android:duration="100" />
<item android:drawable="#drawable/loadingframe7" android:duration="100" />
<item android:drawable="#drawable/loadingframe8" android:duration="100" />
<item android:drawable="#drawable/loadingframe9" android:duration="100" />
<item android:drawable="#drawable/loadingframe10" android:duration="100" />
<item android:drawable="#drawable/loadingframe11" android:duration="100" />
<item android:drawable="#drawable/loadingframe12" android:duration="100" />
<item android:drawable="#drawable/loadingframe13" android:duration="100" />
<item android:drawable="#drawable/loadingframe14" android:duration="100" />
<item android:drawable="#drawable/loadingframe15" android:duration="100" />
<item android:drawable="#drawable/loadingframe16" android:duration="100" />
<item android:drawable="#drawable/loadingframe17" android:duration="100" />
<item android:drawable="#drawable/loadingframe18" android:duration="100" />
<item android:drawable="#drawable/loadingframe19" android:duration="100" />
<item android:drawable="#drawable/loadingframe20" android:duration="100" />
</animation-list>
And here the LoadingAnimation.class Listing
package com.justkidding.animation;
import android.support.v7.app.ActionBarActivity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
public class LoadingAnimation extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_loading_animation);
}
#Override
public void onWindowFocusChanged (boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
ImageView animation = (ImageView)findViewById(R.id.aniimage);
animation.setBackgroundResource(R.drawable.loading_animation);
AnimationDrawable frameAnimation = (AnimationDrawable) animation.getBackground();
if(hasFocus) {
frameAnimation.start();
} else {
frameAnimation.stop();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.loading_animation, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Google's code works. The "can not be cast issue" which lead me here, was because I was not paying attention and put my animation.xml in res.anim instead of res.drawable.
However I agree using setImageDrawable and getDrawable works better.
About this problem, I had a little oversight on the detail on google's example code in the documentation and this might be the case for a couple of persons using the guide.
There is an separate xml file that holds the drawables, indicating the transitions and has the tag:
<animation-list
xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="true">
<item android:drawable="#drawable/rocket_thrust1" android:duration="200" />
<item android:drawable="#drawable/rocket_thrust2" android:duration="200" />
<item android:drawable="#drawable/rocket_thrust3" android:duration="200" />>
</animation-list>
The above file is named rocket_thrust, and it is this same file that is set as backgroundDrawable in the following lines:
ImageView rocketImage = (ImageView) findViewById(R.id.rocket_image);
rocketImage.setBackgroundResource(R.drawable.rocket_thrust);
rocketAnimation = (AnimationDrawable) rocketImage.getBackground();
Give a try at this and be sure that documentation has no errors.
Best of luck.
Complete process to do animation is :
1. Create XML layout with imageView
and
2. Create XML file for animation suppose drawable/animation.xml
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item android:drawable="#drawable/twit" android:duration="120"></item>
<item android:duration="120" android:drawable="#drawable/a111"></item>
<item android:duration="120" android:drawable="#drawable/a2"></item>
<item android:duration="120" android:drawable="#drawable/a3"></item>
<item android:duration="120" android:drawable="#drawable/a4"></item>
<item android:duration="120" android:drawable="#drawable/a5"></item>
<item android:duration="120" android:drawable="#drawable/a6"></item>
</animation-list>
now
3. Create Main Activity
Then Type this code
public class AnimationMe extends AppCompatActivity {
private ImageView imgView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.logoo);
imgView = (ImageView) findViewById(R.id.imgView);
// the frame-by-frame animation defined as a xml file within the drawable folder
/*imgView.setBackgroundResource(R.drawable.animation);*/
imgView.setImageDrawable(getResources().getDrawable(R.drawable.animation));
// It's not possible to start the animation during the onCreate.
}
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
AnimationDrawable animationDrawable = (AnimationDrawable)imgView.getDrawable();
if(hasFocus)
{
animationDrawable.start();
}
else
{
animationDrawable.stop();
}
}
}
*
Note : ImageView have background as drawable and give a name of
animation.xml not for a particular image and then call with
imageview.getDrawable in AnimationDrawable.
---- You can't Run Animation in onCreate Method. Set drawable property in Imageview in onCreate() but call AnimationDrawable method out of
block of onCreate().
*
Sure it will work !
Just to add more answer to this page based on my experience because Stackoverflow seems has a very limited answer for this issue
I my case I tried to animate my background layout which using drawable for rounded radius. I got logcat error
java.lang.ClassCastException: android.graphics.drawable.GradientDrawable cannot be cast to android.graphics.drawable.AnimationDrawable
Turn out I must set the background attribute on my layout file to this drawable file
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="true">
<item android:drawable="#drawable/rounded_corner" android:duration="80" />
<item android:drawable="#drawable/rounded_corner_gray_background" android:duration="80" />
<item android:drawable="#drawable/rounded_corner" android:duration="80" />
<item android:drawable="#drawable/rounded_corner_gray_background" android:duration="80" />
<item android:drawable="#drawable/rounded_corner" android:duration="80" />
<item android:drawable="#drawable/rounded_corner_gray_background" android:duration="80" />
<item android:drawable="#drawable/rounded_corner" android:duration="80" />
And then call this code on my Main Activity
val backgroundAnim = info_layout?.background as AnimationDrawable
backgroundAnim.start()
My mistake is previously I put #drawable/rounded_corner on layout file as background attribute.
Hope this can help somebody, as I spend 3 hours just to solve this issue.
I've set up a Dialog using AlertDialog.Builder, and assigned it a style that has the animations in it, but the dialog doesn't animate upon being shown. It just cuts right into the frame.
Here is the code for the Dialog:
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
RelativeLayout dialogLayout = (RelativeLayout)inf.inflate(R.layout.dialog_layout, null);
dialogLayout.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_PARENT));
final Spinner typeSpinner = (Spinner)dialogLayout.findViewById(R.id.type_spinner);
final Spinner classSpinner = (Spinner)dialogLayout.findViewById(R.id.class_spinner);
final DatePicker datePicker = (DatePicker)dialogLayout.findViewById(R.id.date_picker);
final EditText desc = (EditText)dialogLayout.findViewById(R.id.dialog_edit);
ArrayAdapter<CharSequence> typeAdapter = ArrayAdapter.createFromResource(getActivity(), R.array.types, android.R.layout.simple_spinner_item);
typeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
typeSpinner.setAdapter(typeAdapter);
getClassList();
ArrayAdapter<String> classAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, classes);
classAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
classSpinner.setAdapter(classAdapter);
builder.setMessage(" Add an Assignment");
builder.setView(dialogLayout);
Dialog addDialog = builder.create();
addDialog.getWindow().getAttributes().windowAnimations = R.style.PauseDialog;
addDialog.show();
Here is the styles.xml:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
<item name="android:actionBarStyle">#style/AppTheme.ActionBar</item>
</style>
<style name="AppTheme.ActionBar" parent="#android:style/Widget.Holo.Light.ActionBar">
<item name="android:background">#469558</item>
<item name="android:titleTextStyle">#style/MyActionBar.TitleTextStyle</item>
</style>
<style name="MyActionBar.TitleTextStyle" parent="#android:style/TextAppearance.Holo.Widget.ActionBar.Title">
<item name="android:textColor">#FFFFFF</item>
</style>
<style name="PauseDialog" parent="#android:style/Theme.Dialog">
<item name="android:windowAnimationStyle">#style/PauseDialogAnimation</item>
</style>
<style name="PauseDialogAnimation">
<item name="android:windowEnterAnimation">#anim/expand_slide</item>
<item name="android:windowExitAnimation">#anim/shrink</item>
</style>
And, for example, here is the expand_slide.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillEnabled="true"
android:fillAfter="true">
<scale
android:interpolator="#android:anim/accelerate_decelerate_interpolator"
android:fromXScale="0.0"
android:toXScale="1.0"
android:fromYScale="0.0"
android:toYScale="1.0"
android:duration="300"
android:pivotX="100%"
android:pivotY="100%"
android:fillAfter="false"
/>
<translate android:duration="300"
android:fromYDelta="10%"
android:toYDelta="0"
android:fromXDelta="-10%"
android:toXDelta="0"/>
Can anyone help me out? I've been trying a lot of things, including instantiating the Dialog with the style ID as one of the parameters, but I can't seem to get it to work.
Thanks for anyone who helps out.
I'm new to animations myself, but my understanding is that you need to build an Animation object 'myanamation'. In your case build the animation from your xml resource. Then from the View pertaining to the animation, call startAnimation(myanimation) after dialog.show().
View mydialogview = (ImageView)findViewById(R.id.yourviewid);
Animation myanimation = AnimationUtils.loadAnimation(this, R.anim.youranimationid);
mydialogview.startAnimation(myanimation);
Hope this helps some.
I have an AlertDialog that coming in and out with animations, and I want to add buttons to it, how can I do it?
Here is my AlertDialog code:
private void openPopUP() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Game Paused");
builder.setMessage("Check out the transition!");
dialog = builder.create();
dialog.getWindow().getAttributes().windowAnimations =
R.style.dialog_animation;
dialog.show();
}
Here is my style code:
<resources>
<style name="AppBaseTheme" parent="android:Theme.Light"/>
<style name="AppTheme" parent="AppBaseTheme"/>
<style name="dialog_animation">
<item name="android:windowEnterAnimation">#anim/slide_in_left</item>
<item name="android:windowExitAnimation">#anim/slide_out_right</item>
</style>
</resources>
Here is my anim folder files:
Slide in left:
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="100%p" android:toXDelta="0"
android:duration="500" />
Slide out right:
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="500"
android:fromXDelta="0"
android:toXDelta="100%p" />
I need to add 3 buttons inside the alertdialog, how can I do it? Thank you.
You can use the method builder.setPositiveButton(String title, DialogInterface.OnClickListener listener) and then builder.setNeutralButton(...) and finally builder.setNegativeButton(...)
I'm showing a DialogFragment when the user taps on a row in a ListView. I'd like to animate the showing of the dialog so that it grows from the center of the row. A similar effect can be seen when opening a folder from the launcher.
One idea that I've had is a combination of TranslateAnimation and ScaleAnimation. Is there another way?
Being DialogFragment a wrapper for the Dialog class, you should set a theme to your base Dialog to get the animation you want:
public class CustomDialogFragment extends DialogFragment implements OnEditorActionListener
{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
return super.onCreateView(inflater, container, savedInstanceState);
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
// Set a theme on the dialog builder constructor!
AlertDialog.Builder builder =
new AlertDialog.Builder( getActivity(), R.style.MyCustomTheme );
builder
.setTitle( "Your title" )
.setMessage( "Your message" )
.setPositiveButton( "OK" , new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int which) {
dismiss();
}
});
return builder.create();
}
}
Then you just need to define the theme that will include your desired animation. In styles.xml add your custom theme:
<style name="MyCustomTheme" parent="#android:style/Theme.Panel">
<item name="android:windowAnimationStyle">#style/MyAnimation.Window</item>
</style>
<style name="MyAnimation.Window" parent="#android:style/Animation.Activity">
<item name="android:windowEnterAnimation">#anim/anim_in</item>
<item name="android:windowExitAnimation">#anim/anim_out</item>
</style>
Now add the animation files in the res/anim folder:
( the android:pivotY is the key )
anim_in.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale
android:interpolator="#android:anim/linear_interpolator"
android:fromXScale="0.0"
android:toXScale="1.0"
android:fromYScale="0.0"
android:toYScale="1.0"
android:fillAfter="false"
android:startOffset="200"
android:duration="200"
android:pivotX = "50%"
android:pivotY = "-90%"
/>
<translate
android:fromYDelta="50%"
android:toYDelta="0"
android:startOffset="200"
android:duration="200"
/>
</set>
anim_out.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale
android:interpolator="#android:anim/linear_interpolator"
android:fromXScale="1.0"
android:toXScale="0.0"
android:fromYScale="1.0"
android:toYScale="0.0"
android:fillAfter="false"
android:duration="200"
android:pivotX = "50%"
android:pivotY = "-90%"
/>
<translate
android:fromYDelta="0"
android:toYDelta="50%"
android:duration="200"
/>
</set>
Finally, the tricky thing here is to get your animation grow from the center of each row. I suppose the row is filling the screen horizontally so, on one hand the android:pivotX value will be static. On the other hand, you can't modify the android:pivotY value programmatically.
What I suggest is, you define several animations each of which having a different percentage value on the android:pivotY attribute (and several themes referencing those animations). Then, when the user taps the row, calculate the Y position in percentage of the row on the screen. Knowing the position in percentage, assign a theme to your dialog that has the appropriate android:pivotY value.
It is not a perfect solution but could do the trick for you. If you don't like the result, then I would suggest forgetting the DialogFragment and animating a simple View growing from the exact center of the row.
Check it out this code, it works for me
// Slide up animation
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="#android:integer/config_mediumAnimTime"
android:fromYDelta="100%"
android:interpolator="#android:anim/accelerate_interpolator"
android:toXDelta="0" />
</set>
// Slide dowm animation
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="#android:integer/config_mediumAnimTime"
android:fromYDelta="0%p"
android:interpolator="#android:anim/accelerate_interpolator"
android:toYDelta="100%p" />
</set>
// Style
<style name="DialogAnimation">
<item name="android:windowEnterAnimation">#anim/slide_up</item>
<item name="android:windowExitAnimation">#anim/slide_down</item>
</style>
// Inside Dialog Fragment
#Override
public void onActivityCreated(Bundle arg0) {
super.onActivityCreated(arg0);
getDialog().getWindow()
.getAttributes().windowAnimations = R.style.DialogAnimation;
}
DialogFragment has a public getTheme() method that you can over ride for this exact reason. This solution uses less lines of code:
public class MyCustomDialogFragment extends DialogFragment{
...
#Override
public int getTheme() {
return R.style.MyThemeWithCustomAnimation;
}
}
To get a full-screen dialog with animation, write the following ...
Styles:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="actionModeBackground">?attr/colorPrimary</item>
<item name="windowActionModeOverlay">true</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.NoActionBar.FullScreenDialog">
<item name="android:windowAnimationStyle">#style/Animation.WindowSlideUpDown</item>
</style>
<style name="Animation.WindowSlideUpDown" parent="#android:style/Animation.Activity">
<item name="android:windowEnterAnimation">#anim/slide_up</item>
<item name="android:windowExitAnimation">#anim/slide_down</item>
</style>
res/anim/slide_up.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="#android:interpolator/accelerate_quad">
<translate
android:duration="#android:integer/config_shortAnimTime"
android:fromYDelta="100%"
android:toYDelta="0%"/>
</set>
res/anim/slide_down.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="#android:interpolator/accelerate_quad">
<translate
android:duration="#android:integer/config_shortAnimTime"
android:fromYDelta="0%"
android:toYDelta="100%"/>
</set>
Java code:
public class MyDialog extends DialogFragment {
#Override
public int getTheme() {
return R.style.AppTheme_NoActionBar_FullScreenDialog;
}
}
private void showDialog() {
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
Fragment previous = getSupportFragmentManager().findFragmentByTag(MyDialog.class.getName());
if (previous != null) {
fragmentTransaction.remove(previous);
}
fragmentTransaction.addToBackStack(null);
MyDialog dialog = new MyDialog();
dialog.show(fragmentTransaction, MyDialog.class.getName());
}
In DialogFragment, custom animation is called onCreateDialog. 'DialogAnimation' is custom animation style in previous answer.
public Dialog onCreateDialog(Bundle savedInstanceState)
{
final Dialog dialog = super.onCreateDialog(savedInstanceState);
dialog.getWindow().getAttributes().windowAnimations = R.style.DialogAnimation;
return dialog;
}
Use decor view inside onStart in your dialog fragment
#Override
public void onStart() {
super.onStart();
final View decorView = getDialog()
.getWindow()
.getDecorView();
decorView.animate().translationY(-100)
.setStartDelay(300)
.setDuration(300)
.start();
}
If you want to work over APIs you have to do inside your DialogFragemnt->onStart and not inside onCreateDialog
#Override
public void onStart()
{
if (getDialog() == null)
{
return;
}
getDialog().getWindow().setWindowAnimations(
R.style.DlgAnimation);
super.onStart();
}
Note: This is just a complement to other answers.
No matter which the solutions you pick you might have the same problem as me.
I need to UNINSTALL the game from my development device before installing the new version for the animation changes to take effect.
I am not sure why but I guess it has to do with the optimized deployment on Android studio not recognizing the changes.
Have you looked at Android Developers Training on Zooming a View? Might be a good starting point.
You probably want to create a custom class extending DialogFragment to get this working.
Also, take a look at Jake Whartons NineOldAndroids for Honeycomb Animation API compatibility all the way back to API Level 1.
Add this code on values anim
<scale
android:duration="#android:integer/config_longAnimTime"
android:fromXScale="0.2"
android:fromYScale="0.2"
android:toXScale="1.0"
android:toYScale="1.0"
android:pivotX="50%"
android:pivotY="50%"/>
<alpha
android:fromAlpha="0.1"
android:toAlpha="1.0"
android:duration="#android:integer/config_longAnimTime"
android:interpolator="#android:anim/accelerate_decelerate_interpolator"/>
call on styles.xml
<style name="DialogScale">
<item name="android:windowEnterAnimation">#anim/scale_in</item>
<item name="android:windowExitAnimation">#anim/scale_out</item>
</style>
On java code: set Onclick
public void onClick(View v) {
fab_onclick(R.style.DialogScale, "Scale" ,(Activity) context,getWindow().getDecorView().getRootView());
// Dialogs.fab_onclick(R.style.DialogScale, "Scale");
}
setup on method:
alertDialog.getWindow().getAttributes().windowAnimations = type;