I am facing a scenario, where my main.xml has its main hierarchy written in XML but, the main data is coming from the server, so I need to compose the layout in runtime.
Please, take a look on this screenshot to better understand my scenario.
I shall have a Scroll View and then right under it, a Linear Layout. I retrieve this layout in my java class and create a new linear layout , so that I can create the new TextViews and Buttons like I draw inside LinearLayout 2. The end result is far from I expected and I honestly am not sure how I can accomplish this.
The code for is:
LinearLayout ll = (LinearLayout) findViewById(R.id.linearLayoutOffers); //this is the LinearLayout2 in SS
//Creating the container that will have one business.
LinearLayout containerMain = new LinearLayout(this);
containerMain.setOrientation(LinearLayout.VERTICAL);
containerMain.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
containerMain.setGravity(Gravity.CENTER);
LinearLayout container1 = new LinearLayout(this);
container1.setOrientation(LinearLayout.HORIZONTAL);
container1.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
container1.setGravity(Gravity.CENTER);
container1.setWeightSum(2);
LinearLayout container2 = new LinearLayout(this);
container2.setOrientation(LinearLayout.HORIZONTAL);
container2.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
container2.setGravity(Gravity.CENTER);
container2.setWeightSum(1);
LinearLayout container3 = new LinearLayout(this);
container3.setOrientation(LinearLayout.HORIZONTAL);
container3.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
container3.setGravity(Gravity.CENTER);
container3.setWeightSum(1);
TextView offerTitle = new TextView(this);
offerTitle.setText(pOfferTitle);
offerTitle.setTextSize(14);
//offerTitle.setPadding(10, 0, 10, 0);
offerTitle.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1));
Button redeemButton = new Button(this);
redeemButton.setText("Usar");
redeemButton.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1));
//redeemButton.setPadding(10, 0, 10, 0);
redeemButton.setTextSize(14);
container1.addView(offerTitle);
container1.addView(redeemButton);
TextView mAddress = new TextView(this);
mAddress.setText(pAddress);
mAddress.setTextSize(14);
//mAddress.setPadding(10, 0, 10, 0);
mAddress.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1));
container2.addView(mAddress);
TextView expiryDate = new TextView(this);
expiryDate.setText(pExpiryDate);
expiryDate.setTextSize(14);
//expiryDate.setPadding(10, 0, 10, 0);
offerTitle.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 0.6f));
Button detailsButton = new Button(this);
detailsButton.setText("Detalhes");
detailsButton.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 0.4f));
//detailsButton.setPadding(30, 0, 0, 0);
detailsButton.setTextSize(14);
container3.addView(expiryDate);
container3.addView(detailsButton);
containerMain.addView(container1);
containerMain.addView(container2);
containerMain.addView(container3);
ll.addView(containerMain);
Thanks for your time!
Felipe
I think a far better way would be to use a ListView.
There you can define every row like you want. Also you can add or remove lines at runtime.
A ScrollView collapses easily when you want to add elements to it on runtime.