tags:

views:

284

answers:

2

I would like change the width of a Horizontal Progressbar programmatically (initial size is set in XML using RelativeLayout...but I would like to dynamically change it based on certain values).

I have tried setMinimumWidth(50) in my code, but that did not make a difference. I have also tried setting 'android:layout_width="wrap_content", but that did not work either.

Here is my XML:

<ProgressBar android:id="@+id/progress_horizontal"
     style="?android:attr/progressBarStyleHorizontal"
     android:layout_width="100dip"
     android:layout_height="wrap_content" 
/>

Thanks in advance for any assistance!

A: 

What layout is your ProgressBar in? This makes a difference as to how the layout_width and height are interpreted.

However, I think you are seeing a conflict between the View's width, and the layout_width. Each time the view is rendered, its preferred size is determined based on the minWidth, width, etc. set on it. The layout that the view sits in can then update the actual size to render based on the layout_width information. See How Android Draws Views and View size documentation for more info on the two phase process.

In your case, you have the layout_width set to 100, and the minimum width set to 50, so I would think it would always show 100.

Try setting the layout_width to wrap_content, and then updated the preferred width for the ProgressBar.

Mayra
Thanks for the quick response! I am using RelativeLayout (see below). I did change the layout_width in the XML to wrap_content, but that did not have an impact (progress bar seemed to stay at a fixed size - relatively small - no matter what I changed the setMinimumWidth to. I will checkout the documentation links you provided as well.<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@color/background">
You can edit your original post to provide the additional RelativeLayout information in a more readable way! Might help to include the entire layout, since with RelativeLayouts the various views in the layout affect each other.
Mayra
A: 

I would imagine the setmindwidth would be met when you give it an initial value of 100. the system would receognize it exceeds 50 and then continue to do as the rest of what it's told. try making the min width 150, set the xml for an initial value of 75 and in your onCreate fire off a Log.d("progbar", String.valueOf(myprogress.width));

disclaimer - written when in front of work computer without development environment for reference.

Ben
Thanks for the quick response. I changed xml to 75dip for layout_width. Then I setMinimumWidth(200) and reran. Unfortunately, this did not work (i.e., did not increase the size of the bar). When I ran Log.d("progbar", String.valueOf(myprogress.getWidth())) both before the setMinimumWidth(200) and after the log info was the same - "D/progbar (565): 0"