Articles → Android → Horizontal Progress Bar In Android

Horizontal Progress Bar In Android






What Is Horizontal Progress Bar?





Example




<LinearLayout    android:layout_width="match_parent"    
android:layout_height="match_parent"    android:orientation="vertical"><Button  
            android:id="@+id/button3"        android:layout_width="wrap_content"        
            android:layout_height="wrap_content"        android:layout_margin="20dp"        android:onClick="ClickEvent"        
            android:text="Show Progress Bar" /><ProgressBar        android:id="@+id/progressBar3"        style="?android:attr/progressBarStyleHorizontal"        
            android:layout_width="match_parent"        android:layout_height="wrap_content" /><TextView        android:id="@+id/textView3"        android:layout_width="match_parent"        
            android:layout_height="wrap_content" /></LinearLayout>




int counter = 0;
Handler handler;
ProgressBar progressBar;
TextView textView;

@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_edit_text_demo);
}

public void ClickEvent(View view) {
  progressBar = (ProgressBar) findViewById(R.id.progressBar3);
  textView = (TextView) findViewById(R.id.textView3);
  handler = new Handler();

  Thread thread = new Thread(new Runnable() {

    @Override
    public void run() {
      while (counter <= 100) {
        handler.post(new Runnable() {

          @Override
          public void run() {
            progressBar.setProgress(counter);
            textView.setText(String.valueOf(counter) + " %");
          }
        });

        try {
          Thread.sleep(200);
        } catch (InterruptedException e) {
          e.printStackTrace();
        }

        counter += 10;
      }
    }
  });

  thread.start();
}



Output




Picture showing the horizontal progress bar once the screen is loaded
Click to Enlarge



Picture showing the horizontal progress bar once the process execution starts
Click to Enlarge



Picture showing the horizontal progress bar once the process execution ends
Click to Enlarge


Posted By  -  Karan Gupta
 
Posted On  -  Thursday, November 21, 2019

Query/Feedback


Your Email Id  
 
Subject 
 
Query/FeedbackCharacters remaining 250