I am new to android and I am attempting to programmatically create RelativeLayouts (at least I think that is the only way it will work if my values are dynamic). Essentially I built a webservice to return a list of values and I want to display those values. It could be one value or it could be 500.
In my layout.xml I was able to build a RelativeLayout relatively close to what I wanted but I could only populate it once. Below is my XML:
NoTE: this is the entire XML but he nodes in question are the RelativeLayout.
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#000000" android:orientation="vertical" android:id="@+id/linear"> <Spinner android:id="@+id/band" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#000000" android:dropDownSelector="#000000" android:textColor="#F0F0F0" /> <Spinner android:id="@+id/yearSpinner" android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="#F0F0F0" /> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="?android:attr/listPreferredItemHeight" android:padding="6dip" android:id="@+id/showRelativeLayout"> <ImageView android:id="@+id/icon" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_alignParentBottom="true" android:layout_alignParentTop="true" android:layout_marginRight="6dip" android:src="@drawable/ic_launcher" /> <TextView android:id="@+id/secondLine" android:layout_width="fill_parent" android:layout_height="26dip" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:layout_toRightOf="@id/icon" android:ellipsize="marquee" android:singleLine="true" android:text="Simple application that shows how to use RelativeLayout" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_above="@id/secondLine" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:layout_alignWithParentIfMissing="true" android:layout_toRightOf="@id/icon" android:gravity="center_vertical" android:text="My Application" android:id="@+id/thirdLine" /> </RelativeLayout> </LinearLayout> So I then attempted to build the same in my Activity so that I could iterate through my webservice results. Below is the method that is called numerous times (for each json array returned). Unfortunately the behavior is that there will only be one relativeLayout and it is attempting to take up the remainder of the screen (about 90% for the one row). I am definitely not comprehending something fundamental.
private void processRelativeLayout(int count) { RelativeLayout rl = new RelativeLayout(this); RelativeLayout.LayoutParams lay = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT); rl.setLayoutParams(lay); //Set the Image //TODO: pull from webservice to get the artist image RelativeLayout.LayoutParams imlay = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.MATCH_PARENT); imlay.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); imlay.addRule(RelativeLayout.ALIGN_PARENT_TOP); imlay.setMargins(0, 0, 6, 0); ImageView iv = new ImageView(this); iv.setImageDrawable(getResources().getDrawable(R.drawable.ic_launcher)); iv.setId(1+count); iv.setLayoutParams(imlay); rl.addView(iv); RelativeLayout.LayoutParams tv1lay = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, 26); tv1lay.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); tv1lay.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); tv1lay.addRule(RelativeLayout.RIGHT_OF, 1+count); TextView tv = new TextView(this); tv.setSingleLine(); tv.setEllipsize(TruncateAt.MARQUEE); tv.setTextColor(Color.YELLOW); tv.setText("Simple application that shows how to use RelativeLayout"); tv.setId(2+count); tv.setLayoutParams(tv1lay); rl.addView(tv); RelativeLayout.LayoutParams tv2lay = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT); tv2lay.addRule(RelativeLayout.ALIGN_PARENT_TOP); tv2lay.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); tv2lay.addRule(RelativeLayout.ABOVE, 2+count); tv2lay.addRule(RelativeLayout.RIGHT_OF,1+count); TextView tv2 = new TextView(this); tv2.setTextColor(Color.YELLOW); tv2.setGravity(Gravity.CENTER_VERTICAL); tv2.setText("My Application"); tv2.setId(count+3); tv2.setLayoutParams(tv2lay); rl.addView(tv2); rootLinear.addView(rl); //add the relative layoutll.addView(tv, lay); } I am also attaching a screenshot of my emulator to show you the output. I would have expected to see the result 5 times (since I call processRelativeLayout 5 times).
Apologies for the long post but I wanted to give enough information.
Update: I attempted to post an image but the site says I am not seasoned enough :)