Reputation: 7115
I want to implement a progessDialog when my class is called. I know how to implement a progressDialog for a fragment class that uses AsyncTask. But here i have a fragment class that doesn't use asyncTask.
What this fragment does is showing some news details when it is called. I want to show a progressDialog while the news is loading and stop the progressDialog when the news is loaded and ready to display.How can i do this? I'm a newbie so please help me
My fragment class
package com.fortuna.cinemalk;
public class NewsDetailFragment extends Fragment {
private ViewPager view1;
private ViewPageAdapter adapter;
private Activity activity;
private CommonVariable commonVariable;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.newsdetail_fragment, container,
false);
activity = this.getActivity();
commonVariable = (CommonVariable) activity.getApplication();
view1 = (ViewPager) view.findViewById(R.id.listviewpager);
adapter = new ViewPageAdapter(commonVariable.getNewsDescription(),
Element.NEWS_DETAIL.getType(), activity);
view1.setAdapter(adapter);
return view;
}
}
Upvotes: 2
Views: 672
Reputation: 10640
You need like this :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ProgressBar
android:id="@+id/progressBar1"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:indeterminate="true" />
</RelativeLayout>
Just hide this progressbar once you load the data like
progressBarObj.setVisibility(false);
Upvotes: 1
Reputation: 42
You can show ProgressDialog from your AsyncTask or Service or you can initialize ProgressDialog in fragment like static and call dialog from your AsyncTask or Service, but you need check progressDialog!=null
(because i will need dismiss progress when news is loaded)
Upvotes: 0
Reputation: 1701
The best option for me is creating a custom handler which modifies the proggress component:
public final class ProgressBarComponentViewHandler extends Handler {
private ProgressBar progressLayout;
private Activity mActivity;
public LoadingComponentViewHandler(Activity activity, ProgressBar progressLayout) {
this.progressLayout = progressLayout;
this.mActivity=activity
}
public void update(int status){
Message m = new Message();
Bundle bundle = new Bundle();
bundle.putInt("status", status);
m.setData(bundle);
super.sendMessage(m);
}
public void handleMessage(Message msg) {
Bundle b = msg.getData();
Integer status = b.getInt("status", 0);
progressLayout.setProgress(status);
}
}
And in your fragment:
loadingComponentView = (ProgressBar) yourRootView.findViewById( R.id.progress_bar_component_view );
progressBarDialogHandler = new ProgressBarViewHandler(getActivity(), loadingComponentView);
At the end, to update your view:
progressBarDialogHandler.update(80);//To set 80% of progressbar.
Upvotes: 1