Search code examples
androiddensity-independent-pixelandroid-percentrelativelayout

Use dp width in PercentRelativeLayout


When I try to add a view without "widthPercent" or "heightPercent" in a PercentRelativeLayout, the view doesn't show up.

The reason why I want this is to add a divider between the 2 elements of my PercentRelativeLayout with a width of 1dp.

The layout I use is this :

<android.support.percent.PercentRelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout android:id="@+id/fragment_list_container"
        app:layout_widthPercent="30%"
        android:layout_height="match_parent"

        android:layout_toLeftOf="@+id/divider"
        android:layout_toStartOf="@+id/divider"/>

     <View android:id="@id/divider"
        android:layout_width="1dp"
        android:layout_height="match_parent"
        android:background="@drawable/detailed_list_divider"

        android:layout_toLeftOf="@+id/fragment_details_container"
        android:layout_toStartOf="@+id/fragment_details_container"/>

     <FrameLayout android:id="@id/fragment_details_container"
         app:layout_widthPercent="70%"
         android:layout_height="match_parent"

         android:layout_alignParentRight="true"
         android:layout_alignParentEnd="true"

         android:paddingBottom="@dimen/base_activity_padding"
         android:paddingRight="@dimen/base_activity_padding"
         android:paddingLeft="@dimen/base_activity_padding" />

</android.support.percent.PercentRelativeLayout>

And if I set "layout_widthPercent" to 1% at my divider it will show up. But I want it to take only 1dp. Is their a way to achieve this?

Thank's !


Solution

  • I found one way to achieve my goal but I don't really like it (not very clean) so I post it here and if no one gives me any better solution I will add it as an answer.

    What I've done is to add a RelativeLayout with a widthPercent of 1% that containing my view of 1dp. Like this :

    <android.support.percent.PercentRelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <FrameLayout android:id="@+id/fragment_list_container"
            app:layout_widthPercent="30%"
            android:layout_height="match_parent"
    
            android:layout_toLeftOf="@+id/divider"
            android:layout_toStartOf="@+id/divider"/>
    
        <RelativeLayout android:id="@id/divider"
            app:layout_widthPercent="1%"
            android:layout_height="match_parent"
    
            android:layout_toLeftOf="@+id/fragment_details_container"
            android:layout_toStartOf="@+id/fragment_details_container">
    
            <View
                android:layout_width="1dp"
                android:layout_height="match_parent"
                android:background="@drawable/detailed_list_divider" />
    
         </RelativeLayout>
    
         <FrameLayout android:id="@id/fragment_details_container"
             app:layout_widthPercent="70%"
             android:layout_height="match_parent"
    
             android:layout_alignParentRight="true"
             android:layout_alignParentEnd="true"
    
             android:paddingBottom="@dimen/base_activity_padding"
             android:paddingRight="@dimen/base_activity_padding"
             android:paddingLeft="@dimen/base_activity_padding" />
    
    </android.support.percent.PercentRelativeLayout>
    

    Let me know if you have any better idea.