Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6245583
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T12:31:54+00:00 2026-05-24T12:31:54+00:00

I’m trying to implement a custom ProgressBarPreference, adding at the bottom of a default

  • 0

I’m trying to implement a custom ProgressBarPreference, adding at the bottom of a default preference a ProgressBar and a TextView, and granting the methods of the progressbar like setProgress() or setMax() for changing it. Here’s my code works except the updating the progress within onCreate or onResume for example, how can I allow to set the progress in those points ? (actually is giving a null pointer exception over mProgressBar):

ProgressBarPreference.java

public class ProgressBarPreference extends Preference {


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

    public ProgressBarPreference(Context context, AttributeSet attrs,
            int defStyle) {
        super(context, attrs, defStyle);
    }

    ProgressBar mProgressBar;

    @Override
    protected View onCreateView(ViewGroup parent) {

        LayoutInflater li = (LayoutInflater) Manager.appcontext.getSystemService(Service.LAYOUT_INFLATER_SERVICE);
        View myLayout=li.inflate(R.layout.progressbarpreference, null, false);
                ((ViewGroup)myLayout.findViewById(R.id.preference_super_container)).addView(super.onCreateView(parent));
        mProgressBar=(ProgressBar) myLayout.findViewById(R.id.preference_progress_bar);
        return myLayout;
    }       
    public void setProgress(int value){
         mProgressBar.setProgress(value);

    }




}

progressbarpreference.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
>
    <LinearLayout
        android:id="@+id/preference_super_container"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:orientation="vertical"
    ></LinearLayout>

    <RelativeLayout
        android:id="@+id/relativeLayout1"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:paddingLeft="20sp"
        android:paddingRight="20sp"
    >
        <TextView
            android:layout_alignParentRight="true"
            android:text="0 new of 100"
            android:id="@+id/preference_progress_label"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:gravity="center"
            android:layout_centerVertical="true"
            android:paddingLeft="15sp"
            android:textColor="#FFFFFF"
        ></TextView>
        <ProgressBar
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:id="@+id/preference_progress_bar"
            android:layout_centerVertical="true"
            android:progress="50"
            android:layout_toLeftOf="@id/preference_progress_label"
            android:layout_alignParentLeft="true"
        ></ProgressBar>
    </RelativeLayout>

</LinearLayout>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T12:31:55+00:00Added an answer on May 24, 2026 at 12:31 pm

    Ok have a solution (Android does it with an OnPreferenceChangeInternalListener but this is an easy working solution)

    public class ProgressBarPreference extends Preference {
    
    
        public ProgressBarPreference(Context context) {
            super(context);
        }
        public ProgressBarPreference(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public ProgressBarPreference(Context context, AttributeSet attrs,
                int defStyle) {
            super(context, attrs, defStyle);
        }
    
        private ProgressBar mProgressBar;
        private TextView mLabel;
        private int lastReqProgress=-1;
        private int lastReqMax=-1;
        private String lastLabel;
    
        @Override
        protected View onCreateView(ViewGroup parent) {
    
            LayoutInflater li = (LayoutInflater) Manager.appcontext.getSystemService(Service.LAYOUT_INFLATER_SERVICE);
            View myLayout=li.inflate(R.layout.progressbarpreference, null, false);
                    ((ViewGroup)myLayout.findViewById(R.id.preference_super_container)).addView(super.onCreateView(parent));
            mProgressBar=(ProgressBar) myLayout.findViewById(R.id.preference_progress_bar);
            mLabel=(TextView) myLayout.findViewById(R.id.preference_progress_label);
            if (lastReqProgress>-1){
                mProgressBar.setProgress(lastReqProgress);
            }
            if (lastReqMax>-1){
                mProgressBar.setMax(lastReqMax);
            }
            if (lastLabel!=null){
                mLabel.setText(lastLabel);
            }
    
            return myLayout;
        }
    
    
        public void setProgress(int value){
            if (mProgressBar!=null){
                mProgressBar.setProgress(value);
            } else {
                lastReqProgress=value;
            }
    
        }
    
        public void setMax(int value){
            if (mProgressBar!=null){
                int savedprogress=mProgressBar.getProgress();
                mProgressBar.setMax(0);
                mProgressBar.setMax(value);
                mProgressBar.setProgress(savedprogress);
            } else {
                lastReqMax=value;
            }
    
        }
    
    
        public void setLabel(String text){
            if (lastLabel!=null){
                mLabel.setText(text);
            } else {
                lastLabel=text;
            }
        }
    
    
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm trying to create an if statement in PHP that prevents a single post
I am trying to loop through a bunch of documents I have to put

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.