Search code examples
androidandroid-layoutseekbarandroid-seekbar

Align Seekbar to right of ImageView (Icon)


Layout problem

As you can see, the seek bar is a little bit to high, thus not looking correctly aligned to the icon. Is there a simple way to fix it? I set the fixed size on the icon because I want it to look larger than 24dp, similar to Androids audio settings.

<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".ui.MainActivity">

    <SeekBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/seekBar"
        android:layout_below="@+id/textView"
        android:layout_toRightOf="@+id/imgRing"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_alignBaseline="@+id/imgRing"
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Ring volume"
        android:id="@+id/textView"

        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
     />

    <ImageView
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:id="@+id/imgRing"
        android:background="@drawable/ic_notifications_black_24dp"
        android:layout_below="@+id/textView"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

</RelativeLayout>

Solution

  • just update your Seekbar to the same height as your ImageView:

    <?xml version="1.0" encoding="utf-8"?>
        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context=".ui.MainActivity">
    
        <!-- SeekBar height the same size as your ImageView-->
        <SeekBar
            android:layout_width="wrap_content"
            android:layout_height="30dp"
            android:id="@+id/seekBar"
            android:layout_below="@+id/textView"
            android:layout_toRightOf="@+id/imgRing"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:layout_alignBaseline="@+id/imgRing"
            />
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="Ring volume"
            android:id="@+id/textView"
    
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
         />
    
        <ImageView
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:id="@+id/imgRing"
            android:background="@drawable/ic_notifications_black_24dp"
            android:layout_below="@+id/textView"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />
    
    </RelativeLayout>