0

Can someone please tell me why my ImageButton is not showing when I use this layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/main"
    android:orientation="vertical" >


    <ImageButton
        android:id="@+id/continuebutton"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1.02"
        android:background="@android:color/transparent"
        android:src="@drawable/continuebutton"
        android:visibility="visible" />

</RelativeLayout>

When I use a LinearLayout, it shows up just fine. Can anyone please clarify what I am doing wrong?

BlackHatSamurai
  • 23,275
  • 22
  • 95
  • 156

1 Answers1

1

You are trying to add layout_weight to a RelativeLayout - which doesn't make any sense. Relative layout places items relative to one another. They don't have their widths and heights calculated as a whole like what the LinearLayout does, because the flow matters on a Linear layout and not in a Relative layout.

Set the button to either wrap_content or match_parent. If you want weights, nest a LinearLayout inside the RelativeLayout.

Extra info on that here: Percentage width in a RelativeLayout

EDIT LinearLayout looks at the weight AND 0dip values on its orientation when the calculations are made on how to layout its child views.. Say you have 2 items side by side in a horizontal linear-layout... if they are both 0dip width, the width will be calculated based on their weights. The RelativeLayout doesn't do this. It doesn't worry about the flow of items. It places items relative to one another's placing.

HTH

Community
  • 1
  • 1
dineth
  • 9,822
  • 6
  • 32
  • 39
  • That didn't answer my question. Why does my button not display when I use the `RelativeLayout`? `Layout_weights` doesn't make a difference. – BlackHatSamurai Aug 14 '12 at 23:31
  • 1
    It doesn't display because you have set the height to 0dip. The weight is irrelevant here. – dineth Aug 14 '12 at 23:32
  • 1
    LinearLayout looks at the weight AND 0dip values on its orientation. Say you have 2 items side by side in a horizontal linear-layout... if they are both 0dip width, the width will be calculated based on their weights. The RelativeLayout doesn't do this. – dineth Aug 14 '12 at 23:35
  • Thanks! Why didn't you just say that in the first place? Lol... Edit your answer and I'll accept. – BlackHatSamurai Aug 14 '12 at 23:37
  • 1
    Also noticed you are setting orientation:"verticle" on a RelativeLayout... not a valid property. – dineth Aug 14 '12 at 23:37