0

I need to show the progress bar in my app, but it will not visible if I run. I created the progress bar programmatically. pls refer my code below,

ProgressBar progressBar = new ProgressBar(CusProgressActivity.this,
null, android.R.attr.progressBarStyleSmall);
progressBar.setVisibility(View.VISIBLE);
progressBar.bringToFront();

The above code is what is try to show the progress bar and I dont want to use the xml files to show the progress bar. I dont know whats wrong with my code and I need to show small progress in my activity by programmatically.

Sean Owen
  • 66,182
  • 23
  • 141
  • 173
sivanesan1
  • 779
  • 4
  • 20
  • 44

2 Answers2

2

Use a layout and add the progress bar as a view to the layout as follows -

    RelativeLayout relativeLayout = new RelativeLayout(this);

    ProgressBar pb = new ProgressBar(this, null, android.R.attr.progressBarStyleHorizontal);

    relativeLayout .addView(pb);
    setContentView(relativeLayout);

    pb.setProgress(10);
user1721904
  • 843
  • 4
  • 13
  • my progress is showing now in white color, is it possible to change it in black color – sivanesan1 Feb 23 '13 at 11:55
  • 1
    [How to change default color of progress bar](http://stackoverflow.com/questions/6421178/how-to-change-default-color-of-progress-bar) – Mani Feb 24 '13 at 13:32
1

You must either set the view to be the contentview of your activity, or add it to the view heiarchy using ViewGroup.addView to append you progressbar to the root view

public void ViewGroup.addView (View child) Added in API level 1

Adds a child view. If no layout parameters are already set on the child, the default parameters for this ViewGroup are set on the child.

Note: do not invoke this method from draw(android.graphics.Canvas), onDraw(android.graphics.Canvas), dispatchDraw(android.graphics.Canvas) or any related method.

Parameters child the child view to add

public void Activity.setContentView (View view) - Added in API level 1

Set the activity content to an explicit view. This view is placed directly into the activity's view hierarchy. It can itself be a complex view hierarchy. When calling this method, the layout parameters of the specified view are ignored. Both the width and the height of the view are set by default to MATCH_PARENT. To use your own layout parameters, invoke setContentView(android.view.View, android.view.ViewGroup.LayoutParams) instead. Parameters view The desired content to display.

Moog
  • 10,193
  • 2
  • 40
  • 66