Add Buttons to Relative Layout Dynamically or Extended Linear Layout android

AndyN

I want to add buttons to relative layout dynamically as per button and screen width as shown in image. Number of buttons is not fixed and width of button depends on the text which is set to the button. enter image description here

I tried to achieve this using below code but that to not working fine as per my requirement. Can anyone please help me ? Please help me through this.

RelativeLayout layout = (RelativeLayout) findViewById(R.id.genre_layout);
                    for(int i=0; i < genreList.size(); i++){

                        Button btn = new Button(MovieDetailsActivity.this);

                        btn.setText(genreList.get(i).getGenreName());
                        btn.setPadding(15, 5, 15, 5);
                        btn.setBackgroundColor(Color.parseColor("#FFFFFF"));
                        btn.setBackgroundColor(Color.parseColor("#80333333"));


                        LayoutParams param = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
                        param.setMargins(5, 5, 5, 5);

                        if (i != 0){

                            int prevId = genreList.get(i).getGenreId();
                            param.addRule(RelativeLayout.RIGHT_OF, prevId);
                        }
                        btn.setLayoutParams(param);
                        btn.setId(genreList.get(i).getGenreId());

                        layout.addView(btn);

                    }
AndyN

I Solved my problem by using FlowLayout. Sample code and references are available here Its simple to use. Thank you all for help.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Add Buttons to Relative Layout Dynamically or Extended Linear Layout android

From Dev

Dynamically List Buttons in Android Studio (Relative Layout)

From Dev

Android complex layout linear and relative

From Dev

Linear Layout Or Relative Layout?

From Dev

Android: How to dynamically add a "Custom View" into a linear layout

From Dev

Convert a Relative Layout to a Linear Layout

From Dev

Swipable Linear/Relative Layout

From Dev

Android Relative Layout In TableRow followed by TableRow with Linear Layout

From Dev

Is it possible to put some Buttons in a Linear or Relative Layout in variable rows?

From Dev

put buttons in relative layout

From Dev

Add a TextView dynamically in a layout ANDROID

From Dev

Set default layout for new activity to "Linear layout" instead of "Relative layout" in Android Studio

From Dev

Add scrollView and highlight on a Linear Layout in Android

From Dev

Zooming a layout (Relative Layout) in android

From Dev

Zooming a layout (Relative Layout) in android

From Dev

How to arrange two linear layouts inside a relative layout in android?

From Dev

How to get programmatically width and height of Relative - Linear layout in android?

From Dev

Add relative layout background

From Dev

How to add functions to buttons in an Android layout?

From Dev

How do I dynamically add buttons using Java in a given XML layout file in android studio?

From Dev

How to dynamically insert a relative layout into a table layout

From Dev

Dynamically add layout to another layout

From Dev

How to add relative Layout below some element dynamically

From Dev

How to set a linear layout of buttons at the top of an image (layouts in android)?

From Dev

how can i add and remove element dynamically from linear layout?

From Dev

android relative layout design

From Dev

Infalting Relative Layout Android

From Dev

Relative layout android loopable

From Dev

Android relative layout issue

Related Related

  1. 1

    Add Buttons to Relative Layout Dynamically or Extended Linear Layout android

  2. 2

    Dynamically List Buttons in Android Studio (Relative Layout)

  3. 3

    Android complex layout linear and relative

  4. 4

    Linear Layout Or Relative Layout?

  5. 5

    Android: How to dynamically add a "Custom View" into a linear layout

  6. 6

    Convert a Relative Layout to a Linear Layout

  7. 7

    Swipable Linear/Relative Layout

  8. 8

    Android Relative Layout In TableRow followed by TableRow with Linear Layout

  9. 9

    Is it possible to put some Buttons in a Linear or Relative Layout in variable rows?

  10. 10

    put buttons in relative layout

  11. 11

    Add a TextView dynamically in a layout ANDROID

  12. 12

    Set default layout for new activity to "Linear layout" instead of "Relative layout" in Android Studio

  13. 13

    Add scrollView and highlight on a Linear Layout in Android

  14. 14

    Zooming a layout (Relative Layout) in android

  15. 15

    Zooming a layout (Relative Layout) in android

  16. 16

    How to arrange two linear layouts inside a relative layout in android?

  17. 17

    How to get programmatically width and height of Relative - Linear layout in android?

  18. 18

    Add relative layout background

  19. 19

    How to add functions to buttons in an Android layout?

  20. 20

    How do I dynamically add buttons using Java in a given XML layout file in android studio?

  21. 21

    How to dynamically insert a relative layout into a table layout

  22. 22

    Dynamically add layout to another layout

  23. 23

    How to add relative Layout below some element dynamically

  24. 24

    How to set a linear layout of buttons at the top of an image (layouts in android)?

  25. 25

    how can i add and remove element dynamically from linear layout?

  26. 26

    android relative layout design

  27. 27

    Infalting Relative Layout Android

  28. 28

    Relative layout android loopable

  29. 29

    Android relative layout issue

HotTag

Archive