Android ProgressBarPreference Custom Problem

I am trying to implement the custom parameter ProgressBarPreference by adding the default preferences ProgressBar and TextView at the bottom and providing methods for the progress bar, such as setProgress () or setMax () to change it. Here my code works, besides updating progress in onCreate or onResume, for example, how can I let me set progress at these points? (this is actually 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> 
+4
source share
1 answer

You have a solution (Android does this with OnPreferenceChangeInternalListener, but this is a simple 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; } } } 
+6
source

All Articles