Search code examples
androidandroid-layoutandroid-seekbar

How to evenly distribute TextView on discrete seekbar ticks in Android?


How to evenly distribute texts above each tick mark in Discrete Seekbar?

This is how I am doing:

 <SeekBar
            android:id="@+id/sb_task_detail_status"
            style="@style/Widget.AppCompat.SeekBar.Discrete"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:max="4"
            android:progress="0" />

        <TableLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:stretchColumns="*"
            android:layout_above="@+id/sb_task_detail_status"
            android:layout_alignStart="@+id/sb_task_detail_status">

            <TableRow
                android:id="@+id/statusRow"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
                <TextView
                    android:id="@+id/tv_task_status_newly_developed"
                    android:layout_height="wrap_content"
                    android:layout_width="wrap_content"
                    android:layout_column="1"
                    android:textColor="@android:color/white"
                    android:textSize="10sp"
                    android:text="Newly Developed"/>
                <TextView
                    android:id="@+id/tv_task_status_developed"
                    android:layout_height="wrap_content"
                    android:layout_width="wrap_content"
                    android:layout_column="2"
                    android:textSize="10sp"
                    android:textColor="@android:color/white"
                    android:text="Developed"/>
                <TextView
                    android:id="@+id/tv_task_status_in_progress"
                    android:layout_height="wrap_content"
                    android:layout_width="wrap_content"
                    android:layout_column="3"
                    android:textSize="10sp"
                    android:textColor="@android:color/white"
                    android:text="In Progress"/>
                <TextView
                    android:id="@+id/tv_task_status_completed"
                    android:layout_height="wrap_content"
                    android:layout_width="wrap_content"
                    android:layout_column="4"
                    android:textSize="10sp"
                    android:textColor="@android:color/white"
                    android:text="Completed"/>
                <TextView
                    android:id="@+id/tv_task_status_submitted"
                    android:layout_height="wrap_content"
                    android:layout_width="wrap_content"
                    android:layout_column="5"
                    android:textSize="10sp"
                    android:textColor="@android:color/white"
                    android:text="Submitted"/>
            </TableRow>

        </TableLayout>

And this is the result I got:

enter image description here

Note that only one TextView will be visible at a time depending on a progress.


Solution

  • Have you tried using layout_width="0dp" and assign similar weights? Like this:

    android:layout_width="0dp"
    android:layout_weight="1"
    

    for each TextView

    EDIT I used this xml and it splits the space evenly

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >
    <SeekBar
        android:id="@+id/sb_task_detail_status"
        style="@style/Widget.AppCompat.SeekBar.Discrete"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:max="4"
        android:progress="0" />
    
    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:stretchColumns="*"
        android:layout_above="@+id/sb_task_detail_status"
        android:layout_alignStart="@+id/sb_task_detail_status">
    
        <TableRow
            android:id="@+id/statusRow"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <TextView
                android:id="@+id/tv_task_status_newly_developed"
                android:layout_height="wrap_content"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_column="0"
                android:textColor="@android:color/white"
                android:textSize="10sp"
                android:text="Newly Developed"/>
            <TextView
                android:id="@+id/tv_task_status_developed"
                android:layout_height="wrap_content"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_column="1"
                android:textSize="10sp"
                android:textColor="@android:color/white"
                android:text="Developed"/>
            <TextView
                android:id="@+id/tv_task_status_in_progress"
                android:layout_height="wrap_content"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_column="2"
                android:textSize="10sp"
                android:textColor="@android:color/white"
                android:text="In Progress"/>
            <TextView
                android:id="@+id/tv_task_status_completed"
                android:layout_height="wrap_content"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_column="3"
                android:textSize="10sp"
                android:textColor="@android:color/white"
                android:text="Completed"/>
            <TextView
                android:id="@+id/tv_task_status_submitted"
                android:layout_height="wrap_content"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_column="4"
                android:textSize="10sp"
                android:textColor="@android:color/white"
                android:text="Submitted"/>
        </TableRow>
    
    </TableLayout>
    

    EDIT 2 For anyone who wants to distribute evenly Texviews on discrete seekbar, here's a nice tutorial that explains all the steps to achieve it.