user7431071
user7431071

Reputation:

How to stop the Visibility of the ProgressBar inside LinearLayout

I have an activity which have 5-6 fragments in it and i am trying to show a progress bar whenever a fragment loads its data from REST api. I am able to show the progress bar but not able to stop visibility of it. whenever i am trying to stop the visibility of it, Progress Bar doesn't come up at all. Here is my Activity.java

public class MainActivity extends AppCompatActivity {


    private Toolbar   toolbar;
    private TabLayout tabLayout;
    private ViewPager viewPager;
    public LinearLayout linearLayout;

       @Override
       protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);



        toolbar = (Toolbar) findViewById(R.id.toolbar);
        ImageView imageTitle = (ImageView) toolbar.findViewById(R.id.appbar_image);
           setSupportActionBar(toolbar);
           getSupportActionBar().setDisplayShowTitleEnabled(false);
           getSupportActionBar().setDisplayHomeAsUpEnabled(false);
           linearLayout = (LinearLayout)findViewById(R.id.linlaHeaderProgress);
           linearLayout.setVisibility(View.VISIBLE);



        viewPager = (ViewPager) findViewById(R.id.viewpager);
        setupViewPager(viewPager);

        tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(viewPager);


       }


private void setupViewPager(ViewPager viewPager) {
        ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
        adapter.addFragment(new HomeFragment(), "Home News");
        adapter.addFragment(new BusinessFragment(),"Business News");
        adapter.addFragment(new PoliticsFragment(),"Politics News");
        adapter.addFragment(new TechnologyFragment(),"Technology News");
        adapter.addFragment(new SportsFragment(),"Sports News");
        adapter.addFragment(new EntertainmentFragment(),"Entertainment News");
        adapter.addFragment(new HealthFragment(),"Health News");
        adapter.addFragment(new WorldFragment(),"World News");
        adapter.addFragment(new MoreFragment(),"More News");


        viewPager.setAdapter(adapter);



}

private void stopBar() {

    linearLayout.setVisibility(View.GONE);
}


class ViewPagerAdapter extends FragmentPagerAdapter {
     private final List<Fragment> mFragmentList = new ArrayList<>();
    private final List<String> mFragmentTitleList = new ArrayList<>();

public ViewPagerAdapter(FragmentManager manager) {
    super(manager);
}

@Override
public Fragment getItem(int position) {
    return mFragmentList.get(position);
}

@Override
public int getCount() {
    return mFragmentList.size();
}

public void addFragment(Fragment fragment, String title) {
    mFragmentList.add(fragment);
    mFragmentTitleList.add(title);


}

     @Override
    public CharSequence getPageTitle(int position) {
    return mFragmentTitleList.get(position);
   }
 }
}  

Here is my activity.xml

<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:layout_height="match_parent">

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

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/colorPrimary"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light">


            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/appbar_image"
                android:foregroundGravity="center"
                android:scaleType="fitCenter"
                android:src="@drawable/splash"


                />



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

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabMode="scrollable"/>
    </android.support.design.widget.AppBarLayout>


    <LinearLayout
        android:id="@+id/linlaHeaderProgress"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center"
        android:orientation="vertical"
        android:visibility="gone">

        <ProgressBar
            android:id="@+id/pbHeaderProgress"
            style="@style/Widget.AppCompat.ProgressBar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:theme="@style/CircularProgress">
        </ProgressBar>

    </LinearLayout>



    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />



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

This is my fragment.java

    public class HomeFragment extends android.support.v4.app.Fragment {

    private RecyclerView recyclerView;
    private ArrayList<HomeNews> data;
    private HomeNewsAdapter adapter;
    public HomeFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View rootView = inflater.inflate(R.layout.fragment_home, container, false);

        recyclerView = (RecyclerView)rootView.findViewById(R.id.rv_recycler_view);
        recyclerView.setHasFixedSize(true);
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext());
        recyclerView.setLayoutManager(layoutManager);


        loadJSON();


        return rootView;

    }



    private void loadJSON() {

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://www.vaonlinenews.com/news_api/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        RequestInterface request = retrofit.create(RequestInterface.class);
        Call<HomeResponse> call = request.getHomeJSON();

        call.enqueue(new Callback<HomeResponse>() {
            @Override
            public void onResponse(Call<HomeResponse> call, Response<HomeResponse> response) {

                HomeResponse homeResponse = response.body();
                data = new ArrayList<>(Arrays.asList(homeResponse.getArticles()));
                adapter = new HomeNewsAdapter(getContext(),data);
                recyclerView.setAdapter(adapter);



            }





            @Override
            public void onFailure(Call<HomeResponse> call, Throwable t) {
                Log.d("Error",t.getMessage());
                Error_Dialog alert = new Error_Dialog();
                alert.showDialog(getActivity()," Please Check Your Internet Connection");

            }
        });

    }




}

Please suggest me where i should place the code to stop the visibility of progress bar after the fragments gets loaded.

Upvotes: 0

Views: 768

Answers (3)

Rai_Gaurav
Rai_Gaurav

Reputation: 158

In the your HomeFragment, when you call

     loadJSON()

on response or failure you call your activity's stopBar() method from where you hide progress bar

make you stopBar() method public, so that it can be accessed from Activity

public void stopBar() {
linearLayout.setVisibility(View.GONE);
}

In your HomeFragment's onCreate you can get reference to your activity like below, then you can call the stopBar() method

MainActivity activity = ((MainActivity)getActivity());
activity.stopBar();

it will look like this,

private void loadJSON() {

    Retrofit retrofit = new Retrofit.Builder()
          //
          // here goes your retrofit code             
          //
    Call<HomeResponse> call = request.getHomeJSON();

    call.enqueue(new Callback<HomeResponse>() {
        @Override
        public void onResponse(Call<HomeResponse> call, Response<HomeResponse> response) {
            //
            // here goes your code             
            //
             activity.stopBar();


        }





        @Override
        public void onFailure(Call<HomeResponse> call, Throwable t) {
               //
               // here goes your code             
               //

              activity.stopBar();

            Error_Dialog alert = new Error_Dialog();
            alert.showDialog(getActivity()," Please Check Your Internet Connection");

        }
    });

}

Upvotes: 0

karthik
karthik

Reputation: 357

main activity method to stop progress bar

public void stopProgressBar(){
if (progresslayout.getVisibility() == View.VISIBLE) 
{
 progresslayout.setVisibility(View.GONE);
}
}

call the above method in the fragment after getting the data ex: in onattach get mainactivity context like below

 @Override
 public void onAttach(Context context) {
    super.onAttach(context);

    mainActivity = (MainActivity) context;
    this.context = context;
}

calling stop progressbar method in fragment

mainActivity.stopProgressBar();

Upvotes: 1

Deedar Ali Brohi
Deedar Ali Brohi

Reputation: 265

Use

linearLayout.setVisibility(LinearLayout.GONE);

Upvotes: 0

Related Questions