Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Common Progress bar in every activity Android

I have modify the solution. I am able to get the progress bar but the progress bar never gets hide

Here is class to create a progress bar with relative layout

public class ProgressBarHandler {
    private ProgressBar mProgressBar;
    private Context mContext;
    private View _baseView;
    private View _hideView;
    public ProgressBarHandler(Context context,View baseView, View hideView) {
        mContext = context;
        _baseView = baseView;
        _hideView = hideView;

        ViewGroup layout = (ViewGroup) _baseView;//((Activity) context).findViewById(android.R.id.content).getRootView();

        mProgressBar = new ProgressBar(context, null, android.R.attr.progressBarStyleLarge);
        mProgressBar.setIndeterminate(true);

        RelativeLayout.LayoutParams params = new
                RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);

        RelativeLayout rl = new RelativeLayout(context);

        rl.setGravity(Gravity.CENTER);
        rl.addView(mProgressBar);

        layout.addView(rl, params);

        hide();
    }

    public void show() {
        mProgressBar.setVisibility(View.VISIBLE);
        _hideView.setVisibility(View.INVISIBLE);
    }

    public void hide() {
        mProgressBar.setVisibility(View.INVISIBLE);
        _hideView.setVisibility(View.VISIBLE);
    }
}

This is my xml file

<FrameLayout xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:descendantFocusability="beforeDescendants"
    android:focusableInTouchMode="true"
    android:orientation="vertical"
    android:id="@+id/profile_activity">


<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
    android:id="@+id/layout_to_hide"
android:layout_height="match_parent"
android:fitsSystemWindows="true">

<android.support.design.widget.AppBarLayout
    android:id="@+id/app_bar_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fitsSystemWindows="true"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collapsing_toolbar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        app:expandedTitleTextAppearance="@android:color/transparent"
        app:contentScrim="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|exitUntilCollapsed">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@mipmap/wallpaper"
                android:orientation="vertical">

                <com.deerwalk.androidcommon.RoundedImageView
                    android:id="@+id/profile_image"
                    android:layout_width="100dp"
                    app:civ_border_color="#EEEEEE"
                    app:civ_border_width="4dp"
                    app:civ_shadow="true"
                    app:civ_shadow_radius="10"
                    app:civ_shadow_color="#8BC34A"
                    android:layout_height="100dp"
                    android:layout_alignWithParentIfMissing="false"
                    android:layout_centerInParent="true"
                    android:layout_gravity="center"
                    android:layout_margin="20dp"
                    android:src="@drawable/avatar" />

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="10dp"
                    android:layout_below="@+id/profile_image"
                    android:orientation="vertical">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="match_parent"
                        android:id="@+id/txt_name"
                        android:layout_gravity="center_horizontal"
                        android:text=""
                        android:textColor="@color/white" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="match_parent"
                        android:id="@+id/txt_dob"
                        android:layout_gravity="center_horizontal"
                        android:text=""
                        android:layout_marginTop="5dp"
                        android:textColor="@color/white" />
                </LinearLayout>
            </RelativeLayout>



        </LinearLayout>
    </android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>


<android.support.v7.widget.RecyclerView
    android:id="@+id/recyclerView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">


</android.support.v7.widget.RecyclerView>

</android.support.design.widget.CoordinatorLayout>
</FrameLayout>

Here is the activity class which call the progress bar

public class ProfileActivity extends AppCompatActivity {

    FrameLayout profile_root_layout;
    CoordinatorLayout layot_to_hide;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profile);
        profile_root_layout = (FrameLayout) findViewById(R.id.profile_activity);
        layot_to_hide = (CoordinatorLayout) findViewById(R.id.layout_to_hide);
         new userProfileTask().execute();
    }




    public class userProfileTask extends AsyncTask<Void, Void, Boolean> {
        @Override
        protected void onPreExecute() {
            new ProgressBarHandler(getBaseContext(),profile_root_layout,layot_to_hide).show();
            super.onPreExecute();
        }

        @Override
        protected Boolean doInBackground(Void... params) {
            try {
                Thread.sleep(2000);

            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            return null;
        }

        @Override
        protected void onPostExecute(Boolean aBoolean) {

            new ProgressBarHandler(getBaseContext(),profile_root_layout,layot_to_hide).hide();
        }
    }

}

The progress bar never gets hides.Where I am doing mistake.

like image 660
Ananda Prasad Jaisy Avatar asked Oct 18 '22 13:10

Ananda Prasad Jaisy


2 Answers

Make a separate class then make a static method with the parameter on it the parameter contain Activity ... call this method on ur activity and pass the Activity name in it

like image 105
Amit Ranjan Avatar answered Oct 21 '22 04:10

Amit Ranjan


Its very simple, you can do in multiple ways. 1) You can define base activity and write your code for progress dialog and extends all activity from BaseActivity used Progressdialog. 2) You can have custom separate class and used in all activities. etc

Here is good example you can modify your code like this example. ProgressHud

Or use this too.

like image 40
khurram Avatar answered Oct 21 '22 05:10

khurram