0

First and foremost, I am new to Android Development, so this question might sound a little juvenile, but I am not able to get an answer to this. I have an app in which I need to display two different buttons side-by-side. I have specified the tags necessary to this and they run in one emulator but not in the other. Below is my code for button design:

file_name.xml:

<RelativeLayout>

   //some code here

   <RelativeLayout
      android:layout_width="wrap_content"
      android:layout_height="wrap_content">
      <Button
         android:id="@+id/button1"
         android:layout_width="50dp"
         android:layout_height="50dp"
         android:paddingTop="15dp"
         android:layout_alignParentBottom="true"
         android:layout_alignParentLeft="true"
         android:layout_alignParentStart="true"
         android:layout_marginLeft="36dp"
         android:layout_marginStart="36dp"
         android:layout_marginBottom="36dp" />
      <Button
         android:id="@+id/button2"
         android:layout_width="45dp"
         android:layout_height="45dp"
         android:paddingTop="15dp"
         android:layout_alignParentBottom="true"
         android:layout_alignParentLeft="true"
         android:layout_alignParentStart="true"
         android:layout_marginLeft="96dp"
         android:layout_marginStart="36dp"
         android:layout_marginBottom="36dp"
         android:layout_toRightOf="@+id/button1" />
   </RelativeLayout>

Is there something wrong with my tags, because as far as my knowledge goes, layout_toRightOF will place button2 after button1's layout completes. Also I have statically provided marginLeft for both.

Both the buttons appear in one emulator(google play services installed) and not in other(google play services not installed). Is it because of google play services not installed? Or some fault in the design of my buttons?

Any kind of help would be helpful.

Thank You in advance.

Bijon Desai
  • 60
  • 2
  • 10
  • 1
    Question, why not just use a LinearLayout? – cyroxis May 11 '16 at 15:26
  • I tried using LinerLayout but the problem still persists. – Bijon Desai May 11 '16 at 15:30
  • You are using alignParentLeft/Start on both, but you don't actually want to align both to the left/start do you? Also, use `toRightOf="@id/button1"` (get rid of the + in the id) – zgc7009 May 11 '16 at 15:33
  • What is the expected output? What are you seeing? If you are not seeing either it could the be parent layout. – cyroxis May 11 '16 at 15:39
  • thank you guys for the suggestion, I will give it a try. – Bijon Desai May 11 '16 at 15:44
  • @zgc7009 - thank you for the suggestion, removing alignLeft and alignStart solved the problem. Would you mind including this suggestion as an answer so that I can mark it as acceptable. – Bijon Desai May 11 '16 at 15:50

2 Answers2

1
<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <Button
        android:text="1"
        android:id="@+id/button1"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:paddingTop="15dp"
        android:layout_marginLeft="36dp"
        android:layout_marginStart="36dp"
        android:layout_marginBottom="36dp" />
    <Button
        android:text="2"
        android:id="@+id/button2"
        android:layout_width="45dp"
        android:layout_height="45dp"
        android:paddingTop="15dp"
        android:layout_marginLeft="96dp"
        android:layout_marginStart="36dp"
        android:layout_marginBottom="36dp"
        android:layout_toRightOf="@+id/button1" />
</RelativeLayout

Hope this helps

Tuhin Subhra
  • 356
  • 5
  • 17
  • That was a typo from my end. I have changed the code now. I changed the names from my app over here to button1 and button2 to simplify. – Bijon Desai May 11 '16 at 15:25
  • No, I had a typo on stackoverflow, in my code I am using the same id – Bijon Desai May 11 '16 at 15:29
  • No. I had changed the name of buttons here on stackoverflow to button1 and button2 in order to simplify the names. In my app, the id is google_maps and I am using android:layout_toRightOf="@+id/google_maps" Like it is shown in my question right now. – Bijon Desai May 11 '16 at 15:32
  • what are you suggesting? – Bijon Desai May 11 '16 at 15:55
  • 1st use the code and tell me whether its working or not. I am suggesting to remove these lines only android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" – Tuhin Subhra May 11 '16 at 15:57
  • You are most welcome. Please up vote the answer for other users help – Tuhin Subhra May 11 '16 at 16:08
  • i would love to, but I do not have enough reputations to vote up. – Bijon Desai May 11 '16 at 16:10
0

Is it necessary to use relative layout ?? If you need two button side by side you can just use one linear horizontal. So something like this.

<LinearLayout
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:weightSum="10"
  android:orientation="horizontal"
  >
<Button
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="5"
    />
<Button
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="5"
    />
</LinearLayout>

Relative layouts provides you more flexibility but it has to do two measure passes (Reference). So, it's better if you go with linear layout.

Community
  • 1
  • 1
user3354265
  • 777
  • 1
  • 10
  • 26
  • *Better* really depending upon the situation – cyroxis May 11 '16 at 15:39
  • That's true @cyroxis . I just wrote in context with requirement to this question :) – user3354265 May 11 '16 at 15:40
  • @user3354265 - i tried using LinearLayout the same way but it did not help. – Bijon Desai May 11 '16 at 15:51
  • What are you trying to achieve?? Can you provide some picture/ diag? Because the above code should work if you just want 2 buttons side by side like this [BUTTON 1] [BUTTON 2] – user3354265 May 11 '16 at 16:02
  • LinearLayout is working now, I had to make a few changes in my button declaration, viz removing align_ParentLeft and align_ParentStart which solved the problem. Thank You for the suggestion though. I appreciate your time and effort. – Bijon Desai May 11 '16 at 16:04