Android - SeekBar - Sử dụng và tùy biến SeekBar trong Android Studio

Bước 1: Tạo drawable file để tùy biến màu sắc, hình dạng thanh trượt

Tạo một file custom_seekbar.xml trong thư mục drawable với nội dung như sau:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:id="@android:id/background">        
<shape android:shape="rectangle" >            
<corners android:radius="14dp" />
            <gradient                
android:angle="270"                
android:endColor="@color/volume_seed_back"                
android:startColor="@color/volume_seed_back" />        
</shape>    
</item>    
<item android:id="@android:id/secondaryProgress">        
<clip>            
<shape android:shape="rectangle" >                
<corners android:radius="14dp" />
 <gradient                    
android:angle="270"                    
android:endColor="@color/volume_seed"                    
android:startColor="@color/volume_seed" />            
</shape>        
</clip>    
</item>    
<item android:id="@android:id/progress">        
<clip>            
<shape android:shape="rectangle" >                
<corners android:radius="14dp" />
<gradient                    
android:angle="270"                    
android:endColor="@color/volume_seed"                    
android:startColor="@color/volume_seed" />            
</shape>        
</clip>    
</item>
</layer-list>



Trong đó chú ý:

- Các trường color để tùy biến màu sắc
- Trường radius trong thẻ corner để chỉnh độ bo tròn góc 2 đầu seekbar
- item với id backgound: nền của seekbar
- item với id progress: thanh progress khi kéo
- item với id secondaryProgress: thanh progress thứ 2: vd như dùng để hiển thị dữ liệu tải về khi phát nhạc online

Bước 2: Tạo một theme trong style.xml hoặc đặt trực tiếp thuộc tính trong thẻ xml của seekbar

<style name="MySeekBar" parent="android:Widget.SeekBar">    
<item name="android:indeterminateOnly">false</item>    
<item name="android:progressDrawable">@drawable/custom_seekbar</item>    
<item name="android:minHeight">32dp</item>    <item name="android:maxHeight">32dp</item>    <item name="android:thumb">@mipmap/custom_thumb</item>    <item name="android:thumbOffset">0dp</item>
</style>


Chú ý:
- android:progressDrawable: thực hiện drawable custom vừa tạo ở bước 1
- android:thumb: đặt hình ảnh bất kỳ, hoặc drawable, color cho nút kéo thả trên seekbar

Bước 3: Tạo view seekbar

<SeekBar
    style="@style/MySeekBar"    
    android:layout_width="match_parent"    
    android:layout_height="wrap_content"
    android:splitTrack="false" />
Chú ý:
- style: áp dụng theme vừa tạo ở bước 3
- splitTrack: false: làm seekbar ko bị cắt bởi thumb

Bước 4: Bắt sự kiện cho seekbar trong activity
Chú ý bắt sự kiện bằng setOnSeekBarChangeListener --> onStopTrackingTouch  và onProgressChanged





Nhận xét

Bài đăng phổ biến từ blog này

Android - Date Time - Lấy giá trị ngày, giờ theo định dạng tùy biến trong Android Studio

Android - Custom SearchView - Tạo khung tìm kiếm đơn giản trong Android Studio

Android - Image - Take Photo, Choose Photo from Gallery - Xử lý chụp ảnh, lấy ảnh trong Android Studio