I am dynamically creating views as show below
public class DynamicLayoutActivity extends Activity
{
private LinearLayout linear_layout,linear_main;
private Button b1,b2,b3;
private LinearLayout.LayoutParams linear_layout_params,button_parameters;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
linear_main = new LinearLayout(this);
linear_layout = new LinearLayout(this);
linear_layout_params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
linear_layout.setOrientation(LinearLayout.VERTICAL);
linear_layout.setGravity(android.view.Gravity.CENTER);
linear_layout.setLayoutParams(linear_layout_params);
linear_layout.setId(3);
// button_parameters = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
b1=new Button(this);
b1.setText("Button1");
b1.setId(31);
b1.setGravity(android.view.Gravity.CENTER);
// b1.setLayoutParams(button_parameters);
b2=new Button(this);
b2.setText("Button1");
b2.setId(32);
b2.setGravity(android.view.Gravity.CENTER);
// b2.setLayoutParams(button_parameters);
b3=new Button(this);
b3.setText("Button1");
b3.setId(33);
b3.setGravity(android.view.Gravity.CENTER);
// b3.setLayoutParams(button_parameters);
linear_layout.addView(b1);
linear_layout.addView(b2);
linear_layout.addView(b3);
linear_main.addView(linear_layout);
setContentView(linear_main);
}
}
Similarly, I am doing the same by declaring everthing in an xml file as show below
<?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:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
Suppose assume i have 100 buttons in the Linear Layout mentioned above then which is the best way to do and which has the best performance related to memory occupied and execution time
If you support a scenario, then please explain me your views why that scenario is best. Such that i can understand the use of Infalting the xml and Creating Views Dynamically
Thanks in advance ..
I will try to answer your question
If you define Layout & controls in xml then you inflate them into code which i think will be bit slower than instantiating views manually.But i dont think we can make out the difference.
1 they allows us to support various Screen Resolutions and Orientation (You need to place main.xml file for each profile in respective folder like ldpi,mdpi,hdpi,xhdpi)
2 You can easily alter your layouts without touching java code.