Search code examples
androidseekbar

Android SeekBar not working in onCreateView


in my MainActivity onCreateView i create a seek bar programatically (no xml) like this:

        mVolControlSB = new SeekBar(context);
        mVolControlSB.setMax(maxVolume);
        mVolControlSB.setProgress(curVolume);
        mVolControlSB.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onStopTrackingTouch(SeekBar arg0) {
            Log.d("MainActivity", "volControlSB.setOnSeekBarChangeListener onStopTrackingTouch arg0="+arg0);
        }

        @Override
        public void onStartTrackingTouch(SeekBar arg0) {
            Log.d("MainActivity", "volControlSB.setOnSeekBarChangeListener onStartTrackingTouch arg0="+arg0);
        }

        @Override
        public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
            mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, arg1, 0);
        }
    });
    mVolControlSB.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, 0, 20));
    kitctrlLayout.addView(mVolControlSB);

it displays the seek bar but the thumb position is in the wrong place (maxVolume is 15 and curVolume is 10 in my example but it shows all the way to the left). also, the callbacks aren't been called when i drag the thumb, nothing happens. i make mVolControlSB a static field in the MainActivity class but i also tried it as a local

static  SeekBar mVolControlSB;

does anyone have an idea?

note, i should add that i'm using tab layout and this code is called inside

        public static class DummySectionFragment extends Fragment {
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {

thanks


Solution

  • It's working fine for me like this.

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        ll = (LinearLayout) findViewById(R.id.lay);
    
        mVolControlSB = new SeekBar(this);
        mVolControlSB.setMax(15);
        mVolControlSB.setProgress(10);
        mVolControlSB
                .setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
                    @Override
                    public void onStopTrackingTouch(SeekBar arg0) {
                        Log.d("MainActivity",
                                "volControlSB.setOnSeekBarChangeListener onStopTrackingTouch arg0="
                                        + arg0.getProgress());
                    }
    
                    @Override
                    public void onStartTrackingTouch(SeekBar arg0) {
                        Log.d("MainActivity",
                                "volControlSB.setOnSeekBarChangeListener onStartTrackingTouch arg0="
                                        + arg0.getProgress());
                    }
    
                    @Override
                    public void onProgressChanged(SeekBar arg0, int arg1,
                            boolean arg2) {
    
                    }
                });
        mVolControlSB.setLayoutParams(new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT));
        ll.addView(mVolControlSB);
    }