7

Now here is my XML code for layout

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

        <RelativeLayout 
                        android:id="@+id/RelativeLayout01" 
                        android:layout_width="wrap_content" 
                        android:layout_height="wrap_content">   
            <ImageView
                        android:layout_width="fill_parent" 
                        android:layout_height="fill_parent" 
                        android:background="@drawable/default1"
                        android:id="@+id/default1"
                        android:layout_gravity="center"
                        android:scaleType="fitXY">
            </ImageView>

            <ImageView
                        android:layout_marginTop="19dp"
                        android:layout_width="180dp" 
                        android:layout_height="45dp" 
                        android:src="@drawable/fc_postyour_best_score_bg"
                        android:id="@+id/postscore"
                        android:layout_alignParentRight="true"
                        android:scaleType="fitXY">
            </ImageView>   

            <ImageButton
                        android:layout_marginTop="22dp"
                        android:layout_width="35dp" 
                        android:layout_height="35dp" 
                        android:background="@drawable/fctwitterup"
                        android:layout_marginLeft="7dp"
                        android:id="@+id/twitter"
                        android:layout_alignRight="@id/postscore"
                        android:scaleType="fitXY">
            </ImageButton>

            <ImageButton
                        android:layout_marginTop="22dp"
                        android:layout_width="35dp" 
                        android:layout_height="35dp" 
                        android:background="@drawable/fcfacebookdown"
                        android:id="@+id/fb"
                        android:layout_toLeftOf="@id/twitter">
            </ImageButton>

            <ImageButton
                        android:layout_width="160dp" 
                        android:layout_height="40dp" 
                        android:background="@drawable/fsremove_ads_down"
                        android:id="@+id/fsremove_ads_down"                     
                        android:layout_below="@id/postscore"
                        android:layout_alignParentRight="true"
                        android:layout_marginBottom="3dp">
            </ImageButton>

            <ToggleButton 
                         android:id="@+id/fsvibrate_on"
                         android:layout_width="135dip"
                         android:layout_height="35dip"
                         android:textOff=""
                         android:textOn=""
                         android:layout_below="@+id/fsremove_ads_down"
                         android:layout_alignParentRight="true"
                         android:background="@drawable/fsvibrate_on">
            </ToggleButton>

            <ImageButton
                        android:layout_width="210dp" 
                        android:layout_height="60dp" 
                        android:background="@drawable/fcplaydown"
                        android:id="@+id/fcplaydown"
                        android:layout_centerInParent="true">
            </ImageButton>

            <ToggleButton 
                        android:id="@+id/fcsoundondown"
                        android:layout_width="35dp"
                        android:layout_height="35dp"
                        android:textOff=""
                        android:textOn=""
                        android:layout_below="@+id/fcplaydown"
                        android:background="@drawable/fcsoundondown">
            </ToggleButton>


        </RelativeLayout>

</LinearLayout>

So My Problem is @+id/fcsoundondown Toggle button I have set to below of @+id/fcplaydown but it is not coming below to the specified button but comes after "@+id/postscore"

1
  • Can you share a screenshot illustrating your problem?
    – Udayan
    Commented Jul 5, 2011 at 13:04

3 Answers 3

12

I don't know to explain very well why this happens, but it's related to your setting the @+id/fcplaydown ImageButton to center in parent, and his parent being a RelativeLayout with wrap_content width and height, the layout just gets confused.

So, change the RelativeLayout settings to match_parent, and it will work.

<RelativeLayout 
    android:id="@+id/RelativeLayout01" 
    android:layout_height="match_parent" 
    android:layout_width="match_parent"> 
3
  • Thanks this works but now I want to put a button right side to @+id/fcsoundondown but it seems at the top of the screen can you guide me??? Commented Jul 5, 2011 at 14:17
  • <ImageButton android:id="@+id/fchelpdown" android:layout_width="115dp" android:layout_height="53dp" android:background="@drawable/fchelpdown" android:layout_toRightOf="@id/fcsoundondown"> </ImageButton> Commented Jul 5, 2011 at 14:20
  • android:layout_toRightOf just set its position on horizontal, you have to add another property to set its position on vertical also, maybe an android:layout_alignTop="@id/fcsoundondown" or android:layout_alignBottom="@id/fcsoundondown", or both, depending on your preferences
    – Adinia
    Commented Jul 5, 2011 at 14:37
0

Layout view id's are used to relate locations and sizes. Now, when a id gets defined, it gets stored in R.java. We all know that. Well, what we dont know is, that views within a RelativeLayout are built out of more than one parse of the layout file. Considering that compilation is a expensive process, and there is a need to optimize on this process, Android needs id to be reset using a + sign when u want to use it, not only when u declare it. Even though its kinda confusing, this is how it is to be used.

-1

@+id should be used only when you are assigning a new ID to the element. While when you are referring an element use @id.

The at-symbol (@) at the beginning of the string indicates that the XML parser should parse and expand the rest of the ID string and identify it as an ID resource. The plus-symbol (+) means that this is a new resource name that must be created and added to our resources (in the R.java file).

Reference: http://developer.android.com/guide/topics/ui/declaring-layout.html#id

1
  • 1
    okk okkk that I have changed but my problem is still there please give any tricks for that Commented Jul 5, 2011 at 13:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.