Reputation: 1051
Hello I'm adding animation left to right and right to left when opening and finishing activities but when I put the overridePendingIntent
it shows a black screen...
This is my left_to_right.xml animation
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate
android:duration="500"
android:fromXDelta="-100%"
android:fromYDelta="0%"
android:toXDelta="0%"
android:toYDelta="0%" />
</set>
This is the right_to_left.xml animation
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate
android:duration="500"
android:fromXDelta="0%"
android:fromYDelta="0%"
android:toXDelta="100%"
android:toYDelta="0%" />
</set>
I'm starting the Activity like this :
startActivity(Intent(this, SignInActivity::class.java))
overridePendingTransition(R.anim.left_to_right, R.anim.right_to_left)
and finishing it like this :
finish()
overridePendingTransition(R.anim.right_to_left, R.anim.left_to_right)
But when doing it is showing black screen...
This is my theme
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.MyTheme" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/purple_500</item>
<item name="colorPrimaryVariant">@color/purple_700</item>
<item name="colorOnPrimary">@color/white</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">@color/teal_200</item>
<item name="colorSecondaryVariant">@color/teal_700</item>
<item name="colorOnSecondary">@color/black</item>
<!-- Status bar color. -->
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. -->
<item name="android:windowDisablePreview">true</item>
</style>
</resources>
And on my Activities I'm using
android:theme="@style/Theme.MaterialComponents.Light.NoActionBar"
What I'm missing?
Upvotes: 0
Views: 492
Reputation: 3683
FIRST ACTIVITY:
I rebuild the situation with a MainActivity
that inflates the TargetActivity
(second Activity
) using a Button
:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button_in = findViewById(R.id.button_in);
button_in.setOnClickListener(v -> {
Intent intent = new Intent(this, TargetActivity.class);
startActivity(intent);
overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
});
}
}
The activity_main.xml
contains the first Button
:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="MainActivity">
<Button
android:id="@+id/button_in"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="20sp"
android:backgroundTint="#2962FF"
android:text="To TargetActivity" />
</RelativeLayout>
SECOND ACTIVITY:
The second Activity
is the TargetActivity
that finish()
with an animation like this:
public class TargetActivity extends Activity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_target);
Button button_out = findViewById(R.id.button_out);
button_out.setOnClickListener(v -> {
this.finish();
overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right);
});
}
}
And also another Layout
with the 2nd Button
called activity_target.xml
:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFAB00"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".TargetActivity">
<Button
android:id="@+id/button_out"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="20sp"
android:textColor="#ffffff"
android:backgroundTint="#2962FF"
android:text="To MainActivity" />
</RelativeLayout>
4 ANIMATIONS:
All you need is 4 animations in your anim
folder. 2 for left to right and also vice versa.
1.) slide_in_left.xml
:
<?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:fromXDelta="-100%p"
android:toXDelta="0" />
</set>
2.) slide_out_left.xml
:
<?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:fromXDelta="0"
android:toXDelta="-100%p" />
</set>
3.) slide_in_right.xml
:
<?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:fromXDelta="100%p"
android:toXDelta="0" />
</set>
4.) slide_out:right.xml
:
<?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:fromXDelta="0"
android:toXDelta="100%p" />
</set>
Finally don't forget to add the TargetActivity
in your AndroidManifest.xml
:
<activity android:name=".TargetActivity"/>
Upvotes: 1