Search code examples
androidqr-codeseekbarandroid-audiomanager

Unable to generate QR Code for changed SeekBar value in android


I want to create/generate a QRCode for the changed SeekBar value for Stream_Ring. But the problem is i am not able to get/save onProgressChanged method's progress value which i need in order to generate QR.

I tried to find out the problem but unable to do so. I am uploading the whole QR generate code for your convenience.

    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

  • The problem was that, i was saving the progress in onCreate but i was never updating the value after that. In order to create a QR for the current value i had to call getProgress() in the onClickListener which returned me 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();
        }
    
    }
    

    });