2

I want to have Horizontal progress bar that moves from 0 to 100. I added this code in layout. what should i do for moving this progress bar?

 <ProgressBar
        android:id="@+id/progressBar"
        android:indeterminate="false"
        android:layout_width="300dp"
        android:layout_height="50dp"
        android:layout_marginTop="100dp"
        android:layout_marginLeft="50dp"
        android:progress="0"
        android:max="100"
        style="@android:style/Widget.ProgressBar.Horizontal"
        />
Sagar gujarati
  • 152
  • 1
  • 15
  • 1
    From code you need to call `setProgress(int)` on the ProgressBar view object and the bar will move accordingly – MatPag Jan 18 '20 at 12:40
  • Does this answer your question? [How to create a horizontal loading progress bar?](https://stackoverflow.com/questions/13409912/how-to-create-a-horizontal-loading-progress-bar) – mmBs Jan 18 '20 at 12:49
  • No,this isn't answer of my question. –  Jan 18 '20 at 13:05
  • find progress view by id in kotlin code and set progress value in loop from 1 to 100 ``progressbar.setProgress(value)`` – amin mahmoudi Jan 18 '20 at 13:21
  • Thanks,could you please help me by code? –  Jan 18 '20 at 13:35

2 Answers2

1

Try this code :

 import android.app.Activity;
    import android.os.AsyncTask;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.ProgressBar;
    import android.widget.TextView;

    public class MainActivity extends Activity {

        Button btn;
        private ProgressBar progressBar;
        TextView txt;
        Integer count =1;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main_activity);

            progressBar = (ProgressBar) findViewById(R.id.progressBar);
            progressBar.setMax(100);
            btn = (Button) findViewById(R.id.btn);
            btn.setText("Start");
            txt = (TextView) findViewById(R.id.output);

            btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                    count =1;
                    progressBar.setVisibility(View.VISIBLE);
                    progressBar.setProgress(0);

                    new MyTask().execute(100);

        });
        }
        class MyTask extends AsyncTask<Integer, Integer, String> {
            @Override
            protected String doInBackground(Integer... params) {
                for (count=1 ; count <= params[0]; count++) {
                    try {
                        Thread.sleep(1000);
                        publishProgress(count);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                return "Task Completed.";
            }
            @Override
            protected void onPostExecute(String result) {
                progressBar.setVisibility(View.GONE);
                    txt.setText(result);
                    btn.setText("Restart");
            }
            @Override
            protected void onPreExecute() {
                txt.setText("Task Starting...");
            }
            @Override
            protected void onProgressUpdate(Integer... values) {
                txt.setText("Running..."+ values[0]);
                progressBar.setProgress(values[0]);
            }
        }
    } 
Sagar gujarati
  • 152
  • 1
  • 15
0
android:indeterminate: false

You must add the feature.

Sample

  <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center"
    >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="116dp"
        android:text="Do some stuff" />

    <ProgressBar
        style="@android:style/Widget.ProgressBar.Horizontal"
        android:id="@+id/progressBar1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:progress="0"/>

</LinearLayout>

class MainActivity : AppCompatActivity() {

private var progressBarStatus = 0
var dummy:Int = 0

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    // get the references from layout file
    var btnStartProgress = this.button1
    var progressBar = this.progressBar1

    // when button is clicked, start the task
    btnStartProgress.setOnClickListener { v ->

        // task is run on a thread
        Thread(Runnable {
            // dummy thread mimicking some operation whose progress can be tracked
            while (progressBarStatus < 100) {
                // performing some dummy operation
                try {
                    dummy = dummy+25
                    Thread.sleep(1000)
                } catch (e: InterruptedException) {
                    e.printStackTrace()
                }
                // tracking progress
                progressBarStatus = dummy

                // Updating the progress bar
                progressBar.progress = progressBarStatus
            }

        }).start()
    }
}

}

Arda Kazancı
  • 8,341
  • 4
  • 28
  • 50