Search code examples
androidandroid-layoutviewseekbarandroid-seekbar

Align view on top of track(progress line) of seekbar


I want to align a view on top of track of seekbar.But as thumb height is included in seekbar height the view aligns on top of thumb.I tried to resolve this by providing margin equal to the height/2 of thumb but that also didn't worked as expected.How can i achieve this requirement.

  <SeekBar
            android:id="@+id/seekBar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginRight="@dimen/dp16"
            android:layout_marginLeft="@dimen/dp16"
            android:maxHeight="@dimen/dp1"
            android:thumbOffset="@dimen/dp8"
            android:progressDrawable="@color/white"/>
        <View

            android:layout_width="@dimen/dp1"
            android:layout_height="@dimen/dp5"

            android:layout_alignLeft="@+id/seekBar"
            android:layout_alignTop="@+id/seekBar"

            android:background="@color/white"

            android:id="@+id/view1" />

Code

  ((ViewGroup.MarginLayoutParams) findViewById(R.id.view1).getLayoutParams()).topMargin=(seekBar.getThumb().getMinimumHeight()/2);

Solution

  • Align you view on top of seekbar. add android:layout_above="@id/seekBar" to your view

     <SeekBar
            android:id="@+id/seekBar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginRight="@dimen/dp16"
            android:layout_marginLeft="@dimen/dp16"
            android:maxHeight="@dimen/dp1"
            android:thumbOffset="@dimen/dp8"
            android:progressDrawable="@color/white"/>
        <View
            android:layout_above="@id/seekBar"
            android:layout_width="@dimen/dp1"
            android:layout_height="@dimen/dp5"
    
            android:layout_alignLeft="@+id/seekBar"
            android:layout_alignTop="@+id/seekBar"
    
            android:background="@color/white"
    
            android:id="@+id/view1" />