How to open dynamically created layout?

Avi Tranqyu

I have coded a layout (i.e LinearLayout with toolbar, code below just for an example) and I want to load it in a new window, like I start new intent, what is the correct way of doing this?

protected Void startNewLayout() {       
    LinearLayout myLayout= new LinearLayout(context);
    Toolbar toolbar = new Toolbar(context);
    toolbar.setTitle("Page#1");
    myLayout.addView(toolbar);
    TextView text = new TextView(context);
    text.setText("Hello World!");
    myLayout.addView(text);
    //open myLayout
}
Muhammad Noman

First you have to create the layout, then you have to create LayoutParams. The constructor takes two parameters: width and height. We set both as MATCH_PARENT. After this, setContentView method is invoked. LinearLayout and LayoutParams are passed to it as parameters. This means that LinearLayout with layout-attributes from LayoutParams will be the root element.

You have to again create LayoutParams object with width = wrap_content and height = wrap_content attributes. Now if you assign this object to one of the Views, this View will have width and height defined by its content.

After this we create a Toolbar, update its text, set it previously created LayoutParams object and add it to LinearLayout using addView(View child) method. The same thing will be happened with the TextView.

So, your Final code will be look like this!

protected Void startNewLayout() {   

    LinearLayout myLayout = new LinearLayout(context);
    myLayout.setOrientation(LinearLayout.VERTICAL);

    // creating LayoutParams  
    LayoutParams layoutParam = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); 
    // set LinearLayout as a root element of the screen 
    setContentView(myLayout, layoutParam);    

    LayoutParams lpView = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

    Toolbar toolbar = new Toolbar(context);
    toolbar.setTitle("Page#1");
    toolbar.setLayoutParams(lpView);
    myLayout.addView(toolbar);

    TextView text = new TextView(context);
    text.setText("Hello World!");
    text.setLayoutParams(lpView);
    myLayout.addView(text);


}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to create a pagination for dynamically created table layout using set visible?

From Dev

How to open file browser dialog in a dynamically created form?

From Dev

How to open the same jQuery Dialog from dynamically and non-dynamically created controls

From Dev

Set dynamically created TextView on frame layout

From Dev

How to add the layout dynamically

From Dev

How do I add buttons that are dynamically created in pure python to a kivy layout that is Written in Kivy Language?

From Dev

how to change order of statically created layouts in a linear layout dynamically? (Android Studio)

From Dev

How to dynamically add Image to dynamically created ImageView

From Dev

How to export dynamically created predicate?

From Dev

how to animate divs that are created dynamically?

From Dev

How to retranslate dynamically created widgets?

From Dev

how to remove buttons created dynamically

From Dev

How to retranslate dynamically created widgets?

From Dev

How to hide dynamically created button?

From Dev

How to access dynamically created button?

From Dev

How to dynamically insert a relative layout into a table layout

From Dev

How to change adapter layout dynamically?

From Dev

How to dynamically include a layout in Android?

From Dev

How to change the page layout dynamically

From Dev

retrieve dynamically created layout ID and match it to a list entry

From Dev

How to created this type of layout correctly (containing PHP)

From Dev

How to use spinner created in a fragment layout to an activity

From Dev

How to set background color to a dynamically created view?

From Dev

How to know which QPushButton was pressed if they are created dynamically

From Dev

Javascript: How to hide a div which is dynamically created?

From Dev

How to apply style sheets to dynamically created elements?

From Dev

How to access a dynamically created checkbox in Windows Form

From Dev

How to access or modify dynamically created button

From Dev

How to know that dynamically created script tag was executed?

Related Related

  1. 1

    How to create a pagination for dynamically created table layout using set visible?

  2. 2

    How to open file browser dialog in a dynamically created form?

  3. 3

    How to open the same jQuery Dialog from dynamically and non-dynamically created controls

  4. 4

    Set dynamically created TextView on frame layout

  5. 5

    How to add the layout dynamically

  6. 6

    How do I add buttons that are dynamically created in pure python to a kivy layout that is Written in Kivy Language?

  7. 7

    how to change order of statically created layouts in a linear layout dynamically? (Android Studio)

  8. 8

    How to dynamically add Image to dynamically created ImageView

  9. 9

    How to export dynamically created predicate?

  10. 10

    how to animate divs that are created dynamically?

  11. 11

    How to retranslate dynamically created widgets?

  12. 12

    how to remove buttons created dynamically

  13. 13

    How to retranslate dynamically created widgets?

  14. 14

    How to hide dynamically created button?

  15. 15

    How to access dynamically created button?

  16. 16

    How to dynamically insert a relative layout into a table layout

  17. 17

    How to change adapter layout dynamically?

  18. 18

    How to dynamically include a layout in Android?

  19. 19

    How to change the page layout dynamically

  20. 20

    retrieve dynamically created layout ID and match it to a list entry

  21. 21

    How to created this type of layout correctly (containing PHP)

  22. 22

    How to use spinner created in a fragment layout to an activity

  23. 23

    How to set background color to a dynamically created view?

  24. 24

    How to know which QPushButton was pressed if they are created dynamically

  25. 25

    Javascript: How to hide a div which is dynamically created?

  26. 26

    How to apply style sheets to dynamically created elements?

  27. 27

    How to access a dynamically created checkbox in Windows Form

  28. 28

    How to access or modify dynamically created button

  29. 29

    How to know that dynamically created script tag was executed?

HotTag

Archive