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
Yes you can you have to inflate the UI of
xmlusingLayoutInflaterand then you can get the UI elements fromxml. Having access to UI objects you can add extra layer of UI elements on to them and finally set them tosetContentView()