11th October, 2022
Hello Android Developer!
Sometimes you need to show full screen loading dialog to prevent the user from input. In this part you will see a way to Display Full Screen Loading Dialog.
Step 1: Create Project
Create a new project in Android Studio from File ⇒ New Project and select Empty Activity from the templates.
Step 2: Create Loading Layout
Create a layout file in the layout resource directory.
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/my_loading_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#835C5C5C" android:clickable="true" android:focusable="true" android:orientation="vertical" android:gravity="center"> <ProgressBar android:layout_width="wrap_content" android:layout_height="wrap_content"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Loading..." android:textColor="#000000" android:layout_marginTop="5dp"/> </LinearLayout>
Step 3: Modify Activity
Now open the activity layout where you want the full screen loading dialog to appear. Include the loading layout in it.
<?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"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="www.CodestringZ.com" /> <!--Other Views--> <!--Full Screen Progress Layout--> <include android:id="@+id/my_loading_layout" layout="@layout/my_loading_layout" /> </RelativeLayout>
Next open the Activity and modify it as follows:
package com.codestringz.mytestapp; import android.os.Bundle; import android.view.View; import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity { private View mLoading; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); setUIRef(); //Display Loading showLoading(); } private void setUIRef() { //Create a Instance of the Loading Layout mLoading = findViewById(R.id.my_loading_layout); } private void showLoading() { /*Call this function when you want progress dialog to appear*/ if (mLoading != null) { mLoading.setVisibility(View.VISIBLE); } } private void hideLoading() { /*Call this function when you want progress dialog to disappear*/ if (mLoading != null) { mLoading.setVisibility(View.GONE); } } }
That’s it. Happy coding!
Output