Create ProgressBar without xml in Android

I am trying to create a progressBar (not a progressBar dialog ) using only java.

I cannot get it to work, all I get is:

enter image description here

(image that keeps spinning in circles)

Here is my code:

    b4 = new Button(this);
    t4 = new ProgressBar(this);

    b4.setId(4);
    b4.setText(SensorData.sensorName[4]);
    b4.setTypeface(null, 1);
    b4.setTextSize(15);
    b4.setGravity(Gravity.LEFT);
    b4.setTextColor(R.color.black);
    b4.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.icon, 0); // down

    // t4.setId(4);
    t4.setPadding(20, 10, 10, 10);
    t4.setIndeterminate(false);
    t4.setProgress(0);
    t4.setScrollBarStyle(ProgressBar.SCROLLBARS_OUTSIDE_INSET);
    t4.setMax(100);
    t4.setVisibility(t4.VISIBLE);


    sensorsView.addView(b4, new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    sensorsView.addView(t4, new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

    b4.setOnClickListener(this);

What I want is horizontal progress, but do you know how I can do this? Thank.

+3
source share
2 answers

Change this:

t4 = new ProgressBar(this);

:

t4 = new ProgressBar(this, null, android.R.attr.progressBarStyleHorizontal);
+5
source

You can do:

t4.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL)
0
source

All Articles