Android Studio Loading Animation Between Activities

Progress Dialog is dialog showing a progress indicator and an optional text message or view.

The methods of Progress Dialog being used in this tutorial are:

ProgressDialog.setTitle() – Used to set title of dialog box

ProgressDialog.setMessage() – Used to set dialog message being displayed

ProgressDialog.setProgressStyle() – Choose the style of indicator

ProgressDialog.dismiss() – Dismiss the progress dialog

For this example there is Two Main Activities. One is called MainActivity.java and the other MainActivity2.java

The function of this app is the user click on the ‘next’ button and the Main Activity is switched to the second activity. During the switch a Progress Dialog is displayed to signal the change in activates.

MainActivity.java
public class MainActivity extends AppCompatActivity {
    Button  next;
    ProgressDialog progress;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        progress = new ProgressDialog(MainActivity.this);
        next = (Button) findViewById(R.id.btn_main);

        next.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                switch (v.getId()) {
                    case R.id.btn_main:
                        progress.setTitle("Progress"); // setting title
                        progress.setMessage("Please Wait Loading..."); // creating message
                        progress.setProgressStyle(ProgressDialog.STYLE_SPINNER); // style of indicator
                        progress.setIndeterminate(true);
                        progress.show();
                        new Thread() {

                            public void run() {
                                try {
                                    Thread.sleep(2000);
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }
                                Intent i = new Intent(MainActivity.this, MainActivity2.class); // Switching to Second Activity 
                                startActivity(i);
                                progress.dismiss();
                            }

                        }.start();
                        break;
                }

            }
        });
    }
}
activity_main.xml
<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/btn_main"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="159dp"
        android:layout_marginLeft="159dp"
        android:layout_marginTop="364dp"
        android:layout_marginEnd="159dp"
        android:layout_marginRight="159dp"
        android:text="Next"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

Check out more tutorials HERE!

Android Studio Tutorial SeekBar

SeekBar is an extension of ProgressBar that adds a draggable thumb. The user can touch thumb and drag left or right to set the current progress level or various other task. In this example, the seekbar will be used to display a percentage. As the user moves the SeekBar left to right the percentage value … Read more Android Studio Tutorial SeekBar

Python chr()

The chr() function returns a character that represents the specified unicode. Syntax chr(i) The chr() method takes only one integer as argument. The range may vary from 0 to 1,1141,111(0x10FFFF in base 16). The chr() method returns a character whose unicode point is num, an integer. If an integer is passed that is outside the range then … Read more Python chr()

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s