Title: Remove padding in horizontal progress bar
Tags: java;android;android-studio;layout;android-constraintlayout
Question: In our application we need an indeterminate progress bar, like so:
We can achieve this by setting a negative margin on the ProgressBar, like this:
<ProgressBar
android:id="@+id/progressbar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:indeterminate="true"
android:marginTop="-7dp"
android:visibility="@{loading ? View.VISIBLE : View.GONE}" /&gt;
BUT because ConstraintLayout does not support negative margins, it will look like this:
OK, the negative margin was a hack. Let's replace it with a different hack, shall we? Let's introduce our custom view CustomProgressBar, which extends ProgressBar and overrides its onDraw method, like this:
@Override
protected void onDraw(Canvas canvas) {
int marginTop = dpToPx(7);
canvas.translate(0, -marginTop);
super.onDraw(canvas);
But all of this smells like bad code. There has to be a better solution!
What would you recommend?

Here is the accepted answer: Another way to do this is to use a Guideline and center ProgressBar between the parent top and the guideline.
<ProgressBar
android:id="@+id/progressBar2"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="0dp"
android:paddingTop="-4dp"
android:layout_height="wrap_content"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintBottom_toTopOf="@+id/guideline"
app:layout_constraintTop_toTopOf="parent" /&gt;
<android.support.constraint.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/guideline"
android:orientation="horizontal"
app:layout_constraintGuide_begin="4dp" /&gt;
Comment for this answer: Thanks, that actually did the trick! I varied the answer a bit by setting `app:layout_constraintGuide_begin="9dp"` on the guideline and only setting `app:layout_constraintTop_toTopOf="@+id/guideline"` (not constraintBottom or padding) for the progressbar – works. Still feels hacky to me, but I feel it's the best option currently.

Here is another answer: A dirty workaround I did was set the height of the ProgressBar closely to the to stroke width like so:
Progress bar:
<ProgressBar
android:id="@+id/pb_loading_progress"
style="@style/Widget.AppCompat.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="2.1dp"
android:layout_below="@id/tb_browser"
android:max="100"
android:progressDrawable="@drawable/style_browser_progress_drawable"
android:visibility="invisible" /&gt;
Progress drawable:
<?xml version="1.0" encoding="utf-8"?&gt;
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"&gt;
<item android:id="@android:id/background"&gt;
<shape android:shape="line"&gt;
<stroke
android:width="2dp"
android:color="@color/colorPrimary" /&gt;
</shape&gt;
</item&gt;
<item android:id="@android:id/progress"&gt;
<clip
android:clipOrientation="horizontal"
android:gravity="left"&gt;
<shape android:shape="line"&gt;
<stroke
android:width="2dp"
android:color="@color/white" /&gt;
</shape&gt;
</clip&gt;
</item&gt;
</layer-list&gt;
Which looked like this:
Comment for this answer: doesn't that only work for determinate, not indeterminate? The OP talks about indeterminate.
Comment for this answer: For me thats a good answer. No need to handle in the layout itself with -paddings or sth.
When you add the values to the dimensions xml, which is then more understandable. +1
Comment for this answer: @John61590 The OP did use indeterminate in his question. But the focus of the question was on removing the padding (hence the title), which shouldn't really matter whether your progressbar is determinate or indeterminate.

Here is another answer: Use inside Linearlayout and you will success, the simple way
<LinearLayout
android:layout_width="match_parent"
android:background="#efefef"
android:layout_height="wrap_content"&gt;
<ProgressBar
android:id="@+id/progressBar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:indeterminate="true"
android:visibility="gone"
android:layout_marginTop="-7dp"
android:layout_marginBottom="-7dp"
style="@style/Widget.AppCompat.ProgressBar.Horizontal"
/&gt;
</LinearLayout&gt;

Here is another answer: Solution that feels less like a hack: Wrap a huge ProgressBar in a smaller FrameLayout. That way the FrameLayout constrains its height, but the ProgressBar still shows in full.
<FrameLayout
android:layout_width="match_parent"
android:layout_height="4dp"&gt;
<ProgressBar
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="32dp"
android:layout_gravity="center" /&gt;
</FrameLayout&gt;
Comment for this answer: another solution is
Comment for this answer: It just sucks we need to do this just for something so simple.

Here is another answer: I encountered the same problem as well. And like you said, I come across numerous solutions which all seem like a hack that might break something else down the line.
With that said, I came across one solution which is to use this library instead for progress bar.
One thing to note is, it tells you to integrate it by adding:
compile 'me.zhanghai.android.materialprogressbar:library:1.3.0'
However, when I used this, it gave me an error from an Android support library for Floating Action Bar. So I'll recommend you to use this instead:
compile 'me.zhanghai.android.materialprogressbar:library:1.1.7'
A sample code snippet on how I used it:
<me.zhanghai.android.materialprogressbar.MaterialProgressBar
android:id="@+id/reviewProgressBar"
style="@style/Widget.MaterialProgressBar.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="6dp"
android:layout_below="@+id/my_toolbar"
android:indeterminate="false"
app:mpb_progressStyle="horizontal"
app:mpb_useIntrinsicPadding="false"/&gt;
Hope this helps!

Here is another answer: You declared your view without any explicit height so it's height is being picked from the pre-defined style.
<ProgressBar
android:id="@+id/progressbar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:indeterminate="true"
android:marginTop="-7dp"
android:visibility="@{loading ? View.VISIBLE : View.GONE}" /&gt;
"?android:attr/progressBarStyleHorizontal" will be searched in current theme and is stored in attrs.xml file. Since this defined by the platform it will obtain it's the value from there. As of writing this answer, I'm on  android platform 29 and searching "?android:attr/progressBarStyleHorizontal" gives the following results
➜  values
/Users/vihaanverma/Library/Android/sdk/platforms/android-29/data/res/values
➜  values
grep -rin "progressBarStyleHorizontal" .
./themes_device_defaults.xml:126:        <item name="progressBarStyleHorizontal"&gt;@style/Widget.DeviceDefault.ProgressBar.Horizontal</item&gt;
./themes_device_defaults.xml:857:        <item name="progressBarStyleHorizontal"&gt;@style/Widget.DeviceDefault.Light.ProgressBar.Horizontal</item&gt;
./themes.xml:272:        <item name="progressBarStyleHorizontal"&gt;@style/Widget.ProgressBar.Horizontal</item&gt;
./themes_holo.xml:263:        <item name="progressBarStyleHorizontal"&gt;@style/Widget.Holo.ProgressBar.Horizontal</item&gt;
./themes_holo.xml:626:        <item name="progressBarStyleHorizontal"&gt;@style/Widget.Holo.Light.ProgressBar.Horizontal</item&gt;
./public.xml:148:  <public type="attr" name="progressBarStyleHorizontal" id="0x01010078" /&gt;
./themes_material.xml:258:        <item name="progressBarStyleHorizontal"&gt;@style/Widget.Material.ProgressBar.Horizontal</item&gt;
./themes_material.xml:635:        <item name="progressBarStyleHorizontal"&gt;@style/Widget.Material.Light.ProgressBar.Horizontal</item&gt;
./styles_material.xml:1010:        <item name="progressBarStyle"&gt;?attr/progressBarStyleHorizontal</item&gt;
./attrs.xml:684:        <attr name="progressBarStyleHorizontal" format="reference" /&gt;
➜  values
Opening one of the files which contain "progressBarStyleHorizontal"  you will see minHeight defined in the style and is equal to 16dp.
<style name="Widget.Material.ProgressBar.Horizontal" parent="Widget.ProgressBar.Horizontal"&gt;
<item name="progressDrawable"&gt;@drawable/progress_horizontal_material</item&gt;
<item name="indeterminateDrawable"&gt;@drawable/progress_indeterminate_horizontal_material</item&gt;
<item name="minHeight"&gt;16dip</item&gt;
<item name="maxHeight"&gt;16dip</item&gt;
</style&gt;
This is the cause of the extra padding you are getting. You can fix it by giving your view height and a scaleY value like below
<ProgressBar
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="5dp"
android:indeterminate="true"
android:scaleY="5"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" /&gt;

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 6, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2016:00:11/10/16', 'FavoriteCount': 5.0, 'PostTypeId': 1, 'Tags': '<java><android><android-studio><layout><android-constraintlayout>', 'Title': 'Remove padding in horizontal progress bar', 'ViewCount': 9512, 'id': 40531394, 'user_AboutMe': '<p>Developer. Loves Linux, enjoys Android and open source in general.<br>\nMaking the world better, one step at a time!</p>\n', 'user_AccountId': 200552.0, 'user_DisplayName': 'mreichelt', 'user_Reputation': 12179.0, 'user_Views': 866.0, 'user_id': 446461.0}}