How to remove ProgressBar margins?

6

I added a ProgressBar simple to a LinearLayout , however I noticed that it generates a margin above and below, as it is in the image:

  

Thesetwowhitestripes,Itriedtoremoveanyway,theonlyonesthat"seem to eventually" work are the custom progressbar, but really I I would just like to remove the margins without needing something completely customized.

How can I remove these margins?

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        tools:context="foo.bar.guilherme.exemplo.MainActivity">

    <android.support.design.widget.AppBarLayout
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:theme="@style/AppTheme.AppBarOverlay">


        <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:popupTheme="@style/AppTheme.PopupOverlay"/>

    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_main"/>

</android.support.design.widget.CoordinatorLayout>

content_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        tools:showIn="@layout/activity_main"
        tools:context="foo.bar.guilherme.exemplo.MainActivity">

    <ProgressBar
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:progress="50"
            android:id="@+id/siteProgress"/>

    <WebView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/siteView"
            android:fadeScrollbars="true"/>

</LinearLayout>
    
asked by anonymous 17.10.2016 / 16:58

2 answers

3

By default, progressBar has a fill above and below, which can be removed in some ways, such as:

Margin:

You can set the margin to a negative value, thus removing the spacing. An example would be this:

<ProgressBar
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:progress="50"
        android:layout_marginTop="-6dp"
        android:layout_marginBottom="-6dp"
        android:id="@+id/siteProgress"/>

Another way would be to change Height , but specifically, android:minHeight and android:maxHeight , like this:

<ProgressBar
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:progress="50"
            android:minHeight="20dp"
            android:maxHeight="20dp"
            android:id="@+id/siteProgress"/>

Remembering that 20dp values should be changed for your layout.

More details you can see in this question

  

Changing Margin or height may depend on the screen, ie it may not work with the same values for all types of screens.

Another possible solution would be to change the color of your progressBar . This article better explains how to do something similar.

If it's for Android 4.0+, there's the MaterialProgressBar that in addition to a layout inspired by Material Design, allows for some more changes.

If you did not feel that any of the options met your purpose, you can always do your own customProgressBar , which is usually the option of many people.

    
17.10.2016 / 17:41
2

As I understood after testing the ProgressBar is a part of "outside" (where the margins are) the blue bar would be like a sub-item, it does not count at the defined height of layout_height , so I did a test, I fixed the height of 14dp and removed% of the% of the margins from top to bottom, like this:

<ProgressBar
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="14dp"
        android:layout_marginBottom="-7dp"
        android:layout_marginTop="-7dp"
        android:progress="50"
        android:id="@+id/siteProgress"/>

And it worked perfectly:

Notethatsettingamuchlargerheightlikethis:

<ProgressBarstyle="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="44dp"
        android:progress="50"
        android:id="@+id/siteProgress"/>

It will look something like:

Notethatthebluebardoesnotchangeitssize,soifyoudothis:

<ProgressBarstyle="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="44dp"
        android:layout_marginBottom="-22dp"
        android:layout_marginTop="-22dp"
        android:progress="50"
        android:id="@+id/siteProgress"/>

The result is the same as the first example:

InlogicIcouldset-7dpto0dpor1dp,butthisaffects"subelements", but if I use negative margins it does not affect.

  

I'm going to do some more tests and see the documentation for detail, but at the beginning this seems to be the solution that works for all cases (at least the ones I was able to test)

    
17.10.2016 / 17:58