Put Items in RelativeLayout dynamically - android

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

Related

Applying LayoutParams hides the View

In the code snippet below there's one commented line. When I uncomment that line, then the contents of LinearLayout are not displayed in a TableRow. Without setting the LayoutParams, the row displays both texts. I don't understand this behavior. I know that I can include complex views via xml files, but I'd rather understand what's wrong with this code:
TableLayout tableLayout = (TableLayout) findViewById(R.id.table);
TableRow tableRow = new TableRow(this );
tableRow.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.MATCH_PARENT));
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.MATCH_PARENT);
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
// when I comment out this line, the row only shows the second text.
// linearLayout.setLayoutParams(layoutParams);
TextView textLabel = new TextView(this);
textLabel.setText("inside linear layout");
linearLayout.addView(textLabel);
TextView message = new TextView(this);
message.setText( "inside tablerow");
tableRow.addView(linearLayout);
tableRow.addView(message);
tableLayout.addView(tableRow);
Assuming the question is something like "What's the issue of this? How to resolve this?", here's my answer:
When you are setting LayoutParams to a View, those params would be used by the parent of this View to layout that View appropriately. So in your case what you have done is following:
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(...);
linearLayout.setLayoutParams(layoutParams);
tableRow.addView(linearLayout);
Now, the tableRow is confused, because it expects TableRow.LayoutParams in order to layout the view appropriately, but it suddenly finds out some other layout params. Whereas, had you not explicitly specified params (i.e. when linearLayout.setLayoutParams() is commented out), the default layout params would be generated.
#Override
protected LinearLayout.LayoutParams generateDefaultLayoutParams() {
return new LayoutParams(); // this is TableRow.LayoutParams
}
So, instead of creating LinearLayout.LayoutParams, create TableRow.LayoutParams:
TableRow.LayoutParams layoutParams = new TableRow.LayoutParams(...);
linearLayout.setLayoutParams(layoutParams);
tableRow.addView(linearLayout);

How to align in RelativeLayout properly?

Hi I am trying to design a layout programatically where I have a textview and a edittext and a button underneath. Two rows. First row with TextView. Second row with Edittext and button. Edittext should extend from left to the button. Right now, the Textview appears in the first row, but the second row (with Edittext and Button) is messed. Right now, the EditText doesnt extend all the way to the send button. I tried using Wrap_Content, Fill_parent, Match_Parent but no luck. Here is what I have so far:
RelativeLayout layout = new RelativeLayout(this);
EditText myText = new EditText(this);
Button myBtn = new Button(this);
parameters_layout = new
RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
parameters_layout.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
parameters_layout.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
myText.setId(1);
myText.setLayoutParams(parameters_layout);
layout.addView(myText,parameters_layout);
parameters_layout = new
RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
parameters_layout.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
parameters_layout.addRule(RelativeLayout.RIGHT_OF, myText.getId());
myBtn.setLayoutParams(parameters_layout);
layout.addView(myBtn,parameters_layout);
Thank you any help is appreciated.
EDIT: Clarification I know
parameters_layout = new
RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
can be replaced with numbers. For example:
parameters_layout = new
RelativeLayout.LayoutParams(350,
RelativeLayout.LayoutParams.WRAP_CONTENT);
Is that 350 just pixels, dp, sp? What is it?
What I understand is what you want to implement is one Button on the bottom-right corner of the screen and one extended EditText just left of the button. Am I correct?
If that, try this. I tested this code in on my galaxy nexus and it worked fine.
RelativeLayout layout = new RelativeLayout(this);
EditText myText = new EditText(this);
Button myBtn = new Button(this);
myBtn.setId(1);
RelativeLayout.LayoutParams parameters_layout = new
RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
parameters_layout.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
parameters_layout.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
parameters_layout.addRule(RelativeLayout.LEFT_OF, myBtn.getId());
myText.setLayoutParams(parameters_layout);
layout.addView(myText,parameters_layout);
parameters_layout = new
RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
parameters_layout.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
parameters_layout.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
myBtn.setLayoutParams(parameters_layout);
layout.addView(myBtn,parameters_layout);
One tip is when you use RelativeLayout, first locate the view that the position is fixed (in your case bottom-right cornered button) then locate other views by using relative layout specific parameters. (In your case LEFT_OF).
Another tip is whenever if you need to assign a ID value in programmatically, don't assign any specific value (in your case '1'), instead of it you can define ID as a resource, and then use it w/o any worries about id conflict or some strange behaviors.
There is a document about ID resource, so check it - http://developer.android.com/guide/topics/resources/more-resources.html#Id

Android RelativeLayout ALIGN_LEFT not working

I have a Button and a TextView. I am trying to align the button all the way to the right of the screen and then put a TextView to the left of it, but it is not working. The code below places the button in the correct place, all the way to the right, but when the TextView it is put on the screen it knocks the button off the screen and sort of replaces the TextView right where the button was. I don't understand why it's doing this?
RelativeLayout layout = (RelativeLayout)findViewById(R.id.mainLayout);
Button button = new Button(this);
button.setId(12345);
RelativeLayout.LayoutParams layoutForAlignment = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
layoutForAlignment.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
layout.addView(button, layoutForAlignment);
TextView myTextView = new TextView(getApplicationContext());
myTextView.setText("Testing");
RelativeLayout.LayoutParams layoutForAlignmentX = (RelativeLayout.LayoutParams) button.getLayoutParams();
layoutForAlignmentX.addRule(RelativeLayout.LEFT_OF, button.getId());
layout.addView(myTextView, layoutForAlignmentX);
I think you should create a new RelativeLayoutParams for TextView because you are getting a params with the rule to align right of its parent (params of button).
You also have to give an id to button.
You do:
Button button = new Button(this);
upgradeButton.setId(12345);
and you should give id to a button:
Button button = new Button(this);
button.setId(12345);
Prove this code:
RelativeLayout layout = (RelativeLayout)findViewById(R.id.mainLayout);
Button button = new Button(this);
button.setId(12345);
RelativeLayout.LayoutParams layoutForAlignment = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
layoutForAlignment.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
layout.addView(button, layoutForAlignment);
TextView myTextView = new TextView(getApplicationContext());
myTextView.setText("Testing");
RelativeLayout.LayoutParams layoutForAlignmentX = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
layoutForAlignmentX.addRule(RelativeLayout.LEFT_OF, button.getId());
layout.addView(myTextView, layoutForAlignmentX);
Sorry if you don't understand something, my english isn't very good....
I think that your problem is this line:
layoutForAlignmentX.addRule(RelativeLayout.LEFT_OF, myTextView.getId());
It should be:
layoutForAlignmentX.addRule(RelativeLayout.LEFT_OF, button.getId());
This way you are setting your textview at the left of your button.

Set ScrollView + Fixed Button Programatically on Android with setText.

I' m at this point stuck : I want to have a scrollview + a fixed button at the bottom, but Programatically Way ! I can' t go with XML for some technical reason.
Actually i have this :
//Is it really usefull Relative View?
RelativeLayout layout = new RelativeLayout(this);
ScrollView sv = new ScrollView(this);
sv.setId(2);
// What is it? RelativeLayout.LayoutParams?
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, sv.getId());
sv.setLayoutParams(new ViewGroup.LayoutParams(480, 800));
layout.addView(saveButton, lp);
layout.addView(sv);
I do the first 3 page Google on "fixed button and scrollview Android programatically"
Im beginner on Android, so, don' t hesitate to comment on my code some hints ;)
Thx for your help.
try this
LinearLayout layout = new LinearLayout(this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setLayoutParams(lp);
ScrollView scroll = new ScrollView(this);
LinearLayout.LayoutParams slp = new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT,0, 1.0f);
scroll.setLayoutParams(slp);
Button btn = new Button(this);
ViewGroup.LayoutParams blp = new ViewGroup.LayoutParams(
LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
btn.setLayoutParams(blp);
btn.setText("Click Me");
layout.addView(scroll);
layout.addView(btn);
setContentView(layout);
If you are trying to create a view where the upper portion of the screen is a scrolling list, and the bottom portion is a button, put the scrollview inside a linearlayout, and put the button in the linearlayout below the scrollview.
http://developer.android.com/reference/android/widget/LinearLayout.html

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