Java – Create a ProgressBar without xml in Android

Create a ProgressBar without xml in Android… here is a solution to the problem.

Create a ProgressBar without xml in Android

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

I

couldn’t get it to work, all I got was:

enter image description here

(Picture 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 a horizontal progress bar, do you know how I can do it?
Thank you.

Solution

Change this :

t4 = new ProgressBar(this);

To this end:

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

Related Problems and Solutions