Add ProgressBar and ViewGroup through viewgroup of Activity

Source: Internet
Author: User

Add ProgressBar and ViewGroup through viewgroup of Activity

ProgressBar is a common component. we can implement a progressBar in multiple ways. One way is to place a bar through an Activity window, as we all know in android that the link is Activity-> Window-> View [ViewGroup], we can load a progressBar through the window of the current Activity, when we use this Activity as a base class, then its subclass and the Fragment attached above can easily display a progressbar. [We can make a single example of this custom progressbar.]

The specific implementation process is as follows:

1: progressbar layout to be displayed

A linearLayout contains a TextView to be prompted and a progress bar.

<?xml version="1.0" encoding="UTF-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:background="#00000000"    android:gravity="center" >    <LinearLayout        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:background="@color/sdk_semitransparent"        android:gravity="center"        android:padding="15dp" >        <ProgressBar            style="@style/sdk_load_progress_style"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:indeterminateDrawable="@drawable/sdk_load_progressbar" />        <TextView            android:id="@+id/progress_txt"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_marginLeft="5dp"            android:text="@string/sdk_loading" >        </TextView>    </LinearLayout></LinearLayout>
2: Implement custom progressbar

The annotations in the Window are clear, that is, a Window is obtained through the Activity, and a layout that we need to display is added.

Public class ProgressView {// progressbar layout private View mDialogView; // ViewGroup [mFLayout] private ViewGroup mFragmentView obtained from the current Activity; // create a layout, place Our progressbar to set some actual properties private LinearLayout mFLayout; // The singleton object private static ProgressView mInstance; // The text View private TextView mProgressText to be prompted; // set whether to cancel private boolean mCancelable = true; private ProgressView () {} public static synchronized ProgressView getInstance () {if (mInstance = null) {mInstance = new ProgressView ();} return mInstance;} public void showProgressView (Activity act, int loadingTextId, boolean cancelable) {showProgressView (act, ResUtil. getString (loadingTextId), cancelable);} public void showProgressView (Activity act, int loadingTextId) {showProgressView (act, ResUtil. getString (loadingTextId), true);} public void showProgressView (Activity act, String tipStr) {showProgressView (act, tipStr, true);} public void showDilaogProgressView (Activity act, String tipStr) {dialogProgressView (act, tipStr, true );} /***** <one-sentence function description> <br> * <function description> ** @ param act content * @ param loading_text progress: String */public void showProgressView (Activity act, string loading_text, boolean cancelable) {mDialogView = act. getLayoutInflater (). inflate (R. layout. sdk_dialog_progress, null); mProgressText = (TextView) mDialogView. findViewById (R. id. progress_txt );
// This layout_base is the layout of mFragmentView = (ViewGroup) act to be displayed in the Activity. getWindow (). findViewById (R. id. layout_base); mCancelable = cancelable; mFLayout = new LinearLayout (act); if (! TextUtils. isEmpty (loading_text) {mProgressText. setText (loading_text) ;}init ();} /***** <one-sentence function description> <br> * <function description> ** @ param act content * @ param loading_text progress: String */public void dialogProgressView (Activity act, string loading_text, boolean cancelable) {mDialogView = act. getLayoutInflater (). inflate (R. layout. sdk_dialog_progress, null); mProgressText = (TextView) mDialogView. findVie WById (R. id. progress_txt); mFragmentView = (ViewGroup) act. getWindow (). findViewById (R. id. skd2_base_content); mCancelable = cancelable; mFLayout = new LinearLayout (act); if (! TextUtils. isEmpty (loading_text) {mProgressText. setText (loading_text);} init ();} private void init () {RelativeLayout. layoutParams params = new RelativeLayout. layoutParams (LinearLayout. layoutParams. MATCH_PARENT, LinearLayout. layoutParams. MATCH_PARENT); params. addRule (RelativeLayout. CENTER_IN_PARENT); mFLayout. setLayoutParams (params); mFLayout. setBackgroundColor (ResUtil. getColor (R. color. sdk_tran Sparent); LinearLayout. layoutParams layoutParams = new LinearLayout. layoutParams (LinearLayout. layoutParams. MATCH_PARENT, LinearLayout. layoutParams. WRAP_CONTENT); layoutParams. gravity = Gravity. CENTER; layoutParams. setMargins (20, 0, 20, 0); mFLayout. addView (mDialogView, layoutParams); addDialogContentView ();} private void addDialogContentView () {if (mFragmentView! = Null) {mFragmentView. addView (mFLayout, new LinearLayout. layoutParams (LinearLayout. layoutParams. MATCH_PARENT, LinearLayout. layoutParams. MATCH_PARENT);} mFLayout. setOnClickListener (new OnClickListener () {@ Override public void onClick (View v) {// when the progress pop-up intercepts the page Click Event});} public void dismissProgress () {if (mFragmentView! = Null & mFLayout! = Null) {mFragmentView. removeView (mFLayout);} reset ();} public boolean onKeyDown (int keyCode, KeyEvent event) {if (! MCancelable) {reset (); return true;} if (keyCode = KeyEvent. KEYCODE_BACK) {// Monitor/intercept/shield the return key if (mFragmentView = null | mDialogView = null | mFLayout = null) {return true;} dismissProgress (); return false;} return true;} private void reset () {mDialogView = null; mFragmentView = null; mFLayout = null ;}}

3: Usage

// Display ProgressView. getInstance (). showProgressView (avtivityA, ""); // cancel ProgressView. getInstance (). dismissProgress ();


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.