Android Different Ways To Add LayoutParams - android

I noticed there are two ways to add LayoutParams programmatically to any view and am curious to ask do they have different meanings as well.
Example 1
In this example, setting LayoutParams directly to the button.
LinearLayout parent = new LinearLayout(this);
Button btnNew = new Button(this);
LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
button.setLayoutParams(params);
parent.addView(btnNew);
Example 2
In this example adding layoutparams to button when it's being added to the parent view.
LinearLayout parent = new LinearLayout(this);
Button btnNew = new Button(this);
LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
parent.addView(btnNew, params);
What's different in both?

There is no difference. If you check the android source code, it specifies that if no layout params are specified, the defualts params of the view group are set. When you use the method
addView(child,params);
It calls another method named addViewInner which just sets the params object to the child object.
The source code from the ViewGroup.java can be seen here

Related

Defining a Linear Layout progmatically

I am trying to dynamically create a linear layout with a dynamic number of buttons based on certain parameters. So far I have some code that compiles but when it runs it does not display anything.
public void displayMenu()
{
LinearLayout lin = new LinearLayout(CategoryMenu.this);
lin.setOrientation(LinearLayout.VERTICAL);
lin.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
ArrayList<Button> btnList= new ArrayList<Button>();
//Test Button
Button btn1= new Button(this);
btn1.setText("Dylan");
lin.addView(btn1);
for (int i =0 ; i < 5; i++){
btnList.add(new Button(this));
btnList.get(i).setText("Hello:"+i);
lin.addView(btnList.get(i));
Log.i("CategoryMenu","Adding btn to view");
}
}
What am i doing incorrectly? I'm sure that i'm missing some thing silly here.
you have to add lin to the current hierarchy of view, or creating a new one calling setContentView(lin);
It seems that you're not adding that linear layout to a viewgroup parent.
After all your code you should add something like
myParentViewGroup.add(lin);
Before lin.addView(btn1);
You should define layoutparams for Button like:
// Defining the layout parameters of the Button
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
// Setting the parameters on the Button
tv.setLayoutParams(lp);
You have to add lin too the layout.
At present you have just created a Linearlayout, but you have not attached it to any layout.
LinearLayout lin = new LinearLayout(CategoryMenu.this);
lin.setOrientation(LinearLayout.VERTICAL);
lin.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
ArrayList<Button> btnList= new ArrayList<Button>();

Put Items in RelativeLayout dynamically

I have done some research, but the answer i found does not work for me. Here is some part of my code. the R.id.relative is the id of the relativelayout in the xml file
RelativeLayout RL = (RelativeLayout) findViewById(R.id.relative);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
TextView title = new TextView(this);
title.setText(" History ");
title.setId(99099);
title.setTextSize(30);
title.setGravity(Gravity.CENTER);
params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
title.setLayoutParams(params);
RL.addView(title);
RelativeLayout.LayoutParams test_params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
Button test = new Button(this);
test.setText(" Back ");
test_params.addRule(RelativeLayout.BELOW,99099);
test.setId(199291);
test.setGravity(Gravity.CENTER);
test.setLayoutParams(test_params);
RL.addView(test);
RelativeLayout.LayoutParams test_params2 = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
Button test2 = new Button(this);
test2.setText(" Clear ");
test_params2.addRule(RelativeLayout.BELOW,test.getId());
test2.setGravity(Gravity.CENTER);
test.setLayoutParams(test_params2);
RL.addView(test2);
all 3 items did show up, but they stack together. I can't get them below another.
Could anyone help ?
From what I've been able to find out, you have to add the view using LayoutParams. Here's an example:
LinearLayout linearLayout = new LinearLayout(this);
RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
relativeParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
parentView.addView(linearLayout, relativeParams);
And to relatively position your items programmatically you have to assign ids to them, this stops them from 'overlapping'.
TextView tv1 = new TextView(this);
tv1.setId(1);
TextView tv2 = new TextView(this);
tv2.setId(2);
Then addRule(RelativeLayout.RIGHT_OF, tv1.getId());
Change test.setLayoutParams(test_params2); to test2.setLayoutParams(test_params2);
Explanation: You have inadvertently set the layout params of test a second time, with params that instruct it to be below itself, which I guess just puts it top-left (default placement in a RelativeLayout). Since you never give test2 any layout params, it also gets default placement. So everything is at the top and thus appear atop each other.
Incidentally, if you just want them arranged linearly, why not use a vertical LinearLayout?
The rule you have added is causing your view to be stacked together.If you need to add it should be used by the following rule.
test_params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, 1);
Next, add the view to your RelativeLayout with your LayoutParams:
RL.addView(yourAdView, rLParams);
same goes for the each cases.try to run.It will solve your problem

Dynamically adding a custom layout one below other

I have to add a custom layout in a relative layout on a button click.
Again on another click add that same layout with another values above the previous inflated layout.
and it will continue like this.
I don't want to use list view.
I can add dynamically my custom layout but how to place it above the previous added.
on click of ADD button a new row will be added to my relative layout in xml, like ROW 1, ROW 2, and this will continue with ROW 3, ROW 4 etc.
you can use the addRule method of RelativeLayout.LayoutParams class. For example:
RelativeLayout.Layoutparams params = (RelativeLayout.LayoutParams)button.getLayoutParams();
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
params.addRule(RelativeLayout.LEFT_OF, R.id.id_to_be_left_of);
button.setLayoutParams(params);
Doing this, you can set programmatically all params you would set in your layout xml file.
on button click event add this code:
LinearLayout linearLayout=(LinearLayout) findViewById(R.id.layout);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
LinearLayout layout=new LinearLayout(getApplicationContext());
LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setLayoutParams(params);
Button button1=new Button(getApplicationContext());
button1.setLayoutParams(params1);
button1.setText("button");
layout.addView(button1);
linearLayout.addView(layout);
Define the layout having id #+id/layout and orientation Vertical in XML file.You will find the button layout being added on every click event.

Creating a button in Java, causes getLayoutParams to return null

I need to create a button in Java. Below is my code:
Button b = new Button(MyClass.this);
b.requestLayout();
LayoutParams lp = b.getLayoutParams();
lp.height = LayoutParams.WRAP_CONTENT;
lp.width = LayoutParams.WRAP_CONTENT;
b.setLayoutParams(lp);
b.setText("bla");
b.setTextSize(16);
b.setOnClickListener(myListener);
I then add this button to the bottom of a ListView:
getListView().addFooterView(b);
However this crashes, because getLayoutParams returns null.
Even if I create new LayoutParams instead of getLayoutParams, i.e.:
Button b = new Button(MyClass.this);
LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
b.setLayoutParams(lp);
b.setText("bla");
b.setTextSize(16);
b.setOnClickListener(myListener);
then the application crashes. Without setLayoutParams, it runs fine, but my button is not sized properly.
How can I size my button?
You have to add this button to a view if you want to get LayoutParams from button. Or just create new LayoutParams and set it.
It's returning null because when you programmatically create a widget, it has no layout params! (Until you add it to a view, then it receives defaults from the LayoutManager)
edit: above is referring to line 3 of your code
Set them like this:
TextView moneyTV = new TextView(this);
LayoutParams lp1 = new LayoutParams(HeightParamHere, WidthParamHere, WeightParamHere);
moneyTV.setLayoutParams(lp1);
Edit2: here's some readybake replacement code.
Button b = new Button(MyClass.this);
b.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
b.setText("bla");
b.setTextSize(16);
b.setOnClickListener(myListener);
Assuming you have defined myListener, this should work.
Because I'm adding this button via ListView::addFooterView, it turns out I had to use the ListView type.
b.setLayoutParams(new ListView.LayoutParams(ListView.LayoutParams.WRAP_CONTENT, ListView.LayoutParams.WRAP_CONTENT));
Using this instead of just LayoutParams resolves my crash. Hope this helps others.

How i detemine Button (x,y) cords in Layout?

I Have this simple Code:
layout = new RelativeLayout(this);
bottom = new LinearLayout(this);
Button close = new Button(this);
close.setText("Close");
bottom.addView(close);
layout.addView(bottom);
setContentView(layout);
How can i put my button at the bottom of the window i prefer to determine the x,y cords but any solution will be good.
since the absoluteLayout is deprecated i tried using the Relativelayout as the api adviced with no succes.
i'm not using XML at all is that a problem ?
You can set LayoutParametrs (RelativeLayout.LayoutParams) to your elements. And don't forget to set LayoutParams to parent view.
RelativeLayout.LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
lp.addRule(ALIGN_PARENT_BOTTOM);
button.setLayoutParams(lp)

Categories

Resources