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

Infalting Relative Layout Android

From Dev

Android complex layout linear and relative

From Dev

Linear Layout Or Relative Layout?

From Dev

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

From Dev

Add Buttons to Relative Layout Dynamically or Extended Linear Layout android

From Dev

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

From Dev

Add a TextView dynamically in a layout ANDROID

From Dev

android relative layout design

From Dev

How to dynamically insert a relative layout into a table layout

From Dev

put buttons in relative layout

From Dev

Add scrollView and highlight on a Linear Layout in Android

From Dev

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

From Dev

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

From Dev

Swipable Linear/Relative Layout

From Dev

Relative layout android loopable

From Dev

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

From Dev

Dynamically List Buttons in Android Studio (Relative Layout)

From Dev

Dynamically add layout to another layout

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

Convert a Relative Layout to a Linear Layout

From Dev

Android relative layout issue

From Dev

Add relative layout background

From Dev

How to add functions to buttons in an Android layout?

From Dev

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

From Dev

How to add relative Layout below some element dynamically

From Dev

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

From Dev

Zooming a layout (Relative Layout) in android

From Dev

Android Relative Layout In TableRow followed by TableRow with Linear Layout

Related Related

  1. 1

    Infalting Relative Layout Android

  2. 2

    Android complex layout linear and relative

  3. 3

    Linear Layout Or Relative Layout?

  4. 4

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

  5. 5

    Add Buttons to Relative Layout Dynamically or Extended Linear Layout android

  6. 6

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

  7. 7

    Add a TextView dynamically in a layout ANDROID

  8. 8

    android relative layout design

  9. 9

    How to dynamically insert a relative layout into a table layout

  10. 10

    put buttons in relative layout

  11. 11

    Add scrollView and highlight on a Linear Layout in Android

  12. 12

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

  13. 13

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

  14. 14

    Swipable Linear/Relative Layout

  15. 15

    Relative layout android loopable

  16. 16

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

  17. 17

    Dynamically List Buttons in Android Studio (Relative Layout)

  18. 18

    Dynamically add layout to another layout

  19. 19

    Zooming a layout (Relative Layout) in android

  20. 20

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

  21. 21

    Convert a Relative Layout to a Linear Layout

  22. 22

    Android relative layout issue

  23. 23

    Add relative layout background

  24. 24

    How to add functions to buttons in an Android layout?

  25. 25

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

  26. 26

    How to add relative Layout below some element dynamically

  27. 27

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

  28. 28

    Zooming a layout (Relative Layout) in android

  29. 29

    Android Relative Layout In TableRow followed by TableRow with Linear Layout

HotTag

Archive