0

UPDATING the question: I know that LinearLayout is much better and already implemented it with linear. I have an educational task that requires RelativeLaouyt implementation. Please, don't comment about Linear layout, this is not the purpose of my question.

I know that it much easier in LinearLayout, but I have to implement it with RelativeLayout only. I have 3 buttons in my xml file and want them on the same line and to have the same width that equals to 1/3 of the screen.

(I found here a nice solution for 2 buttons using a "fake" view. But it doesn't work for 3 buttons.)

Any ideas? (relative layout only!)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:layout_margin="10dp">      
<Button
            android:id="@+id/bottom_left"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:text="Left"/>
        <Button
            android:id="@+id/bottom_center"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:layout_centerInParent="true"
            android:text="Center"/>
        <Button
            android:id="@+id/bottom_right"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:layout_alignParentRight="true"
            android:text="Right"/>
</RelativeLayout>
Community
  • 1
  • 1
Natalie
  • 163
  • 1
  • 3
  • 15
  • You could always add them programmatically and calculate the width there. – Rob Avery IV Mar 22 '17 at 14:50
  • While relativ eis better than linear layout. you could do a linear and then give each button a even weight – Phil3992 Mar 22 '17 at 14:51
  • The reason is educational purpose only. I need to implement the same widget with different layouts. – Natalie Mar 22 '17 at 14:53
  • Sorry natalie. I dont see any educational purpuse in this. As said, each Layout has its own purpose.if you still want to use `RelativeLayout` you have to write additional code to handle widths at runtime. or use `PercentRelativeLayout` and give equal width percent for all buttons. And also, if it is educational, you must be trying it yourself instead of posting it on Stack Overflow. – Mohammed Atif Mar 22 '17 at 14:56
  • It's not my homework...Just trying to learn if there any way to use one layout. You don't have to answer if you don't know the answer. – Natalie Mar 22 '17 at 15:05
  • I already answered. **there is no direct way of achieving wat you are trying** you have to either create a custom view extending relative layout, or handle layout changes directly in code. – Mohammed Atif Mar 22 '17 at 15:07
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/138745/discussion-between-natalie-and-mohammed-atif). – Natalie Mar 22 '17 at 15:27
  • @MohammedAtif In case you still don't see any purpose of using a RelativeLayout for this, how about the fact that nested LinearLayout weights are bad for performance and you might have to nest a RelativeLayout inside a LinearLayout with weights to avoid the performance hit, in which case the OP's question is very valid and practical? And before you say it, it is the year 2018 and the ContraintLayout can do all this very easily with no performance compromise. – PrashanD Oct 02 '18 at 06:54
  • @PrashanD First thing, I don't understand the involvement of nested linear layout in this case, as per OPs requirement, it is supposed to be simple linear layout. And yes as you mentioned, chaining is the solution for most of such layout implementations. I would still discourage the use of relative layout for this use case. – Mohammed Atif Oct 02 '18 at 07:00
  • @MohammedAtif Someone looking to avoid nesting a LinearLayout inside a weighted LinearLayout by nesting a RelativeLayout inside a weighted LinearLayout should find this question extremely valid, in which case there is significant practical purpose as well as _educational purpose_ to the OP's question. That is the whole purpose of SO. It is my opinion too that a RelativeLayout should not be used in this case but that is beside the point of providing an answer to this very valid question. – PrashanD Oct 02 '18 at 08:00
  • I would still not find this question valid. I have designed extremely complex UIs so far, without having to use unnecessary nesting or irrelevant UI component. @PrashanD . If you still feel that this question is valid, you are open to provide your answer for the same which is reliable and efficient using Relative Layout. – Mohammed Atif Oct 02 '18 at 08:31

1 Answers1

1

With reference to your link provided for 2 buttons, you can also use the same for 3 buttons using Relative layout. The answer could be, set 3 different properties of relative layout to each view. For instance, Button 1: alignParentLeft true Button 2: centreInParent true Button 3: alignParentRight true

You are partially right, centerInParent is the property you need. Thanks.

TechInsect
  • 124
  • 1
  • 2