Search code examples
customizationseekbarandroid-seekbarandroid-vertical-seekbar

Custom seek bar don't work correctly


Friends, help me please. I have a custom SeekBar in project. It is not displayed correctly. http://take.ms/NgHW9 enter image description here

                                  <LinearLayout
                                        android:layout_width="match_parent"
                                        android:layout_height="wrap_content"
                                        android:layout_weight="1"
                                        android:background="@mipmap/ballance_bg"
                                        android:gravity="center"
                                        android:padding="2dp">

                                        <SeekBar
                                            android:id="@+id/seekBar"
                                            android:layout_width="match_parent"
                                            android:layout_height="wrap_content"
                                            android:layout_gravity="center"
                                            android:clickable="false"
                                            android:focusable="false"
                                            android:max="1"
                                            android:maxHeight="5dp"
                                            android:minHeight="1dp"
                                            android:paddingLeft="1dp"
                                            android:paddingRight="1dp"
                                            android:progress="0"
                                            android:progressDrawable="@drawable/seekbar_progress"
                                            android:saveEnabled="true"
                                            android:thumb="@mipmap/point_icon"
                                            android:thumbOffset="0dp" />
                                    </LinearLayout>

There is custom drawable resource - seekbar_progress

    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
   <item android:id="@android:id/background">
    <nine-patch
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:src="@mipmap/like_line_2"
        android:dither="true"/>
</item>
<item
    android:id="@android:id/progress"
    android:drawable="@drawable/red_seekbar_progress" /></layer-list>

The like_line_2 is 9.patch image

There is custom drawable resource - red_seekbar_progress

    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item >
    <clip>
        <nine-patch
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:src="@mipmap/dislike_line_2"
            android:dither="true"
            />
    </clip>
</item></layer-list>

The dislike_line_2 is 9.patch image

This code fill SeekBar.

seek_1 = (SeekBar) view.findViewById(R.id.seekBar);
seek_1.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
    return true;
}
});
int sum = Integer.parseInt(products.get(0).getPositive_count()) + Integer.parseInt(products.get(0).getNegative_count());
seek_1.setMax(sum);
seek_1.setProgress(Integer.parseInt(products.get(0).getNegative_count()));

This error appears in different SeekBars


Solution

  • I found the solution itself. That's where the mistake was.

     <item
    android:id="@android:id/progress"
    android:drawable="@drawable/red_seekbar_progress" /> 
    

    Try not to use for XML in item another XML.

    correct version:

    <layer-list xmlns:android="http://schemas.android.com/apk/res/android"><item android:id="@android:id/background">
    <nine-patch
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:src="@mipmap/like_line_2"
        android:dither="true"/>
    </item>
    <item
         android:id="@android:id/progress">
        <clip>
           <nine-patch
               xmlns:android="http://schemas.android.com/apk/res/android"
               android:src="@mipmap/dislike_line_2"
               android:dither="true"/>
        </clip>
       </item>
    </layer-list>