Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8122195
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T05:45:05+00:00 2026-06-06T05:45:05+00:00

I am dynamically creating views as show below public class DynamicLayoutActivity extends Activity {

  • 0

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 ..

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T05:45:07+00:00Added an answer on June 6, 2026 at 5:45 am

    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.

    • Also you have following advantages if you use xml based layouts.

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm creating dynamically Button views with a context menu. When a context menu item
Dynamically I am creating the linear layout and adding the views to linear layout
I´m dynamically creating an instance of a class with reflection and this works fine,
I'm dynamically creating textfields in as3, and formatting them with the TextFormat class. I'm
I'm creating buttons dynamically ... for(int i=0; i<colSize;i++){ final Button btn = new Button(this);
I'm creating custom class based views for a Django project and faced a design
i am dynamically creating sql query for inserting multiple records in one hit.Suppose no
i am dynamically creating radio using jquery as shown belown. but they value only
I am dynamically creating a RadGrid and adding GridTemplateColumns to it. Those columns have
I am having a JSP page, where i am dynamically creating a Table data.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.