1

i'm facing a problem in my android project . Here I have a imageButton , on click of this I have to inflate a layout file and dynamically add it inside a Relative layout . adding view is working perfectly . but the problem is after adding this child view I need move the image button below the new child layout . but my imageButton is inflating under the child view , maybe I'm missing some LayoutParameters and rules .

here are my code

private void addDestinationViews() {

    View destinationChildView = LayoutInflater.from(getActivity()).inflate(R.layout.destination_city_item, null);
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.MATCH_PARENT,
            RelativeLayout.LayoutParams.MATCH_PARENT);
    params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
    params.addRule(RelativeLayout.ALIGN_TOP, R.id.add_destinationbutton);
    destinationChildView.setLayoutParams(params);

    mInflated_view_holder.addView(destinationChildView);
}

here is the layout i want to inflate

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp">

<TextView
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:background="@drawable/add_destination_button_back"
    android:text="afaf"
    android:textSize="18sp"
    android:paddingLeft="30dp"
    android:padding="10dp"
    android:gravity="start|center"/>

<ImageButton
    android:id="@+id/cancle_destination_imagebutton"
    android:layout_width="10dp"
    android:layout_height="10dp"
    android:background="@drawable/cross_place_icon"
    android:scaleType="centerInside"
    android:layout_alignParentRight="true"
    android:layout_marginRight="10dp"
    android:layout_marginTop="10dp"/>

this is the relative layout which will host the inflated layout

<RelativeLayout
        android:id="@+id/inflated_view_holder_three"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:layout_below="@+id/child_counter_title"
        android:layout_marginTop="15dp">

        <ImageButton
            android:id="@+id/add_destinationbutton"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:src="@drawable/add_place_icon"
            android:layout_alignParentRight="true"
            android:layout_marginRight="5dp"
            android:background="@drawable/add_destination_button_back"/>

    </RelativeLayout>

P.S I forget to mention this child view will be added upon the imageButton press . so the code needs to be dynamic here .

1 Answer 1

1

Use this to host your inflated layout

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <RelativeLayout
            android:id="@+id/inflated_view_holder_three"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:layout_marginTop="15dp">

        </RelativeLayout>

        <ImageButton
            android:id="@+id/add_destinationbutton"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_alignParentRight="true"
            android:layout_below="@+id/inflated_view_holder_three"
            android:layout_marginRight="5dp"
            android:src="@drawable/ic_launcher" />
        <!--android:background="@drawable/add_destination_button_back"-->
    </RelativeLayout>

Let me know in case of any issues

Sign up to request clarification or add additional context in comments.

1 Comment

thnks man , you save me . but other thing I have mentioned earlier is not working . like after inflating the first view if the user press the image button new view needs to inflated upon the oder one . may be need some layout parameters and layout rules runtime , but I have no idea what it should be. @Sandy

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.