6

Is it possible to add progressbar to ActionBarSherlock? I need to show and hide it in particular time. But it has to be located inside ActionBarSherlock.

Code of my class. you can see, that I use requestWindowFeature(Window.FEATURE_PROGRESS) but it doesn`t make any difference. The same result. :

public class RecipeActivity extends SherlockActivity {

private String picture;
private String TAG = "RecipeActivity";
private ImageView viewPicture;
private RecipeGeneral recipeGeneral;
private Recipe recipe;
private String [] pictures;
private ArrayList<Step> steps;
//private ImageView viewStar;
private DataBaseFactory db;
private NyamApplication application;
private TextView viewDescription;
private TextView userView;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_PROGRESS);
    setProgressBarIndeterminateVisibility(true);

    try {
        setContentView(R.layout.recipe_page);

        getSupportActionBar().setDisplayShowTitleEnabled(false);
        recipeGeneral = (RecipeGeneral)getIntent().getSerializableExtra("Recipe");
        Log.d(TAG, "recipeGeneral = " + recipeGeneral.toString());
        picture = Constants.URL + recipeGeneral.getImg_url();
        Log.d(TAG,"picture : " + picture);
        viewPicture = (ImageView)findViewById(R.id.picture);
        new DownloadImageTask().execute(pictures);

        viewDescription = (TextView)findViewById(R.id.recepy_description);
        TextView viewTitle = (TextView)findViewById(R.id.recepy_title);
        TextView ratingView = (TextView)findViewById(R.id.txtTitle3);
        userView = (TextView)findViewById(R.id.author_name);

        application = (NyamApplication)getApplication();
        db = application.getDB();

        if (getIntent().getBooleanExtra("isFavorites", false) == true) {
            Log.d(TAG, "isFavorites = " + getIntent().getBooleanExtra("isFavorites", false));

            viewDescription.setText(((Recipe)recipeGeneral).getDescription());
            viewTitle.setText(recipeGeneral.getTitle());
            Log.d(TAG, "Title = " + recipeGeneral.getTitle());
            ratingView.setText(String.valueOf(recipeGeneral.getFavorites_by()));
            Log.d(TAG, "Rating = " + String.valueOf(recipeGeneral.getFavorites_by()));
            userView.setText(((Recipe)recipeGeneral).getUser());
            Log.d(TAG, "User = " + ((Recipe)recipeGeneral).getUser());


            steps = db.getStepsByRecipeId(recipeGeneral.getId());
            if (steps != null) {
                ((Recipe)recipeGeneral).setSteps(steps);
            }

        } else {
            Log.d(TAG, "isFavorites = " + getIntent().getBooleanExtra("isFavorites", false));
            Object [] params = new Object[] {this, Constants.URL + recipeGeneral.getPath()+ Constants.JSON};

            new AsyncHttpGetRecipe().execute(params);   
            new AsyncHttpGetSteps().execute(params);    
            viewTitle.setText(recipeGeneral.getTitle());

            ratingView.setText(recipeGeneral.getFavorites_by());
            Log.d(TAG, "Rating = " + recipeGeneral.getFavorites_by());

        }

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


private class DownloadImageTask extends AsyncTask<String,Void,Bitmap> {

    Bitmap  bitmap = null;

    @Override
    protected Bitmap doInBackground(String... str) {
        try{   
            InputStream in = new java.net.URL(picture).openStream();
            bitmap = BitmapFactory.decodeStream(new SanInputStream(in));
            //viewPicture.setImageBitmap(bitmap);
        }
        catch(Exception e){
            e.printStackTrace();
        }
        return bitmap;
    }

    @Override 
    protected void onPostExecute(Bitmap result) {
        super.onPostExecute(result);
        viewPicture.setBackgroundDrawable(new BitmapDrawable(bitmap));
    }
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    com.actionbarsherlock.view.MenuInflater inflater = getSupportMenuInflater();
    inflater.inflate(R.menu.item_page_menu, (com.actionbarsherlock.view.Menu) menu);
    return super.onCreateOptionsMenu(menu);
}

public void onClick(View v) {
    switch(v.getId()) {
    case R.id.icon_little3:
        Log.d(TAG,"further");
        if (getIntent().getBooleanExtra("isFavorites", false) == false) {
            if (recipe != null && steps != null) {
                recipe.setSteps(steps);
                db.addRecipeToFavorites(recipe);
            }
        }
    }
}


@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch(item.getItemId())  {
    case R.id.menu_further: 
        Log.d(TAG,"In menu_further");
        Intent stepIntent = new Intent(this, StepActivity1.class);

        if (getIntent().getBooleanExtra("isFavorites", false) == true) {
            Log.d(TAG,"id: " + recipeGeneral.getId());
            Log.d(TAG,"desc: " + recipeGeneral.getTitle());
            stepIntent.putExtra("Recipe1", recipeGeneral);
        } else {
            Log.d(TAG,"id: " + recipe.getId());
            Log.d(TAG,"desc: " + recipe.getTitle());
            recipe.setSteps(steps);
            stepIntent.putExtra("Recipe1", recipe);
        }
        startActivity(stepIntent);
        return true;
    case R.id.menu_cart: 
        Toast.makeText(this, "Cart", 10000).show();
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }

}


class AsyncHttpGetRecipe extends AsyncTask<Object, String, Recipe> {

    @Override
    protected  Recipe doInBackground(Object... params) {
        Recipe recipeAsync = null;
        try {
            recipeAsync = ApiFactory.getRecipe((Context)params[0], (String)params[1]);
        } catch (JSONException e) {
            e.printStackTrace();
        } 
        recipe  = recipeAsync;
        return recipeAsync;
    }

    @Override
    protected void onPostExecute(Recipe recipeTemp) {
        viewDescription.setText(recipeTemp.getDescription());
        Log.d(TAG, "Description = " + recipeTemp.getDescription());
        userView.setText(recipeTemp.getUser());
        Log.d(TAG, "User = " + recipeTemp.getUser());

    }
}

class AsyncHttpGetSteps extends AsyncTask<Object, String, ArrayList<Step>> {

    @Override
    protected  ArrayList<Step> doInBackground(Object... params) {
        ArrayList<Step> stepsAsync = null;
        try {
            stepsAsync = ApiFactory.getSteps((Context)params[0],  (String)params[1]);
        } catch (JSONException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return stepsAsync;
    }

    @Override
    protected void onPostExecute(ArrayList<Step> stepsAsync) {
        steps = stepsAsync;
    }
}

}

Stas
  • 1,355
  • 6
  • 22
  • 40

3 Answers3

16

Yes. You can add a ProgressBar to an ActionBar using ABS.

This is an extract from the source provided below, if the solutions helps, +1 the original poster ;-)

In your onCreate() method, add this piece of code:

requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);  
setProgressBarIndeterminateVisibility(true);

And when you are done with the task that you need to display the ProgressBar for, hide the ProgressBar using this code:

setProgressBarIndeterminateVisibility(false);

Credit: https://stackoverflow.com/a/9157874/450534

Community
  • 1
  • 1
Siddharth Lele
  • 27,623
  • 15
  • 98
  • 151
  • It doesn`t show progressbar or any aditional animation. This two lines just disable my actionbar from work. Now it throws exception after pushing any button from actionbar. It doesn`t matter, I just need progress bar. What I`m doing wrong? – Stas May 25 '12 at 13:48
  • You are calling Window.FEATURE_PROGRESS – Siddharth Lele May 25 '12 at 14:06
  • @Radu: I see you have also posted a question for the same issue. But I am afraid, I have never seen this behaviour. Let me see if I can use your code from the question and reproduce the behaviour. – Siddharth Lele Mar 26 '14 at 02:02
  • @SiddharthLele Hello, can you just use ActionBarSherlock's sample demo please? No need to use my code, the ABS demo does the same thing. Only thing I have changed in ABS demo was the support-v4.jar version, to match the latest one. – Radu Mar 26 '14 at 09:58
5

Yes, you can, ABS has such feature. Next piece of code is from Demos sample app of ABS:

public class Progress extends SherlockActivity  {
    Handler mHandler = new Handler();
    Runnable mProgressRunner = new Runnable() {
        @Override
        public void run() {
            mProgress += 2;

            //Normalize our progress along the progress bar's scale
            int progress = (Window.PROGRESS_END - Window.PROGRESS_START) / 100 * mProgress;
            setSupportProgress(progress);

            if (mProgress < 100) {
                mHandler.postDelayed(mProgressRunner, 50);
            }
        }
    };

    private int mProgress = 100;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setTheme(SampleList.THEME); //Used for theme switching in samples
        super.onCreate(savedInstanceState);

        //This has to be called before setContentView and you must use the
        //class in com.actionbarsherlock.view and NOT android.view
        requestWindowFeature(Window.FEATURE_PROGRESS);

        setContentView(R.layout.progress);

        findViewById(R.id.go).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                if (mProgress == 100) {
                    mProgress = 0;
                    mProgressRunner.run();
                }
            }
        });
    }
}

this sample you can find in samples\demos\ directory of ABS (Progress.java in eclipse workspace)

Vladimir
  • 3,774
  • 3
  • 23
  • 27
  • 3
    try setSupportProgressBarIndeterminateVisibility(true); instead setProgressBarIndeterminateVisibility(true); and requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); instead requestWindowFeature(Window.FEATURE_PROGRESS); – Vladimir May 25 '12 at 14:01
  • 1
    if you want IndeterminateProgress – Vladimir May 25 '12 at 14:02
  • I think it always first in actionbar menu, but i'm not sure. Ask another question, maybe someone knows – Vladimir May 25 '12 at 14:11
3

For full compatibility you should use:

setSupportProgressBarIndeterminateVisibility(true);

Here is an example:

public class MyActivity extends SherlockFragmentActivity {
    //...
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);     
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);        
        setContentView(R.layout.my_layout);
        //...
        setSupportProgressBarIndeterminateVisibility(true);
    }
}
Alejandro Casanova
  • 3,633
  • 4
  • 31
  • 46