Last active
October 25, 2017 16:06
-
-
Save alexfu/ccde3c83c050dc43dec0 to your computer and use it in GitHub Desktop.
A simple FrameLayout that hides/shows an indeterminate ProgressBar over top of your UI
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* The MIT License (MIT) | |
* | |
* Copyright (c) 2015 Alex Fu | |
* | |
* Permission is hereby granted, free of charge, to any person obtaining a copy | |
* of this software and associated documentation files (the "Software"), to deal | |
* in the Software without restriction, including without limitation the rights | |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
* copies of the Software, and to permit persons to whom the Software is | |
* furnished to do so, subject to the following conditions: | |
* | |
* The above copyright notice and this permission notice shall be included in all | |
* copies or substantial portions of the Software. | |
* | |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
* SOFTWARE. | |
*/ | |
import android.content.Context; | |
import android.util.AttributeSet; | |
import android.view.Gravity; | |
import android.widget.FrameLayout; | |
import android.widget.ProgressBar; | |
public class ProgressLayout extends FrameLayout { | |
private ProgressBar progressBar; | |
private View contentView; | |
private boolean loading; | |
public ProgressLayout(Context context) { | |
super(context); | |
} | |
public ProgressLayout(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
} | |
public ProgressLayout(Context context, AttributeSet attrs, int defStyleAttr) { | |
super(context, attrs, defStyleAttr); | |
} | |
public void setContentView(View contentView) { | |
this.contentView = contentView; | |
} | |
public void setLoading(boolean isLoading) { | |
if (loading == isLoading) return; | |
loading = isLoading; | |
invalidateLoadingView(); | |
} | |
private void invalidateLoadingView() { | |
if (loading) { | |
showLoadingView(); | |
} else { | |
hideLoadingView(); | |
} | |
} | |
private void showLoadingView() { | |
if (progressBar == null) { | |
progressBar = new ProgressBar(getContext()); | |
LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); | |
lp.gravity = Gravity.CENTER; | |
progressBar.setLayoutParams(lp); | |
} | |
addView(progressBar); | |
showContent(false); | |
} | |
private void hideLoadingView() { | |
showContent(true); | |
if (progressBar != null) { | |
removeView(progressBar); | |
} | |
} | |
private void showContent(boolean show) { | |
if (contentView != null) { | |
contentView.setVisibility(show ? View.VISIBLE : View.GONE); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment