B L Λ C K
B L Λ C K

Reputation: 642

Android XML: Vertical Seekbar in Android with match parent layout isn't working properly

I have seen a different post related to this topic already before posting this question so I can definitely say that this question is not a duplicate. I have a different problem which I couldn't fix or find a solution to it.

Problem: I have created a Vertical Seekbar using new <SeekBar> </SeekBar> view. It does look fine when I see the preview in Android Studio but on the device, the SeekBar doesn't fill the height of the screen. I have rotated the SeekBar view with 90deg and try to set the height:match_parent and width:wrap_content but it isn't working at all as I expected. So I set the height to some hard-coded value and it looked fine. The only problem is, I will have to check the height of the view in which it is getting populated and set the height of the SeekBar problematically, which I want to avoid.

slider.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/slider"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/roundcorner"
    android:backgroundTint="@color/design_default_color_primary"
    android:gravity="center"
    android:layout_gravity="center"
    android:elevation="5dp"
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAlignment="center"
        android:textColor="#fff"
        android:text="@string/RangeValue"
        android:paddingTop="16dp"
        android:paddingBottom="16dp"/>
    <FrameLayout
        android:layout_weight="1"
        android:background="#fff"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <SeekBar
            android:id="@+id/seeker"
            android:layout_width="wrap_content" <!-- Set this to 400dp and it looks fine -->
            android:layout_height="match_parent"
            android:rotation="270"
            android:layout_gravity="center"/>
    </FrameLayout>

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_round_done_24px"
        android:layout_gravity="center"
        android:padding="16dp"  />

</LinearLayout>  

enter image description here

Update

Thank you Reaz for your answer

enter image description here

content_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#fff"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".MainActivity"
    tools:showIn="@layout/app_bar_main">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <RelativeLayout
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <include
                layout="@layout/maingrid"
                android:background="@color/colorAccent"
                android:layout_centerInParent="true"
                android:layout_width="match_parent"
                android:layout_height="match_parent"/>
            <LinearLayout
                android:background="@android:color/transparent"
                android:layout_centerInParent="true"
                android:id="@+id/radarCanvas"
                android:layout_width="300dp"
                android:layout_height="300dp"
                android:orientation="vertical">
            </LinearLayout>
            <include
                android:layout_alignParentBottom="true"
                layout="@layout/biosignals"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" /> 

             <!--- this is where i am including slider layout -->
             <include
                android:layout_alignParentRight="true"
                layout="@layout/slider"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_margin="24dp" /> 

        </RelativeLayout>
    </LinearLayout>
</android.support.constraint.ConstraintLayout> 

Upvotes: 3

Views: 6382

Answers (3)

<RelativeLayout
    android:layout_width="400dp"
    android:layout_height="200dp"
    android:layout_gravity="center">
    <SeekBar
        android:layout_width="400dp"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:maxHeight="4dp"
        android:minHeight="4dp"
        android:progress="50"
        android:progressDrawable="@drawable/custom_seek_bar"
        android:rotation="270"
        android:thumb="@drawable/seekbar_thumb" />
</RelativeLayout>
Use this code, you will get desired result

Upvotes: 0

Reaz Murshed
Reaz Murshed

Reputation: 24211

You need to get rid of your FrameLayout which is limiting the width of the SeekBar. I tried to implement the layout like the following and I think it should fit in your case as well.

Another important thing you should notice that, because of your rotation, the width and height are interchanged in the view for your SeekBar. Hence I made the layout_width="match_parent". Hope that helps!

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/slider"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:elevation="5dp">

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="30dp"
        android:padding="16dp"
        android:text="100"
        android:textAlignment="center" />

    <SeekBar
        android:id="@+id/seeker"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_gravity="center"
        android:rotation="270" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="20dp"
        android:padding="16dp"
        android:src="@android:drawable/star_on" />

</RelativeLayout>

Here is the result that I have got.

enter image description here

Upvotes: 0

Abraham Mathew
Abraham Mathew

Reputation: 2146

You are setting weight inside the Framelayout when the orientation is vertical and at the same time the height of FrameLayout is set to match_parent. If you are setting like this there will be no effect of the weight. Try setting the height to 0dp it will work for sure.

<FrameLayout
    android:layout_weight="1"
    android:background="#fff"
    android:layout_width="match_parent"
    android:layout_height="0dp">

    <SeekBar
        android:id="@+id/seeker"
        android:layout_width="wrap_content" // set this to 400dp and it looks fine
        android:layout_height="match_parent"
        android:rotation="270"
        android:layout_gravity="center"/>

</FrameLayout>

enter image description here

This is my result

Upvotes: 0

Related Questions