Android: Show a Loading Animation Popup

2/28/2014 8:42:22 PM

Some Android apps have to fetch/post data to a server and this can often take some time. Or you might have some calculations that take some time and you want to inform the user or restrict access to the UI. The following code will place a loading animation on your activity's layout. You will have to determine when to show/hide the animation.

<RelativeLayout
	android:id="@+id/main_layoutPageLoading"
	android:layout_width="match_parent"        
	android:layout_height="match_parent"    
	android:background="#DD111111"
	android:gravity="center"   
	android:visibility="gone" >

	<ProgressBar style="@style/progressBar_Indicator"
		android:layout_width="wrap_content"        
		android:layout_height="wrap_content"  
		android:indeterminate="true"      
		 />
</RelativeLayout>


//show
public void ShowLoadingAnimation()
{
	RelativeLayout pageLoading = (RelativeLayout) findViewById(R.id.main_layoutPageLoading);
	pageLoading.setVisibility(View.VISIBLE);
}


//hide
public void HideLoadingAnimation()
{
	RelativeLayout pageLoading = (RelativeLayout) findViewById(R.id.main_layoutPageLoading);
	pageLoading.setVisibility(View.GONE);
}