Object animator display based on width dimensions - android

Please guide how to set Objectanimator dimension dynamically from device width.
Fragment replace code with animation
Fragment f = ... //defined elsewhere
FragmentManager fm1 = getFragmentManager()
FragmentTransaction ft1 = fm1.beginTransaction();
ft1.setCustomAnimations(R.animator.slide_in_left,
R.animator.slide_out_right);
ft1.replace(R.id.content_layout, f)
.addToBackStack(null)
.commit();
slide_in_left.xml
<?xml version="1.0" encoding="utf-8"?>
<set>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
android:propertyName="x"
android:valueType="floatType"
android:valueFrom="-800"
android:valueTo="0"
android:duration="500" />
</set>
how I am supposed to set the "valueFrom", and "valueTo" value based on device width,Please guide me it is possible to achieve this method or suggest some other ideas.

Related

how to add animation in android to popBackStack?

i have a fragment i'm popping back into another fragment like this:
final FragmentManager fm = getParentFragmentManager();
fm.popBackStack(myFirstFragment.LOG_TAG, FragmentManager.POP_BACK_STACK_INCLUSIVE);
right now the fragment just disappeared, instead of going back with a transition
how do I add a customer animation for that popping back?
You can do so, by simply calling the .setCustomAnimations Method from the FragmentTransaction.
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out, android.R.animator.fade_in, android.R.animator.fade_out);
For custom animations create a fade_in.xml and fade_out.xml and call it instead of android.R.animatior.fade_in and android.R.animatior.fade_out
fade_in.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="#android:anim/linear_interpolator">
<alpha
android:duration="2000"
android:fromAlpha="0.1"
android:toAlpha="1.0">
</alpha>
</set>
fade_out.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/linear_interpolator">
<alpha
android:duration="2000"
android:fromAlpha="1.0"
android:toAlpha="0.1" >
</alpha>
</set>
For further information look here.
JAVA
Fragment fragment = new FragmentB();
getSupportFragmentManager().beginTransaction()
.setCustomAnimations(
R.anim.slide_in, // enter
R.anim.fade_out, // exit
R.anim.fade_in, // popEnter
R.anim.slide_out // popExit
)
.replace(R.id.fragment_container, fragment)
.addToBackStack(null)
.commit();
KOTLIN
val fragment = FragmentB()
supportFragmentManager.commit {
setCustomAnimations(
enter = R.anim.slide_in,
exit = R.anim.fade_out,
popEnter = R.anim.fade_in,
popExit = R.anim.slide_out
)
replace(R.id.fragment_container, fragment)
addToBackStack(null)
}
for more information read :
https://developer.android.com/training/basics/fragments/animate#kotlin

How to set slide out transition for fragment

I'm applying transition set for entering fragment and for exiting i want to apply slide out animation, how can i do that, here is my code:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
productDetailFragment.setSharedElementEnterTransition(new DetailsTransition());
productDetailFragment.setEnterTransition(new Fade());
productDetailFragment.setExitTransition(new Slide(Gravity.RIGHT));
}
((MainActivity)context).getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fragment, productDetailFragment)
.addSharedElement(viewHolder.iv, "kittenImage")
.addToBackStack(null)
.commit();
Please check below code for custom animation apply to fragment transitions:
getSupportFragmentManager()
.beginTransaction()
.setCustomAnimations( R.anim.slide_up, 0, 0, R.anim.slide_down)
.show( m_topFragment )
.commit();
slide_up.xml
<?xml version="1.0" encoding="utf-8"?>
<objectAnimator
xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/accelerate_decelerate_interpolator"
android:propertyName="translationY"
android:valueType="floatType"
android:valueFrom="1280"
android:valueTo="0"
android:duration="#android:integer/config_mediumAnimTime"/>
slide_down.xml
<?xml version="1.0" encoding="utf-8"?>
<objectAnimator
xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/accelerate_decelerate_interpolator"
android:propertyName="translationY"
android:valueType="floatType"
android:valueFrom="0"
android:valueTo="1280"
android:duration="#android:integer/config_mediumAnimTime"/>
Above i am showing you slide_up and down transition but you can use left-right and any custom objectAnimator to show animations into your fragment transition time and hope it helps you.
try something like this.! may this work fine for you.!
Fragment Transaction Example
Using Slide Example
FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
Fade exitFade = new Fade();
exitFade.setDuration(FADE_DEFAULT_TIME);
previousFragment.setExitTransition(exitFade);
or
Transition slideTransition = new Slide(Gravity.BOTTOM);
Transition fadeTransition = new Fade();
TransitionSet set = new TransitionSet();
set.addTransition(slideTransition);
set.addTransition(fadeTransition);
Call Fragment :
FragmentManager fragmentManager = getSupportFragmentManager();
boolean fragmentPopped = fragmentManager.popBackStackImmediate("OtpFragment", 0);
Fragment fragment = null;
Bundle b = new Bundle();
if (!fragmentPopped && fragmentManager.findFragmentByTag("OtpFragment") == null) {
fragment = new OtpFragment();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.setCustomAnimations(R.anim.enter_from_right, R.anim.exit_from_left);
transaction.replace(R.id.frame_container, fragment, "OtpFragment")
.addToBackStack("OtpFragment")
.commit();
}
Create anim directory inside res folder :
now create two separate files for enter/exit animation : i.e. R.anim.enter_from_right and R.anim.exit_from_left
Enter Slide Animation :
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate
android:fromXDelta="100%" android:toXDelta="0%"
android:fromYDelta="0%" android:toYDelta="0%"
android:duration="300" />
</set>
Exit Side animation :
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate
android:fromXDelta="0%" android:toXDelta="-100%"
android:fromYDelta="0%" android:toYDelta="0%"
android:duration="300"/>
</set>

Difficulty reversing fragment animation with backstack

I am currently trying to implement a custom animation when changing fragments.
The custom animation only works one way and does not work when back is pressed. On back press the correct fragment is shown but with no animation.
My code for the fragment transaction:
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_right, R.anim.slide_in_right, R.anim.slide_out_left);
transaction.replace(R.id.frame_container, fragment, FragmentTag);
transaction.addToBackStack(null);
transaction.commit();
My code for the anim XML files:
//Slide in left
<?xml version="1.0" encoding="utf-8"?>
<set>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
android:propertyName="x"
android:valueType="floatType"
android:valueFrom="-1280"
android:valueTo="0"
android:duration="500"/>
</set>
//Slide out right
<?xml version="1.0" encoding="utf-8"?>
<set>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
android:propertyName="x"
android:valueType="floatType"
android:valueFrom="0"
android:valueTo="-1280"
android:duration="500"/>
</set>
//Slide in Right
<?xml version="1.0" encoding="utf-8"?>
<set>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
android:propertyName="x"
android:valueType="floatType"
android:valueFrom="-1280"
android:valueTo="0"
android:duration="500"/>
</set>
//Slide out Left
<?xml version="1.0" encoding="utf-8"?>
<set>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
android:propertyName="x"
android:valueType="floatType"
android:valueFrom="0"
android:valueTo="-1280"
android:duration="500"/>
</set>
The code for onBackPressed()
#Override
public void onBackPressed()
{
super.onBackPressed();
}
Any ideas where I'm going wrong? Thanks for any help
Use this code for onBackPressed:
if (getFragmentManager().getBackStackEntryCount() > 0) {
getFragmentManager().popBackStack();
} else {
super.onBackPressed();
}

Android fragment exit animation not firing

I have a fragment that slides in when it is added and slides off the screen to the right when it is removed. However, only the sliding in animation is working, and when I set the visibility of the fragment to gone, it vanishes without sliding. This is also my first post here so please let me know if I'm not doing something right, thanks!
Activity code
protected void onCreate(Bundle savedInstanceState) {
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.setCustomAnimations(R.animator.slide_in, R.animator.slide_out_right);
Bundle bundle = new Bundle();
MyFragment myFrag = new MyFragment();
myFrag.setArguments(bundle);
ft.add(R.id.challenger_preview_fragment_container, myFrag, "MyFragment");
ft.commit();
}
Fragment code
public void removeFragment() {
getView().setVisibility(View.GONE);
}
Animation XML
slide_in
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<objectAnimator
xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="500"
android:propertyName="x"
android:valueFrom="1000"
android:valueTo="0"
android:valueType="floatType" />
</set>
slide_out_right
<?xml version="1.0" encoding="utf-8" ?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:interpolator/accelerate_decelerate"
android:valueFrom="-1280"
android:valueTo="0"
android:valueType="floatType"
android:propertyName="X"
android:duration="2000" />
</set>
Try the below code, in below create an animation object to slide out right and set the duration. Later create a listener to know when the animation is ending so that you can hide your view.
The problem in your above code is that you are hiding the view before the animation is ending.
Animation animation = AnimationUtils.loadAnimation(getActivity(),R.animator.slide_out_right);
animation.setDuration(800);
animation.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationEnd(Animation animation) {
try {
getView().setVisibility(View.GONE);
} catch (Exception e) {
e.printStackTrace();
}
}
});
//Start the animation.
getView().startAnimation(animation);
You are setting visibility getView().setVisibility(View.GONE);
So the fragment cannot come back again its GONE
Try getView().setVisibility(View.INVISIBLE);
And when it comes back make it visible again as getView().setVisibility(View.VISIBLE);

How to avoid adding same fragment to stack

I need some help. em adding fragment to activity this way. problem is on each call of openFragment it create fragment and add. which is obvious. Question: what modification i do, so it can add fragment only once. on the next call with same fragment tag it will do nothing.
case: press button first time it add fragment and shows. i press again same button it response nothing.
public static void openFragment(Activity activity, Fragment fragment) {
FragmentManager fragmentManager = ((ActionBarActivity) activity)
.getSupportFragmentManager();
FragmentTransaction ftx = fragmentManager.beginTransaction();
ftx.addToBackStack(fragment.getClass().getSimpleName());
ftx.setCustomAnimations(R.anim.slide_in_right,
R.anim.slide_out_left, R.anim.slide_in_left,
R.anim.slide_out_right);
ftx.add(R.id.main_content, fragment);
ftx.commit();
}
Here's the solution, It will only allow you to add fragment once in stack otherwise it will pop-out the very same fragment from stack.
public static void openFragment(Activity activity, Fragment fragment) {
String fragmentTag = fragment.getClass().getName();
FragmentManager fragmentManager= ((ActionBarActivity) activity)
.getSupportFragmentManager();
boolean fragmentPopped = fragmentManager
.popBackStackImmediate(fragmentTag , 0);
if (!fragmentPopped && fragmentManager.findFragmentByTag(fragmentTag) == null) {
FragmentTransaction ftx = fragmentManager.beginTransaction();
ftx.addToBackStack(fragment.getClass().getSimpleName());
ftx.setCustomAnimations(R.anim.slide_in_right,
R.anim.slide_out_left, R.anim.slide_in_left,
R.anim.slide_out_right);
ftx.add(R.id.main_content, fragment);
ftx.commit();
}
}
slide_in_right
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="true">
<translate android:fromXDelta="100%"
android:toXDelta="0%" android:fromYDelta="0%"
android:toYDelta="0%" android:duration="200">
</translate>
</set>
slide_out_right
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate android:fromXDelta="0%" android:toXDelta="100%"
android:fromYDelta="0%" android:toYDelta="0%"
android:duration="200">
</translate>
</set>
slide_in_left
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="true">
<translate android:fromXDelta="-100%"
android:toXDelta="0%" android:fromYDelta="0%"
android:toYDelta="0%" android:duration="200">
</translate>
</set>
slide_out_left
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="true">
<translate android:fromXDelta="0%"
android:toXDelta="-100%" android:fromYDelta="0%"
android:toYDelta="0%" android:duration="200">
</translate>
</set>
And this is how you call this function:
openFragment(activity, new MyFragment());
Use FragmentTransaction.replace() instead of FragmentTransaction.add():
This is essentially the same as calling remove(Fragment) for all currently added fragments that were added with the same containerViewId and then add(int, Fragment, String) with the same arguments given here.
The first call to FragmentTransaction.replace() will simply add the fragment as there were no fragments to remove.
check if fragment is already added or not by using this method:
if(!Fragment.isAdded()){
//add fragment
}
Depending on your needs, there are multiple ways to approach this:
Disable or remove the button after it's clicked once.
As #hidro suggested, use replace() instead of add() when calling the FragmentTransaction.
Keep a List of string that will contain the class name of each fragment as it is added to the UI.
E.g.
List<String> fragments = new ArrayList<String>();
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
FragmentTransaction ftx = fragmentManager.beginTransaction();
ftx.add(R.id.main_content, fragment);
ftx.commit();
fragments.add(fragment.getClass().toString());
}
});
Before adding the fragment using FragmenTransaction.commit() check if its class exists in the list and if it does, don't add it.
Again, these 3 approaches work but which one will pick will depend on your app.
To add a fragment only once,
You need to check every time before adding the fragment to backstack,
that if previously it is already added or not. If it is added already,
then you should pop that entry and add another entry.
So, you can do this using :
boolean fragmentPopped = manager.popBackStackImmediate (backStateName, 0);
If fragmentPopped is true then there was a fragment added to backstack and itt is popped and you can add your fragment now to backstack. It is safe to call :
ftx.addToBackStack(fragment.getClass().getSimpleName());
If fragmentPopped is false then there was not any fragment added in backstack
Very nice information you can get : here
You can use show() method of fragment transaction
FragmentManager fragmentManager = ((ActionBarActivity) activity)
.getSupportFragmentManager();
FragmentTransaction ftx = fragmentManager.beginTransaction();
ftx.setCustomAnimations(R.anim.slide_in_right,
R.anim.slide_out_left, R.anim.slide_in_left,
R.anim.slide_out_right);
if(!fragment.isAdded())
{
//add fragment;
ftx.add(R.id.main_content, fragment).comit();
}
else
{
ftx.show(fragment).comit();
}
Updated

Categories

Resources