3

Can we Manipulate the Xml layout in our Java code . I mean Lets say i have a layout in Xml and a want to create its object and change its properties (say adding some child before passing it to setcontentveiw() method)

public class IconExtensionLayoutActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    RelativeLayout mcontainer = (RelativeLayout)IconExtensionLayoutActivity.this.findViewById(R.id.mContainerIconExtension);
    ScrollView mScrollView = (ScrollView)IconExtensionLayoutActivity.this.findViewById(R.id.mRootIconExtension);
    RelativeLayout mRow1 = (RelativeLayout)IconExtensionLayoutActivity.this.findViewById(R.id.Root_Layout_Iconset_One_ID);
    RelativeLayout mRow2 = (RelativeLayout)IconExtensionLayoutActivity.this.findViewById(R.id.Root_Layout_Iconset_Two_ID);
    RelativeLayout mRow3 = (RelativeLayout)IconExtensionLayoutActivity.this.findViewById(R.id.Root_Layout_Iconset_Three_ID);

    setContentView(mScrollView.getId());

}

}

and My Xml layout is below.

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mContainerIconExtension"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:scrollbars="none" >

    <RelativeLayout
        android:id="@+id/Root_Layout_Iconset_One_ID"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <ImageView
            android:id="@+id/IconSet_One_ID"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:src="@drawable/ic_launcher" >
        </ImageView>

        <RelativeLayout
            android:id="@+id/IconSet_One_ScrollView_ID"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/IconSet_One_ID"
            android:layout_margin="5dip" >

            <HorizontalScrollView
                android:id="@+id/HorizontalScrollView02"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentTop="true"
                android:scrollbarAlwaysDrawHorizontalTrack="false"
                android:scrollbars="none" >

                <LinearLayout
                    android:id="@+id/LinearLayout03"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" >

                    <ImageView
                        android:id="@+id/ImageView20"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/ic_launcher" >
                    </ImageView>

                    <ImageView
                        android:id="@+id/ImageView19"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/ic_launcher" >
                    </ImageView>

                    <ImageView
                        android:id="@+id/ImageView21"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/ic_launcher" >
                    </ImageView>

                    <ImageView
                        android:id="@+id/ImageView22"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/ic_launcher" >
                    </ImageView>

                    <ImageView
                        android:id="@+id/ImageView23"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/ic_launcher" >
                    </ImageView>

                    <ImageView
                        android:id="@+id/ImageView24"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/ic_launcher" >
                    </ImageView>

                    <ImageView
                        android:id="@+id/ImageView25"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/ic_launcher" >
                    </ImageView>

                    <ImageView
                        android:id="@+id/ImageView31"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/ic_launcher" >
                    </ImageView>

                    <ImageView
                        android:id="@+id/ImageView32"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/ic_launcher" >
                    </ImageView>

                    <ImageView
                        android:id="@+id/ImageView05"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/ic_launcher" >
                    </ImageView>

                    <ImageView
                        android:id="@+id/ImageView02"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/ic_launcher" >
                    </ImageView>

                    <ImageView
                        android:id="@+id/ImageView01"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/ic_launcher" >
                    </ImageView>

                    <ImageView
                        android:id="@+id/ImageView04"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/ic_launcher" >
                    </ImageView>

                    <ImageView
                        android:id="@+id/ImageView03"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/ic_launcher" >
                    </ImageView>

                    <ImageView
                        android:id="@+id/ImageView06"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/ic_launcher" >
                    </ImageView>
                </LinearLayout>
            </HorizontalScrollView>
        </RelativeLayout>

        <ImageView
            android:id="@+id/PURCHASE_BUTTON_ICONSET_1_ID"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_below="@+id/IconSet_One_ScrollView_ID"
            android:src="@drawable/ic_launcher" >
        </ImageView>
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/Root_Layout_Iconset_Two_ID"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <ImageView
            android:id="@+id/ImageView17"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:src="@drawable/ic_launcher" >
        </ImageView>

        <RelativeLayout
            android:id="@+id/RelativeLayout02"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/ImageView17"
            android:layout_margin="5dip" >

            <HorizontalScrollView
                android:id="@+id/Horizontal_Scroller"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:scrollbars="none" >

                <LinearLayout
                    android:id="@+id/LinearLayout01"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" >

                    <ImageView
                        android:id="@+id/ImageView51"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/ic_launcher" >
                    </ImageView>

                    <ImageView
                        android:id="@+id/ImageView50"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/ic_launcher" >
                    </ImageView>

                    <ImageView
                        android:id="@+id/ImageView49"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/ic_launcher" >
                    </ImageView>

                    <ImageView
                        android:id="@+id/ImageView48"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/ic_launcher" >
                    </ImageView>

                    <ImageView
                        android:id="@+id/ImageView47"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/ic_launcher" >
                    </ImageView>

                    <ImageView
                        android:id="@+id/ImageView46"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/ic_launcher" >
                    </ImageView>

                    <ImageView
                        android:id="@+id/ImageView45"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/ic_launcher" >
                    </ImageView>

                    <ImageView
                        android:id="@+id/ImageView44"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/ic_launcher" >
                    </ImageView>

                    <ImageView
                        android:id="@+id/ImageView43"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/ic_launcher" >
                    </ImageView>

                    <ImageView
                        android:id="@+id/ImageView40"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/ic_launcher" >
                    </ImageView>

                    <ImageView
                        android:id="@+id/ImageView39"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/ic_launcher" >
                    </ImageView>

                    <ImageView
                        android:id="@+id/ImageView38"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/ic_launcher" >
                    </ImageView>

                    <ImageView
                        android:id="@+id/ImageView37"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/ic_launcher" >
                    </ImageView>

                    <ImageView
                        android:id="@+id/ImageView36"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/ic_launcher" >
                    </ImageView>

                    <ImageView
                        android:id="@+id/ImageView35"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/ic_launcher" >
                    </ImageView>

                    <ImageView
                        android:id="@+id/ImageView34"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/ic_launcher" >
                    </ImageView>

                    <ImageView
                        android:id="@+id/ImageView33"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/ic_launcher" >
                    </ImageView>

                    <ImageView
                        android:id="@+id/ImageView30"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/ic_launcher" >
                    </ImageView>

                    <ImageView
                        android:id="@+id/ImageView29"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/ic_launcher" >
                    </ImageView>

                    <ImageView
                        android:id="@+id/ImageView28"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/ic_launcher" >
                    </ImageView>

                    <ImageView
                        android:id="@+id/ImageView15"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/ic_launcher" >
                    </ImageView>

                    <ImageView
                        android:id="@+id/ImageView14"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/ic_launcher" >
                    </ImageView>

                    <ImageView
                        android:id="@+id/ImageView11"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/ic_launcher" >
                    </ImageView>
                </LinearLayout>
            </HorizontalScrollView>
        </RelativeLayout>

        <ImageView
            android:id="@+id/ImageView16"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_below="@+id/RelativeLayout02"
            android:src="@drawable/ic_launcher" >
        </ImageView>
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/Root_Layout_Iconset_Three_ID"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <ImageView
            android:id="@+id/ImageView27"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:src="@drawable/ic_launcher" >
        </ImageView>

        <RelativeLayout
            android:id="@+id/RelativeLayout04"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/ImageView27"
            android:layout_margin="5dip" >

            <HorizontalScrollView
                android:id="@+id/HorizontalScrollView01"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentTop="true"
                android:scrollbars="none" >

                <LinearLayout
                    android:id="@+id/LinearLayout02"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" >

                    <ImageView
                        android:id="@+id/ImageView42"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/ic_launcher" >
                    </ImageView>

                    <ImageView
                        android:id="@+id/ImageView41"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/ic_launcher" >
                    </ImageView>

                    <ImageView
                        android:id="@+id/ImageView13"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/ic_launcher" >
                    </ImageView>

                    <ImageView
                        android:id="@+id/ImageView09"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/ic_launcher" >
                    </ImageView>

                    <ImageView
                        android:id="@+id/ImageView08"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/ic_launcher" >
                    </ImageView>

                    <ImageView
                        android:id="@+id/ImageView12"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/ic_launcher" >
                    </ImageView>

                    <ImageView
                        android:id="@+id/ImageView10"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/ic_launcher" >
                    </ImageView>

                    <ImageView
                        android:id="@+id/ImageView18"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/ic_launcher" >
                    </ImageView>
                </LinearLayout>
            </HorizontalScrollView>
        </RelativeLayout>

        <ImageView
            android:id="@+id/ImageView26"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_below="@+id/RelativeLayout04"
            android:src="@drawable/ic_launcher" >
        </ImageView>
    </RelativeLayout>
</LinearLayout>

Regards, Hikmat Khan

3 Answers 3

6

Yes you can you have to inflate the UI of xml using LayoutInflater and then you can get the UI elements from xml. Having access to UI objects you can add extra layer of UI elements on to them and finally set them to setContentView()

LayoutInflater mInflater = LayoutInflater.from(this);  
View contentView = mInflater.inflate(R.layout.main, null); 
LinearLayout root = (LinearLayout) contentView.findViewById(R.id.root);// mContainerIconExtension in your case
root.addView(new TextView(this));
setContentView(root);
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks ingsaurabh Brother for your time and quick response.
@HikmatKhan Glad to hear it helped you
Dear can i all a child of Xml layout multiple time in parent layout. lets say i want to add @+id/IconSet_One_ScrollView_ID in above xml 15 time. then how can i do it. it throws exception of "specified child is already a parent"some thing like that.
for(int i =0 ;i < 15; i++){ RelativeLayout ROW = (RelativeLayout)row.findViewById(R.id.IconSet_One_ScrollView_ID); mContainer.addView(ROW); ROW = null; } I do it like this but it throw exception.
put this line after row.findViewbyid, Row.setId(i.toString());
|
1
  1. Inflate it and get the object reference ifthe layouts in the xml is not a part of your set content view of the main activity.
  2. If the objects are the part of youe xml which is set a s a content view. then by doing findview by id youw will bw getting the references to these objects and you can use apis such as set layout params, getwidth, set width and so on to dynamically modify them before on create.

Comments

1

Sure you can,

View myLayout = View.inflate(mContext, R.layout.my_layout, null); // or use LayoutInflater
//now myLayout is actually the Root element of your layout, ie The LinearLayout.

So LinearLayout root = (LinearLayout)myLayout

and then you have addViewXXX() methods to add any widget dynamically.

1 Comment

Thanks st0le Brother for your quick response and Correct answer. regards Hikmat Khan

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.