luckysing_noobster
luckysing_noobster

Reputation: 2013

How to make an progressBar touch bottom screen border in Android

I am having a similar problem as mentioned in here

Here is my xml layout :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true">

<ImageButton
    android:id="@+id/imageButton”
    android:layout_width="40dip"
    android:layout_height="match_parent"
    android:background="@android:color/transparent"
    android:contentDescription="@string/descrp"
    android:scaleType="center"
    android:src="@drawable/some_drawable”
    android:visibility="visible" />

<TextView
    android:id="@+id/textview”
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginRight="10dip"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true"
    android:textColor="#FFFFFF"
    android:textStyle="bold" />

<ProgressBar
    android:id="@+id/progressbar”
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="match_parent"
    android:layout_height="10dp"
    android:layout_alignParentBottom="true" />
</RelativeLayout>

How can I make the ProgressBar touch the border of the screen without any gap between the progress bar and border?

Upvotes: 0

Views: 1997

Answers (3)

S.Trathi
S.Trathi

Reputation: 13

Just add the following attributes inside ProgressBar

        android:minHeight="4dp"      // or whatever height you want
        android:maxHeight="4dp"      // or whatever height you want

Complete code:

    <ProgressBar
        android:id="@+id/snackbar_progress_crop"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        android:indeterminate="true"
        android:indeterminateOnly="false"

        android:minHeight="4dp"
        android:maxHeight="4dp"
        />

Upvotes: 0

Malte
Malte

Reputation: 561

I have been struggling with this for a while now. My conclusion: There is no way to archieve this behaviour with XML for arbitrary screen sizes. On some screen resolutions the progress bar will always be misplaced a little.

My simple solution: Set the Y of the progressBar programmatically to the bottom of the super view/layout substracting half of the progressBar's height

protected void onLayout(boolean changed, int l, int t, int r, int b) {
    super.onLayout(changed, l, t, r, b);

    progressBar.setY(layout.getY() + layout.getHeight() -  progressBar.getHeight() / 2);
}

Works like a charm and for all screen sizes.

Upvotes: 1

luckysing_noobster
luckysing_noobster

Reputation: 2013

I found a work around.I am basically adding a layout_marginBottom="-4dp" to my ProgressBar and wrapped it inside a RelativeLayout and aligned it to the bottom of the parent view.This changes may break your app in the future.For a better solution design a custom progress bar with your own custom drawable which you can align correctly and occupies less canvas space compared to the progressBarStyleHorizontal.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true">

<ImageButton
android:id="@+id/imageButton”
android:layout_width="40dip"
android:layout_height="match_parent"
android:background="@android:color/transparent"
android:contentDescription="@string/descrp"
android:scaleType="center"
android:src="@drawable/some_drawable”
android:visibility="visible" />

<TextView
android:id="@+id/textview”
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dip"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:textColor="#FFFFFF"
android:textStyle="bold" />
//changes
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ProgressBar
        android:id="@+id/some_other_id"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="10dp"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="-4dp" />
</RelativeLayout></RelativeLayout>

Upvotes: 0

Related Questions