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.
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);
}
Try this
Display display=getWindowManager().getDefaultDisplay();
int width=display.getWidth();
btn.setWidth(width);
or if you have two buttons, do
Display display=getWindowManager().getDefaultDisplay();
int width=display.getWidth();
btn1.setWidth(width/2);
btn2.seTwidth(width/2);
Update
LinearLayout.LayoutParams paramz = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
0dp, 1.0f);
then btn.setLayoutParams(paramz);
I have the code to add buttons dynamically to linear layout. Just check out whether it helps you https://docs.google.com/document/d/1QA1tAIe601fZT2Dlp1A0qDuLD0U4IqKL3f_Crx1rtkE/edit?usp=sharing
Try this for adding dynamic buttons in relativelayout
RelativeLayout layout = (RelativeLayout)findViewById(R.id.slidingDrawerContent);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT );
for (int i=0; i<4; i++) {
Button btn = new Button(this);
btn.setId(i);
btn.setText("some_text");
// lp.addRule(RelativeLayout.RIGHT_OF, <Id>);
layout.addView(tv2, lp);
}
I would like to suggest use LinearLayout as you asked in your question and give a Weight.
LinearLayout
I Solved my problem by using FlowLayout.
Sample code and references are available here
Its simple to use. Thank you all for help.
Related
Here is my code, I am trying to add few buttons in a row. Its working fine, But these buttons appear too close to each other, I want to apply horizontal margins to buttons. But not able to add. To achieve this I tried to keep button inside a Linearlayout and applied margins to it. But it somehow don't show any buttons, And when I comment out this line- ll.setLayoutParams(lp); Buttons can be seen again, But without any margin. Please let me know how can I make buttons at some distance from each other.
maintable = (TableLayout) findViewById(R.id.maintable);
TableRow tr = new TableRow(this);
for (int z = 0; z < 3; z++) {
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(5, 5, 5, 5);
LinearLayout ll = new LinearLayout(this);
//ll.setLayoutParams(lp);
Button b = new Button(savedLists.this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(150, 150);
params.setMargins(0, 20, 0, 20);
b.setBackgroundResource(R.drawable.circular);
b.setText(Integer.toString(c));
b.setPadding(10,10,10,10);
ll.addView(b,params);
tr.addView(ll);
c++;
}
maintable.addView(tr);
R.drawable.circular is just creating a simple circular button. Please let me know, if I should post that too.
You haven't set any margin for the button here. You are setting it's padding! Here is how you can set the margin for a button!
Button b = new Button(context);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width, height);
params.setMargins(top, left, bottom, right);
yourLinearLayout.addView(b,params);
I am trying to change the width and height of Buttons I dynamically create but my code below is not working. The Buttons stays the same default size. Anyone know why this is not working?
I believe it might have to do with this line:
myButton.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
The problem may be that I am not supposed to create new LinearLayout params each time but rather add it to an existing LayoutParam?
What do you guys think?
public void onClick(View view)
{
LinearLayout linearLayout2 = new LinearLayout(view.getContext());
linearLayout2.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams rlp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
int size = enter_txt.getText().toString().length();
for (int i=0; i<size; i++){
Button myButton = new Button(view.getContext());
myButton.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
myButton.setWidth(100);
myButton.setHeight(100);
myButton.setBackgroundResource(R.drawable.button);
linearLayout2.addView(myButton, rlp);
}
}
Change this code :
myButton.setWidth(100);
myButton.setHeight(100);
To this :
myButton.getLayoutParams().width = 100;
myButton.getLayoutParams().height = 100;
And yes, in your case you can replace :
myButton.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
To :
myButton.setLayoutParams(rlp);
You can use other constructor of the LinearLayout.LayoutParams, which receiving width and height:
myButton.setLayoutParams(new LinearLayout.LayoutParams(100, 100));
I've been working through a bunch of examples on StackOverflow to create a dynamic layout.
I've made some headway but have now stalled on a particular bug.
How do I make this code fill the width of the parent?
LinearLayout masterLayout = (LinearLayout) findViewById(R.id.button_views);
for(int i=0; i<(launchers.length/3+1); i++){
layout[i] = new LinearLayout(getBaseContext());
layout[i].setLayoutParams(new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
layout[i].setWeightSum(3);
for(int i1=0; i1<(3); i1++){
launchers[i1] = new Button(this);
launchers[i1].setText(titles[i1]);
launchers[i1].setLayoutParams(new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
launchers[i1].setOnClickListener(this);
layout[i].addView(launchers[i1]);
}
masterLayout.addView(layout[i]);
The code works MOSTLY as desired, the part i'm missing is the buttons being an even width and being spaced evenly across the full length of the screen.
Figured out that LinearLayout.LayoutParams has an option for weight at the end of it. Here is the final code for a dynamic button layout system that fits the width of the screen.
LinearLayout masterLayout = (LinearLayout) findViewById(R.id.button_views);
for(int i=0; i<(launchers.length/3+1); i++){
layout[i] = new LinearLayout(getBaseContext());
layout[i].setLayoutParams(new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
layout[i].setWeightSum(3);
for(int i1=0; i1<(3); i1++){
launchers[i1] = new Button(this);
launchers[i1].setText(titles[i1]);
launchers[i1].setLayoutParams(new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT, 1.0f));
launchers[i1].setOnClickListener(this);
layout[i].addView(launchers[i1]);
}
masterLayout.addView(layout[i]);
What would help would be a way to allow the buttons to always be square if anyone wants to have a go at that...
I need do design programatically one activity with 6 buttons (same size h and w), and all buttons show a fullsize of activity.
I tried do this: RelativeLayout with buttons and modify for tests.
Show one button!!!
`
setContentView(R.layout.main);
ArrayList<Button> buttons = new ArrayList<Button>();
//RelativeLayout bg = (RelativeLayout) findViewById(R.layout.main);
for (int i = 0; i < 10; i++) {
Button newButton = new Button(this);
newButton.setId(100 + i + 1); // ID of zero will not work
newButton.setText("XXXX");
buttons.add(newButton);
// New layout params for each button
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
if (i > 0) {
// using getId() here in case you change how you assign IDs
int id = buttons.get(i - 1).getId();
lp.addRule(RelativeLayout.RIGHT_OF, id);
}
this.addContentView(newButton, lp);
}`
please look this line if ok: this.addContentView(newButton, lp);
Thanks!!!
mateus
It's a bit not clear from your question whether you want all buttons to distribute evenly across the activity space or each button to take over all activity space?
In first case what you need is a LinearLayout with layout_weight for buttons set to 1.
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
lp.weight = 1;
In the second case I think it's better to use a FrameLayout and just let buttons take over all space by setting
FrameLayout.LayoutParams.FILL_PARENT
to both dimensions.
I am creating some buttons and add them to a linear layout which is defined as
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="#+id/mylayout">
</LinearLayout>
The buttons are created with
for (int i = 0; i < 3; i++)
{
Button btn = new Button(activity);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(1, 1, 1, 1);
btn.setText("Button");
btn.setPadding(0, 0, 0, 0);
mylayout.addView(pv, lp);
}
These buttons always have a margin (about 3px) which I'd like to remove. Is there anything I am missing? If I use a custom view which I created there is no space between them.
Should I set
lp.setMargins(-3, -3, -3, -3);
which removes the margin? Is there a drawback with this?
I do not really think they have a margin but it is related to the background of the button. Probably the default background of the button has a image like this one:
http://developer.android.com/guide/developing/tools/draw9patch.html
which includes fiction margins. Here you can find more info about 9-Patch.
http://developer.android.com/guide/topics/resources/drawable-resource.html
In my opinion if you want to remove the "margins", you should create a different background for the image because the -3 value is not a good workaround (IHMO).
Why are you using Layout Params .. simply add the view after its creation...
This will surely remove the problem
for (int i = 0; i < 3; i++)
{
Button btn = new Button(activity);
btn.setText("Button");
mylayout.addView(pv);
}