Search code examples
androidandroid-studioandroid-activitystart-activity

Screen/view shifting between activities


Complete newbie here...when I call a second activity from a first activity the screen view kind of jumps/shifts. The same happens when I hit the back button to return to the first activity. The files were generated by Android Studio and I've only added a button with onClick to call the second activity, and modified the actionbar text. Hope the screen recording illustrates. Any ideas what might be causing it?

screen recording converted to animated gif

enter image description here

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/enter_button"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="32dp"
        android:layout_marginLeft="32dp"
        android:layout_marginTop="120dp"
        android:layout_marginEnd="32dp"
        android:layout_marginRight="32dp"
        android:text="Button"
        android:textSize="18sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

activity_main2.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Main2Activity">

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java

package com.philburfitt.test;

import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // change text on actionbar
        getSupportActionBar().setTitle("Test - First Activity");

        // call Main2Activity on button press
        Button enterButton = findViewById(R.id.enter_button);
        enterButton.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick( View v ) {
                startActivity(new Intent(MainActivity.this, Main2Activity.class));
            }
        } );
    }
}

Main2Activity.java

package com.philburfitt.test;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;

public class Main2Activity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        getSupportActionBar().setTitle("Test - Second Activity");
    }
}

Solution

  • What you have seen is the default transition ,however you can use custom transitions . I will share a sample .

    This is how i call next activity B from activity A.

    Intent i = new Intent(getApplicationContext(), Dashboard.class);
           startActivity(i);
           finish();
           overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
    

    Now Add this animation files in res>anim

    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>
    

    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>