views:

360

answers:

2

My application need to create a small progressBar programmatically. ProgressBar doesn't have method to set the style (I want a small progressBar). The constructor can take a AttributeSet, however it is a interface and require me implements a set of functions. Is there a way to set the progressBar small style? ( I can't use xml to create progressBar)

+2  A: 

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 Activity class you can create ProgressBar object from that layout:

LayoutInflater inflater = getLayoutInlfater();
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?

radek-k
A: 

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