Question

I'm trying to create a progressBar using a image with text on it (actually the image is a text logo saved as png).

I already created a normal progressBar using 2 images (a gray one as background and a blue one as the progress bar) and it worked just fine.

<ProgressBar
    android:id="@+id/pbLoading"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="25dp"
    android:layout_marginRight="25dp"
    android:layout_marginTop="130dp"
    android:background="@drawable/grayloadingbar"
    android:progress="@integer/loadingProgress"
    android:progressDrawable="@drawable/custom_progress"
    android:radius="0dp"
    android:visibility="visible" />

This is the custom_progress.xml

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@android:id/progress">
        <bitmap
            android:gravity="center"
            android:src="@drawable/blueloadingbar"
            android:layout_height="fill_parent" />
        <corners android:radius="0dp" />
    </item>
</layer-list>

The problem is that replacing those 2 images with other images (the text logo images mentioned above), this doesn't look good anymore.

The loading works just fine and I can see the progress but the progress bar image seams to be having a fixed size while the background src is variable, depending on the display and I can resize it myself, something I cannot do for the progress bar.

Here is the XML:

<ProgressBar
    android:id="@+id/pbLoading"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="fill_parent"
    android:layout_height="68dp"
    android:layout_gravity="center_vertical"
    android:layout_marginLeft="25dp"
    android:layout_marginRight="25dp"
    android:layout_marginTop="130dp"
    android:background="@drawable/loading_bw"
    android:minHeight="20dip"
    android:mirrorForRtl="false"
    android:progress="@integer/fullProgress"
    android:progressDrawable="@drawable/custom_progress"
    android:radius="0dp"
    android:visibility="visible" />

And the custom_progress

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@android:id/progress">
        <bitmap
            android:gravity="center"
            android:src="@drawable/loading_color"
            android:layout_width="fill_parent"
            android:layout_height="90dp" />
        <corners android:radius="0dp" />
    </item>
</layer-list>

I cannot show you the images but the whole progress bar looks like it has 100% width & height and 25dp margin top & left, if you take the top-left corner of the background image as a reference point.

Any ideas?

I've been digging a lot for this and no luck so far.

Was it helpful?

Solution

You can add theme to your progress bar like Make a layout surround your progress bar eg: a relativeLayout place your progressbar inside it and then apply this tag to the layout. So the layout having progressbar now will be adjusted as a dialog.

android:theme="@style/Theme.Dialog"

Second method

Display display = getWindowManager().getDefaultDisplay(); 
int width = display.getWidth();  
int height = display.getHeight();

Apply the above width/2 and height/2 as the width and height of your progress bar.

Method 3

If you want the display dimensions in pixels you can use getSize:

Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;

Apply the above width/2 and height/2 as the width and height of your progress bar.But as these dimensions will be in pixels.So adjust them accordingly.

NOTE:

Method 3 works with ApiLevel :13 and above only.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top