My application needs to create a small ProgressBar programmatically.
ProgressBar doesn't have a method to set the style (I want a small
ProgressBar). The constructor can take an AttributeSet, however, it is an
interface and requires me to implement a set of functions. Is there a way
to set the ProgressBar to a small style? (I can't use XML to create
ProgressBar.)
            Asked
            
        
        
            Active
            
        
            Viewed 1e+01k times
        
    78
            
            
        3 Answers
180
            Most of the time if you provide an AttributeSet manually you have to use one of Android's.  Luckily, they've exposed the attribute set that describes a small progress bar.  Use this code:
progressBar = new ProgressBar(activity, null, android.R.attr.progressBarStyleSmall);
 
    
    
        Neil Traft
        
- 18,367
- 15
- 63
- 70
- 
                    This makes the progressbar look yelowish and old instead of the thin blue (newish) one. – AlikElzin-kilaka Jul 22 '13 at 17:30
- 
                    8@kilaka just for future reference, change the android.R.attr.progressBarStyleSmall to android.R.attr.progressBarStyleHorizontal for the newish one :) – ElaGorilaki May 03 '14 at 16:11
- 
                    1@BabyGorilla And does progressBarStyleHorizontal work on all of android version?! – Dr.jacky Jul 04 '14 at 12:34
- 
                    Will 'progressBarStyleSmall' this style supports in mdpi devices ?? – Sakkeer Hussain May 20 '15 at 11:45
- 
                    3@Neil Traft I tried this code, but didn't work. So, Shall I call anything else to show it? – Abdulhamid Dhaiban Aug 22 '16 at 08:41
- 
                    2@AbdulhamidDhaiban You have to add your `ProgressBar` to a `ViewGroup` like `LinearLayout`, then only it will appear on the screen. – CopsOnRoad Mar 14 '18 at 10:10
12
            
            
        Create a layout xml file in res/layout directory with desired progress bar containig all attributes you need:
<?xml version="1.0" encoding="utf-8"?>
<ProgressBar
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content" ... />
Next in the Activity class you can create ProgressBar object from that layout:
LayoutInflater inflater = getLayoutInflater();
    ProgressBar bar = (ProgressBar ) inflater.inflate(R.layout.small_progress_bar, null);
where R.layout.small_progress_bar links to your layout xml file.
Can you still not use xml file?
- 
                    Instead of inflating manually, you can setYour contentview to this progressbar and change the layout once your data are loded. – amalBit Apr 08 '14 at 12:23
0
            
            
        Activity.java
  progressBar = (ProgressBar) findViewById(R.id.progressbar);
 `progressBar.setVisibility(View.VISIBLE);`// To Show ProgressBar 
 `progressBar.setVisibility(View.INVISIBLE);` //To Hide ProgressBar
Check here ProgressDialog is deprecated.What is the alternate one to use?
 
    
    
        Gowthaman M
        
- 8,057
- 8
- 35
- 54
- 
                    Not sure, but does the progressBar run always in background(just hidden from the UI) after making it invisible? – Shambhu May 15 '18 at 06:45
- 
                    @Shambhu hi,,,yes it will be run in background...but if you make **View.GONE** it will not run in background as well foreground because object is removed...i hope this may help you – Gowthaman M May 15 '18 at 07:54
 
     
     
     
    