4

I'm new here. Let me know if I'm asking the question the wrong way or if I need to clarify this more.

I'm creating an Android app and I'm using fragments in my code (ie 4 tabs that can slide). I'm trying to add a bar to show a percentage of how much the user has used. I thought the best option would be a progress bar but I cant seem to get it to work.

Here's my code for the fragment I want to add the bar to;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class UserFragment extends Fragment {

    private ProgressDialog progress;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_calls, container, false);

    progress = new ProgressDialog(this);
    return rootView;
}

public void open(View view){
      progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
      progress.setIndeterminate(true);
      progress.show();

       final int totalProgressTime = 100;

       final Thread t = new Thread(){

       @Override
       public void run(){

          int jumpTime = 0;
          while(jumpTime < totalProgressTime){
             try {
                sleep(200);
                jumpTime += 5;
                progress.setProgress(jumpTime);
             } catch (InterruptedException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
             }

          }

       }
       };
       t.start();
}

} I followed a couple of tutorials online but I kept getting errors saying "the constructor progress dialog is undefined". I understand that I need some kind of context object but I'm lost on how to go about doing this. If any of ye could give me help on this, I'd greatly appreciate it. Thanks

BrianMac21
  • 61
  • 1
  • 1
  • 3
  • Where are you setting up the progress bar? There is no progress bar code in the code you posted – AlexBrand Jan 23 '14 at 20:59
  • Sorry, I tried adding a progress bar and it didn't work so I deleted it and put up the code I was trying to add the bar to incase I was wrong. I changed the code now with the progress bar I was trying to add (this is only a sample one; dont mind the times used or what this bar is doing) and I explained the error – BrianMac21 Jan 24 '14 at 12:04

3 Answers3

0

Here is another question that shows how to setup a progress dialog. Show ProgressDialog Android

Community
  • 1
  • 1
brwngrldev
  • 3,664
  • 3
  • 24
  • 45
0

If you need to get a context object inside fragment, you can use getActivity() method, which returns the activity associated to your fragment. If you've got the Activity, you've got the context, since Activity extends Context.

0

You can use any of these:

ProgressDialog progress = new ProgressDialog(getContext());

or

ProgressDialog progress = new ProgressDialog(getActivity());

or override onAttach method in your Fragment and use the context:

private Context context;
@Override
public void onAttach(Context context) {
   super.onAttach(context);
   this.context = context; //when fragment is created, context will be initialised for use.
}

then in the onCreateView method, use the context:

ProgressDialog progress = new ProgressDialog(context);

This will ensure that whenever the Fragment gets attached to the Activity, then only context will be initialized. Using getContext() or getActivity() directly can sometimes create NullPointerException.

Nilesh Singh
  • 1,750
  • 1
  • 18
  • 30