Horizontal Colored ProgressBar in Android ListView

269 views Asked by At

I used horizontal progress bar in my ListView under Activity as well Fragment. in this setProgress will shows correctly in Activity but not Fragment(color showing entire ProgressBar (100%)). can you please suggest me what was the issue

Here is My Code:

public class ProgressBarTest extends Activity{
    ProgressBar p;
    ListView mStatsList;
    StatsAdapter adapter;
    Context ctx;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.progressbar);
        ctx = this;
        mStatsList = (ListView)findViewById(R.id.listview);
        adapter = new StatsAdapter(ctx);
        mStatsList.setAdapter(adapter);
        adapter.notifyDataSetChanged();
    }
    class StatsAdapter extends BaseAdapter{
        Context ct;
        LayoutInflater li;
        LinearLayout item, mGoalLayout;
        ProgressBar p;
        ColorMatrixColorFilter progressMaskColorFilter;
        public StatsAdapter(Context mctx) {
            ct = mctx;
            li = (LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }

        @Override
        public int getCount() {
            return 100;
        }

        @Override
        public Object getItem(int arg0) {
            return arg0;
        }

        @Override
        public long getItemId(int arg0) {
            return arg0;
        }

        @Override
        public View getView(int arg0, View arg1, ViewGroup arg2) {
            arg1 = li.inflate(R.layout.listitem, null);
            int color = Color.RED;  // for example..but use your own color value here
            p = (ProgressBar) arg1.findViewById(R.id.goal_progress);
            p.setMax(100);
            float[] progressMatrix = {
                    0, 0, 0, 0, Color.red(color), //red
                    0, 0, 0, 0, Color.blue(color), //blue
                    0, 0, 0, 0, Color.green(color), //green
                    0, 0, 0, 1, 0  //alpha
            };
            progressMaskColorFilter = new ColorMatrixColorFilter(new ColorMatrix(progressMatrix));
            p.getProgressDrawable().setColorFilter(progressMaskColorFilter);
            /*if(arg0==1)
               */p.setProgress(60);
            return arg1;
        }

    }
}

progressbar.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical" >
   <ListView
        android:id="@+id/listview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:scrollbars="none"
        android:cacheColorHint="@android:color/transparent">
    </ListView>

</LinearLayout>

listitem.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:background="#FFFFFF"
    android:orientation="vertical" >


        <LinearLayout
            android:id="@+id/goal_layout" 
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <ProgressBar
        android:id="@+id/goal_progress"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="10dp" />  

        </LinearLayout>


</LinearLayout>
0

There are 0 answers