Question

I am trying to define a linearlayout at the bottom of a relative layout programmatically, so I am using ALIGN_PARENT_BOTTOM. But the layout created appearson the top of my screen as if I haven't define its position. Here is java code :

final RelativeLayout rootRelativeLayout = (RelativeLayout) findViewById(R.id.relative_layout);


//find view elements
        image = (ImageView) findViewById(R.id.image_id);

        image.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // Adding the delete icon and text

                Log.i("click", "I clicked ! ");


                // Creating a new LinearLayout that will contain the icons
                 /* <LinearLayout
                 *      android:id="@+id/icons_layout"
                 *      android:layout_width="match_parent"
                 *      android:layout_height="wrap_content"
                 *      android:layout_alignParentBottom="true"
                 *      android:gravity="center"
                 *      android:orientation="horizontal" >
                 *  
                 */
                final LinearLayout iconsLinearLayout = new LinearLayout(getApplicationContext());
                //attach linearLayout to root relativeLayout
                rootRelativeLayout.addView(iconsLinearLayout);      
                iconsLinearLayout.setGravity(Gravity.CENTER_HORIZONTAL);

                final RelativeLayout.LayoutParams illp = new RelativeLayout.LayoutParams(
                        RelativeLayout.LayoutParams.MATCH_PARENT,
                        RelativeLayout.LayoutParams.WRAP_CONTENT
                    );

                illp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);

                 /* Defining the image */
                 /* <ImageView
                 *      android:id="@+id/icon_id"
                 *      android:layout_width="wrap_content"
                 *      android:layout_height="wrap_content"
                 *      android:src="@drawable/icon"
                 *      android:clickable="true" />
                 */
                ImageView icon = new ImageView(context);
                //adding the ImageView in the linearlayout
                iconsLinearLayout.addView(icon);
                icon.setId(R.id.icon_id);
                icon.setImageResource(R.drawable.icon);
                icon.setClickable(true);

                // Defining the ImageView layout parameters.
                /* the image is inside a linear layout --> linearLayout params */
                final LinearLayout.LayoutParams dblp = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.WRAP_CONTENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT
                );

                dblp.weight = 1;
                dblp.gravity = Gravity.CENTER;
                icon.setLayoutParams(dblp);




            }
        });

And here the XML containing the relativeLayout :

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/shelves_layout"
    android:background="@drawable/background" >

    <LinearLayout
        android:id="@+id/layout1"
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:layout_marginLeft="70dp"
        android:layout_marginRight="65dp"
        android:layout_marginTop="@dimen/first_shelf_margin_top"
        android:orientation="horizontal" >
        <ImageView
            android:id="@+id/image_id"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:src="@drawable/image1"
            android:clickable="true"/>

    </LinearLayout>

</RelativeLayout>

Any idea why is this not working properly ?

Was it helpful?

Solution

I finally figured outwhat was going wrong. I wasn't setting the parameters I defined to the layout, and same thing for the image. The followong lines were mising : iconsLinearLayout.setLayoutParams(illp);

Now it works fine !

OTHER TIPS

You should set the layoutparams before adding the view.

iconsLinearLayout.setGravity(Gravity.CENTER_HORIZONTAL);

final RelativeLayout.LayoutParams illp = new RelativeLayout.LayoutParams(
       RelativeLayout.LayoutParams.MATCH_PARENT,
       RelativeLayout.LayoutParams.WRAP_CONTENT
       );

illp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);

rootRelativeLayout.addView(iconsLinearLayout);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top