Search code examples
androidandroid-edittextseekbar

Get editText value from seekBar's progressChanged


help me please! I have 3 seek Bars and 3 Edit Texts. I just need value from each of edit text based on seek bar's progress Changed. Here is my code:

public class Main extends Activity {

    int nil,nil1,nil2,nil3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        //sb1 for seekBar1 ... etc
        //ed1 for editText1 ... etc

        sbValue(sb1, ed1);
        nil1 = nil; // I think I failed to get value in here
        sbValue(sb2, ed2);
        nil2 = nil;
        sbValue(sb3, ed3);
        nil3 = nil;

        Button btn = (Button)findViewById(R.id.button1);
        btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                Log.d("","nil1 = "+nil1+"nil2 = "+nil2+"nil3 = "+nil3);
            }
        });
    }

and it is sbValue Methode

    public int sbValue(final SeekBar sb, final EditText ed) {
        sb.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
            ...
            @Override
            public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
                int val = sb.getProgress();
                ed.setText(String.valueOf(val));
                nil = val; //here is okay, the value is right.
            }
        });
        //but when I tried to get Log from here, It always give me 0.
        return 0;
    }

}

Solution

  • I got it. I changed my variable for nil as an array:

    int[] nil = new int[3];
    

    Then added a parameter for describing edittext.

    sbValue(sb1, ed1, 1);
    sbValue(sb2, ed2, 2);
    sbValue(sb3, ed3, 3);
    

    And the method is like this:

    public int sbValue(final SeekBar sb, final EditText ed, final int sbSelect) {
        sb.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
            ...
            @Override
            public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
                int val = sb.getProgress();
                ed.setText(String.valueOf(val));
                switch (sbSelect){
                    case 1: nil[0] = val; break;
                    case 2: nil[1] = val; break;
                    case 3: nil[2] = val; break;
                }
            }
        });
        return 0;
    }