R Fauzdar
R Fauzdar

Reputation: 241

Seekbar.getRight() and Seekbar.getLeft() is returning 0

I am working in android. I want to add functionality of seekbar in my application.

This is my xml file:-

  <SeekBar

        android:id="@+id/ProgressBar01"
        android:layout_width="fill_parent"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_height="60dp"
        android:layout_below="@+id/incentives_textViewBottemLeft"
        android:max="10"
        android:progressDrawable="@drawable/incentive_progress"
        android:secondaryProgress="0"
        android:thumb="@drawable/incentives_progress_pin" />


And this is my java code related to this seek bar. 

 mSkbSample = (SeekBar)findViewById(R.id.ProgressBar01);

  v1 = (TextView)findViewById(R.id.incentives_textViewAbove_process_pin);


v1.setText(String.valueOf(progress).toCharArray(), 0, String.valueOf(progress).length());   


 mSkbSample.setProgress(progress);


 int xPos = ((mSkbSample.getRight() - mSkbSample.getLeft()) * mSkbSample.getProgress()) / mSkbSample.getMax();

 v1.setPadding(xPos,0,0,0);   

I tried to print mSkbSample.getRight()and mSkbSample.getLeft(), this always returning zero. help me to find out what mistake i have done.

Upvotes: 3

Views: 1546

Answers (2)

blessanm86
blessanm86

Reputation: 31789

You wont get the getRight value in onCreate because the UI hasn't been drawn on the screen so none of the UI elements are measure yet. Try this

SeekBar seekbar = (SeekBar)findViewById(R.id.YOUD VIEW ID); 
ViewTreeObserver vto = seekbar.getViewTreeObserver();  
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {  
    @Override  
    public void onGlobalLayout() {  
        this.seekbar.getViewTreeObserver().removeGlobalOnLayoutListener(this);  
        int left = seekbar.getLeft(); 
    }  
}); 

Put the code in the onCreate method. This will get called after the seekbar has been laid out. You should get the left value now.

Upvotes: 5

shafali
shafali

Reputation: 40

I think you are required to add

android:progress and android:secondaryProgress with some values.

you can check the example below:

http://coderzheaven.com/2011/06/how-to-use-seekbar-in-android/

Thanks, Shafali

Upvotes: 0

Related Questions