Search code examples
javaandroidandroid-audiomanagerandroid-seekbar

ProgressBar value does not change


I want to use the progress variable of onProgressChanged method outside of the inner class. I tried to use a method defined in outer class and called from inner class to get that value. But every-time I get 0.
No updated value of progress variable. It is basically an android app to get the updated value of SeekBar.
Have a look at the code for better understanding. Currently I am only getting previous/last value of progress. Not getting updated value immediately as I change it.

Button gene;
ImageView image;
SeekBar ring_bar;
String collect;
private AudioManager am;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_prof);

    gene = (Button) findViewById(R.id.gene);
    image = (ImageView) findViewById(R.id.image);
    ring_bar = (SeekBar) findViewById(R.id.ring_bar);
    am = (AudioManager)getSystemService(Context.AUDIO_SERVICE);

    int maxV = am.getStreamMaxVolume(AudioManager.STREAM_RING);
    int curV = am.getStreamVolume(AudioManager.STREAM_RING);
    ring_bar.setMax(maxV);
    ring_bar.setProgress(curV);
    ring_bar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onStopTrackingTouch(SeekBar arg0) {
        }

        @Override
        public void onStartTrackingTouch(SeekBar arg0) {
        }

        @Override
        public void onProgressChanged(SeekBar arg0, int progress, boolean arg2) {
            am.setStreamVolume(AudioManager.STREAM_RING, progress, 0);
        }
    });

    int ring = ring_bar.getProgress();

    String ringgg = Integer.toString(ring);

    collect = ringgg;

    gene.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
            try{
                BitMatrix bitMatrix = multiFormatWriter.encode(collect, BarcodeFormat.QR_CODE,200,200);
                BarcodeEncoder barcodeEncoder = new BarcodeEncoder();
                Bitmap bitmap = barcodeEncoder.createBitmap(bitMatrix);
                image.setImageBitmap(bitmap);
            }
            catch (WriterException e){
                e.printStackTrace();
            }

        }
    });
}

}


Solution

  • Your problem is, that you save the progress in onCreate to your variable but you never update the value after that.

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    //....
    
    int ring = ring_bar.getProgress();//Progress at the time of onCreate
    
    gene.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
    
            MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
            try{
               //collect is still the same value since onCreate 
                BitMatrix bitMatrix = multiFormatWriter.encode(collect, BarcodeFormat.QR_CODE,200,200);
                //....
            }
            catch (WriterException e){
                e.printStackTrace();
            }
    
        }
    });
    

    In order to create a QR for the current value you have to call getProgress() in the onClickListener. This will return you the current value of the progress bar.

    gene.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
    
            MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
            try{
                int ringtoneVolumeValue = ring_bar.getProgress();
                String ringtoneVolume = Integer.toString(ringtoneVolumeValue);
                BitMatrix bitMatrix = multiFormatWriter.encode(ringtoneVolume, BarcodeFormat.QR_CODE,200,200);
                //....
            }
            catch (WriterException e){
                e.printStackTrace();
            }
    
        }
    });