Danny
Danny

Reputation: 1050

How to change seekbar progress Position by changing value in the EditText?

  I want to change the seekbar progress position by changing the value in EditText dynamically. please help me out from this issue .

         <EditText  
                android:id="@+id/editText1"   
                android:layout_width="120dp"   
                android:layout_height="wrap_content" />    

     <SeekBar  
     android:id="@+id/seekBar1"  
     android:layout_width="match_parent"  
     android:layout_height="wrap_content"    
     android:max="4990000"    
     android:progress="10"  />  

And here is the java code

    seekbar1 = (SeekBar) findViewById(R.id.seekBar1);
    seekbar1.setProgress(30);
    seekbar1.setOnSeekBarChangeListener( new OnSeekBarChangeListener()
    {
        public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser)
        {
            spinnervalue=(EditText)findViewById(R.id.editText1);

                spinnervalue.setText(""+Integer.toString(progress));

            //  Notify that the progress level has changed.  

            if(txtAmount.getText().length() > 0
                    && txtYears.getText().length() > 0
                    && txtRate.getText().length() > 0)
            {
                calculate();
            }

        }

    });

so by changing the values in the edittext based on the value entered in that the seekbar has to be moved to particular value.

Upvotes: 1

Views: 16098

Answers (3)

Harpreet
Harpreet

Reputation: 3070

As much I got to know regarding your situation. You have 1 EditText & 1 SeekBar & on text change in EditText you want to set the SeekBar current selected position.

So to achieve this you need to use an onTextChangedListener on EditText and do the following in it.

First define spinnervalue=(EditText)findViewById(R.id.editText1); spinnervalue.setOnTextChangeListener(this) in onCreate()

now use following code in onTextChanged

String value = spinnervalue.getText().toString();
try{
    int progress = Integer.parseInt(value);
    if (progress >= 0 && progress <= 100)
        {
          Log.e("", "Value set:" + progress);
          seekbar1.setProgress(progress);
        }
    }
    catch(Exception e)
    {
        Log.e("", "Value set:" + value);
        e.printStackTrace();
    }    
}

also do add the below code to seekbar-tag in layout

android:max="100"

Hope I am understandable.

Thanks.

Upvotes: 0

EMarci15
EMarci15

Reputation: 303

I think you have 2 solutions for this:

  1. Create a button on the form and use the android:onClick in xml for this. Inside it's event you can set the Seekbar's progress by using seekbar1.setProgress(progress);.
  2. Add a Timer and see periodically if the value has changed:

    Timer timer = new Timer();

        timer.schedule(new TimerTask() {
          @Override
          public void run() {
              runOnUiThread(new Runnable() {
                  @Override
                  public void run() {
                      seekbar1.setProgress(progress);
                  }
              }
          }
        }, 1000, 1000);
    

Hope this helps.

Upvotes: 1

DcodeChef
DcodeChef

Reputation: 1550

Try setProgress(Value) of SeekBar. It should be in range of value supported by your Seekbar.

Upvotes: 4

Related Questions