0

Following the instructions here: How to make custom seek bar in android? I was able to make my own custom seekbar that draws text inside the thumb. I know want to create my own seekbar class so that I can call it from my activity. I'm running into problems because when I create a seekbar in my main activity it does not work properly. I don't think I have set up the OnSeek Listener correctly. Here is my main class:

public class MainActivity extends Activity {

    CustomSeekBar test;

    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        test = (CustomSeekBar) findViewById(R.id.seekBar1);

        setContentView(R.layout.activity_main);

    }

}

Here is my CustomSeekBar class:

public class CustomSeekBar extends SeekBar implements OnSeekBarChangeListener {

    String TAG = "TOUCHING";

    SeekBar seekbar;

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

    // TODO Auto-generated constructor stub
}

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress,
            boolean fromUser) {
        int value = seekBar.getProgress();
        String valueString = value + "";
        seekBar.setThumb(writeOnDrawable(R.drawable.thumbler_small, valueString));
        Log.d(TAG, "Thumb is being touched");
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        // TODO Auto-generated method stub

    }

    public BitmapDrawable writeOnDrawable(int drawableId, String text) {

        Bitmap bm = BitmapFactory.decodeResource(getResources(), drawableId)
                .copy(Bitmap.Config.ARGB_8888, true);

        Paint paint = new Paint();

        paint.setColor(Color.BLACK);
        paint.setTextSize(20);

        Canvas canvas = new Canvas(bm);
        canvas.drawText(text, 0, bm.getHeight() / 2, paint);

        return new BitmapDrawable(bm);
    }

}

Here is my activity_main.xml file:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <SeekBar
        android:name="com.example.customseekbarclass"
        android:id="@+id/seekBar1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_margin="10dp"
        android:indeterminate="false"
        android:paddingLeft="15dp"
        android:paddingRight="15dp"
        android:progressDrawable="@drawable/styled_progress"
        android:thumb="@drawable/thumbler_small" />

</RelativeLayout>

This is the way my directory is setup: enter image description here How do I setup the SeekBar in my main activity class so that it calls the methods from my SeekBar class?

Community
  • 1
  • 1
DMC
  • 1,184
  • 5
  • 21
  • 50

2 Answers2

0

you should switch

    test = (CustomSeekBar) findViewById(R.id.seekBar1);

    setContentView(R.layout.activity_main);

since R.id.seekBar1 belongs to activity_main.xml otherwise you will get null for test:

    setContentView(R.layout.activity_main);
    test = (CustomSeekBar) findViewById(R.id.seekBar1);

Then I would change the seekbar part of your layout this way

 <com.example.CustomSeekBar
    android:id="@+id/seekBar1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true"
    android:layout_margin="10dp"
    android:indeterminate="false"
    android:paddingLeft="15dp"
    android:paddingRight="15dp"
    android:progressDrawable="@drawable/styled_progress"
    android:thumb="@drawable/thumbler_small" />
Blackbelt
  • 156,034
  • 29
  • 297
  • 305
  • I have edited my post to show you my directory setup. I tried setting up the seek bar using the above layout and using but I get an Inflate Exception. Any ideas? – DMC Jun 24 '13 at 10:31
  • yes I do. You missed a constructor for yout CustomSeekBar. You need to add the one that takes as paramters the Context and the Attrs. NextTime post your logcat – Blackbelt Jun 24 '13 at 10:32
  • I have done this is as shown in my edited code above. It now runs but its not calling any methods when the seekbar is pressed! – DMC Jun 24 '13 at 10:41
0

I solved this by calling the OnSeekBarChangeListener from my main activity class:

public class MainActivity extends Activity {

    CustomSeekBar test;

    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);



        setContentView(R.layout.activity_main);

        test = (CustomSeekBar) findViewById(R.id.seekBar1);
        test.setOnSeekBarChangeListener(test);



    }

}

and adding the missing constructors from my CustomSeekBar class:

public class CustomSeekBar extends SeekBar implements OnSeekBarChangeListener {

    String TAG = "TOUCHING";
    SeekBar mSeekBar;

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

    }

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

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

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress,
            boolean fromUser) {
        // TODO Auto-generated method stub
        int value = seekBar.getProgress();
        String valueString = value + "";
        seekBar.setThumb(writeOnDrawable(R.drawable.thumbler_small, valueString));
        Log.d(TAG, "Thumb is being touched");

    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
        // TODO Auto-generated method stub
        Log.d(TAG, "START");

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        // TODO Auto-generated method stub
        Log.d(TAG, "STOP");

    }



    public BitmapDrawable writeOnDrawable(int drawableId, String text) {

        Bitmap bm = BitmapFactory.decodeResource(getResources(), drawableId)
                .copy(Bitmap.Config.ARGB_8888, true);

        Paint paint = new Paint();

        paint.setColor(Color.BLACK);
        paint.setTextSize(20);

        Canvas canvas = new Canvas(bm);
        canvas.drawText(text, 0, bm.getHeight() / 2, paint);

        return new BitmapDrawable(bm);
    }

}
DMC
  • 1,184
  • 5
  • 21
  • 50