Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add a ProgressBar into a custom view

I could not find tips or examples about how to do this. I want to add a progressbar over a Rect that I have already drawn, so how I can I do this?

Your answers will be truly appreciated! :)

Edited

public class MainView extends View {

    public MainView(Context context) {
        super(context);
    }

    public MainView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        logo.set(getPX(5), getPX(10), getPX(65), getPX(70));

        background.set(getPX(30), getPX(2), canvas.getWidth() - getPX(10),
                getPX(81));

        canvas.drawRect(background, paint);
        canvas.drawRect(logo, paint);
    }

    final private int getPX(float dp) {
        return (int) (getResources().getDisplayMetrics().density * dp);
    }
}

1 Answers

Based on your implementation (as you shown it to me):

<ScrollView .. >
    <LinearLayout .. >
        <MainView .. />
        <MainView .. />
        <MainView .. />
    </LinearLayout>
</ScrollView>

my proposed solution would be - extend your CustomView class with LinearLayout (because then you can add additional views to your custom view):

public class MainView extends LinearLayout {
    private Rect logo;
    private Rect background;

    public MainView(Context context) {
        super(context);

        setWillNotDraw(false);  //needed in order to call onDraw method

        logo = new Rect();
        background = new Rect();
    }

    public MainView(Context context, AttributeSet attrs) {
        super(context, attrs);

        setWillNotDraw(false);

        logo = new Rect();
        background = new Rect();

        RelativeLayout progressBarLayout = new RelativeLayout(context);
        RelativeLayout.LayoutParams lay = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.FILL_PARENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        ProgressBar progressBar = new ProgressBar(context, null, R.attr.progressBarStyleHorizontal);
        progressBar.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
        progressBar.setIndeterminate(true); //remove that (only for demonstration purposes)
        lay.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        progressBarLayout.addView(progressBar, lay);

        addView(progressBarLayout);

       //Apart from the ProgressBar you are able to add as many views as you want to your custom view and align them as you would like
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        logo.set(getPX(5), getPX(10), getPX(65), getPX(70));

        background.set(getPX(30), getPX(2), canvas.getWidth() - getPX(10),
                getPX(81));

        canvas.drawRect(background, paint);
        canvas.drawRect(logo, paint);
    }

    final private int getPX(float dp) {
        return (int) (getResources().getDisplayMetrics().density * dp);
    }
}

In this example I am only showing how to add ProgressBar component, because that was your original question

You will need to add some more code in order to carry out your requirements (got from questioner): enter image description here

P.S. This is just a solution to your particular problem/request. I would advise to use ListView component, which is better in the sense that it reuses views, so in such cases where you will have LOTS OF custom view instances, your application might become unusable, because there will be too much load on the activity class.

In order for you to migrate to using ListView component, try some examples first, like this

like image 116
Boris Mocialov Avatar answered Apr 27 '26 12:04

Boris Mocialov