Creating Layouts/Views at runtime

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

Creating Layouts/Views at runtime

Postby michael22 » Sun May 03, 2009 10:24 pm

Hey,
i'm currently coding an app, which has to add/remove interface elements on the fly. When one clicks on a Spinneritem, the selected item is added to a container with a delete button to remove it if necessary.

The problem is, that the needed views and the layout are created directly in the javacode using this code:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. LinearLayout layout = new LinearLayout(MainActivity.this);
  3.  
  4.  
  5.  
  6. TextView newTag = new TextView(MainActivity.this);
  7.  
  8. newTag.setText(tags.get((int) rowid));
  9.  
  10.  
  11.  
  12. Button delButton = new Button(MainActivity.this);
  13.  
  14. delButton.append("X");
  15.  
  16.  
  17.  
  18. layout.addView(newTag);
  19.  
  20. layout.addView(delButton);
  21.  
  22.  
  23.  
  24. tagContainer.addView(layout);
Parsed in 0.030 seconds, using GeSHi 1.0.8.4


I'm now wondering how it is possible to add layoutparams to the new views like in an xml file (android:width="fill_parent" etc.) or if it is not the better way to read this layoutpart from an xml and change only the needed parameters. And if yes, how to do it (both ways).

thx
michael22
Freshman
Freshman
 
Posts: 8
Joined: Wed Jan 30, 2008 5:37 pm

Top

Reply

Postby satishkumar_lskin » Mon May 04, 2009 11:46 am

Hi michael,
For any View in Android , u should use setLayoutParams(width, height) to set its dimensions. I suggest u use this method to set a view's dimensions if ur views are to be generated at runtime ( based on a UI event such as a button click ) . This is more flexible than inflating the layouts from XML files ( unless u use hard-coded layouts ) .
Examples :
textView.setLayoutParams(60, 100 );
textView.setLayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT );
satishkumar_lskin
Developer
Developer
 
Posts: 32
Joined: Mon Oct 20, 2008 8:15 am
Location: Coimbatore , TamilNadu, India

Postby michael22 » Mon May 04, 2009 5:03 pm

thank you, thats good to know. just thought it would be a better style to keep all the layouts in the XMLs.
Where can I now change the params like layout_weight or paddingTop. I can not find it in the LayoutParams Object.
michael22
Freshman
Freshman
 
Posts: 8
Joined: Wed Jan 30, 2008 5:37 pm

Reply

Postby satishkumar_lskin » Tue May 05, 2009 6:09 am

For specifying layout_weight , u should use something similar to the following :

float weight = 0.5f;
LinearLayout.LayoutParams layoutParams;
layoutParams = new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, weight);

For padding , u should use :

LinearLayout layout ;
layout.setPadding(10, 10, 20, 30 );

The method signature for setPadding() is :

public void setPadding(int left, int top, int right, int bottom) ;


Thanks
Satish
satishkumar_lskin
Developer
Developer
 
Posts: 32
Joined: Mon Oct 20, 2008 8:15 am
Location: Coimbatore , TamilNadu, India

Postby michael22 » Tue May 05, 2009 10:47 am

thank you. although its very confusing, that the parameters, which are set all in the same way in the xml are set totally different in source. :roll:

but everything works now. thank you!
michael22
Freshman
Freshman
 
Posts: 8
Joined: Wed Jan 30, 2008 5:37 pm

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 8 guests