Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Thursday, January 7, 2016

Android - set a ProgressBar to be a vertical bar instead of horizontal?

Android - set a ProgressBar to be a vertical bar instead of horizontal?


I am trying to use a ProgressBar as a metering like display. I thought it was going to be an easy task and thought that ProgressBar had a property to set to be vertical, but I'm not seeing anything.

Additionally I'd like to be able to show ruler like indicator along the side of the bar to clearly indicate the current level.

Pointers appreciated - Thanks!

Answer by Romain Guy for Android - set a ProgressBar to be a vertical bar instead of horizontal?


Vertical progress bars are not supported by default.

Answer by John Leehey for Android - set a ProgressBar to be a vertical bar instead of horizontal?


To utilize the ProgressBar and make it vertical, you would have to create your own custom View extending the ProgressBar view and override the onDraw() method. This will allow you to draw it in a reverse orientation. Take a look at the source code of the ProgressBar.onDraw() (located at the bottom of the link) for help on how to do this. Best case scenario, you'll just have to swap a few x and y variables.

Answer by jagsaund for Android - set a ProgressBar to be a vertical bar instead of horizontal?


I had recently come across the need for a vertical progress bar but was unable to find a solution using the existing Progress Bar widget. The solutions I came across generally required an extension of the current Progress Bar or a completely new class in it self. I wasn't convinced rolling out a new class to achieve a simple orientation change was necessary.

This article presents a simple, elegant, and most importantly, a no-hack solution to achieving a vertical progress bar. I'm going to skip the explanation and simply provide a cookie cutter solution. If you require further details feel free to contact me or leave a comment below.

Create an xml in your drawable folder (not drawable-hdpi or drawable-mdpi -- place it in drawable). For this example I call my xml vertical_progress_bar.xml

Here's what to place in the xml file:

                                                                                                                                                        

Create an xml file called styles.xml and place it in res/values. If your project already contains styles.xml in res/values then skip this step.

Modify your styles.xml file and append the following code to the end of the file:

      

Add your new vertical progress bar to your layout. Here's an example:

  

That should be all you need to do to make use of a vertical progress bar in your project. Optionally, you might have custom drawable nine-patch images that you are using for the progress bar. You should make the appropriate changes in the progress_bar_vertical.xml file. I hope this helps you out in your project!

Answer by Ramesh Akula for Android - set a ProgressBar to be a vertical bar instead of horizontal?


You have to create your own custom progressbar.

In your xml add this layout:

   

VerticalProgressBar.java

public class VerticalProgressBar extends ProgressBar{      private int x, y, z, w;        @Override      protected void drawableStateChanged() {          // TODO Auto-generated method stub          super.drawableStateChanged();      }        public VerticalProgressBar(Context context) {          super(context);      }        public VerticalProgressBar(Context context, AttributeSet attrs, int defStyle) {          super(context, attrs, defStyle);      }        public VerticalProgressBar(Context context, AttributeSet attrs) {          super(context, attrs);      }        protected void onSizeChanged(int w, int h, int oldw, int oldh) {          super.onSizeChanged(h, w, oldh, oldw);          this.x = w;          this.y = h;          this.z = oldw;          this.w = oldh;      }        @Override      protected synchronized void onMeasure(int widthMeasureSpec,              int heightMeasureSpec) {          super.onMeasure(heightMeasureSpec, widthMeasureSpec);          setMeasuredDimension(getMeasuredHeight(), getMeasuredWidth());      }        protected void onDraw(Canvas c) {          c.rotate(-90);          c.translate(-getHeight(), 0);          super.onDraw(c);      }        @Override      public boolean onTouchEvent(MotionEvent event) {          if (!isEnabled()) {              return false;          }            switch (event.getAction()) {          case MotionEvent.ACTION_DOWN:                setSelected(true);              setPressed(true);              break;          case MotionEvent.ACTION_MOVE:              setProgress(getMax()                      - (int) (getMax() * event.getY() / getHeight()));              onSizeChanged(getWidth(), getHeight(), 0, 0);                break;          case MotionEvent.ACTION_UP:              setSelected(false);              setPressed(false);              break;            case MotionEvent.ACTION_CANCEL:              break;          }          return true;      }        @Override      public synchronized void setProgress(int progress) {            if (progress >= 0)              super.setProgress(progress);            else              super.setProgress(0);          onSizeChanged(x, y, z, w);        }  }  

Or : Jagsaund solution is also being perfect.

Answer by Pan Ng for Android - set a ProgressBar to be a vertical bar instead of horizontal?


I have the exact problem. Making a custom class (extending ProgressBar) will create code that are hard to maintain. Using a custom style will cause compatibility issue with different theme from new OS (e.g. lollipop)

Eventually, I just apply a rotation animation to an horizontal progress bar. Inspired by Pete.

  1. Create the tag in your layout xml like normal horizontal progress bar.
  2. Make sure that the size and position of the ProgressBar is what you want after rotation. (Perhaps setting negative margin will help). In my code I rotate the view from 0,0.
  3. Use the method below to rotate and set new progress.

Code:

private void setProgress(final ProgressBar progressBar, int progress) {      progressBar.setWillNotDraw(true);      progressBar.setProgress(progress);      progressBar.setWillNotDraw(false);      progressBar.invalidate();  }    private void rotateView(final View v, float degree) {      Animation an = new RotateAnimation(0.0f, degree);      an.setDuration(0);      an.setRepeatCount(0);      an.setFillAfter(true);               // keep rotation after animation      v.setAnimation(an);  }  

Answer by Daniel S. for Android - set a ProgressBar to be a vertical bar instead of horizontal?


I know that its an old post but I found a very simple solution to this problem that maybe can help somebody. First at all create a progress_drawable_vertical.xml like this:

                                                                  

Then just use this in your progressBar:

  

I also have created an progress_drawable_horizontal.xml

                                                                  

with the objetive of mantain the same style defined in progress_drawable_vertical.xml

The key here is the correct use of android:clipOrientation and android:gravity.

I found this solution here and the core of the solution is similar to jagsaund but a little bit more simple.

Answer by Lakhwinder Singh Dhillon for Android - set a ProgressBar to be a vertical bar instead of horizontal?


This was relatively easy. I found this website tutorial even for beginner like me

Tutorial for vertical progress bar


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.