Customize Android Progressbar

Asked

Viewed 2,978 times

1

I would like to develop a ProgressBar customized to use in my application, but I’m not sure how to do this.

It is necessary to create a sub-class of the ProgressBar? You can only do it in xml?

I’d like you to give me a basic example of how to customize this component.

follows my code, without any customization.

File main_activity.xml

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <ProgressBar 
        android:id="@+id/progressBar"
        android:layout_width="250dip"
        android:layout_height="wrap_content"
        android:max="100"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        style="?android:attr/progressBarStyleHorizontal"/>

</RelativeLayout>

Mainactivity class

import android.app.Activity;
import android.os.Bundle;
import android.widget.ProgressBar;

public class MainActivity extends Activity{

    private ProgressBar progressBar;
    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        progressBar = (ProgressBar)findViewById(R.id.progressBar);
    }

    //Mais código

}
  • 1

    What kind of customization do you want to do? A ProgressBar Android is very limited, so the chances of you having to create a subclass and override the view are great.

  • The customization I want to do is: change background color when it loads, change edge, take out rounded corners and tals... If you have a better, more customizable component for the same purpose, I would appreciate it if you could tell me :)

1 answer

3


I can’t explain to you how to do more I’ll show this example that I use:

<ProgressBar
        style="@android:style/Widget.DeviceDefault.Light.ProgressBar.Horizontal"
        android:layout_width="match_parent"
        android:layout_height="3dp"
        android:id="@+id/progressBar"
        android:layout_below="@+id/edittext_url"
        android:layout_centerHorizontal="true"
        android:progressDrawable="@drawable/custom_progressbar" />

Attention to the line android:progressDrawable="@drawable/custom_progressbar"

custom_progressbar.xml (res drawable)

<?xml version="1.0" encoding="utf-8"?>
<layer-list
  xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@android:id/progress">
        <clip>
            <shape>
                <gradient 
                    android:startColor="#ff49a8a6"
                    android:centerColor="#ff49a8a6"
                    android:endColor="#ff49a8a6"
                    android:angle="270.0" android:centerY="1.0"  />
            </shape>
        </clip>
    </item>
</layer-list>

Upshot: print android studio

Browser other questions tagged

You are not signed in. Login or sign up in order to post.