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.
Related
I have a view, where main heading and its associated sub heading but i don't know length of string, so, i want to create button based on length of string and if not capable on same line then display next line like a picture.
Could you please suggest me, how to display such button and its heading in textview or button in android, because i am new, it sucks me since 1 day, i couldnot get any solution.
Thank you in advance.
You can achieve this by using FolowLayout. Refer the following link for details
FlowLayout
Please check bellow code for creating textview and a button dynamically and show button below to textview. I think it's helpful to you.
RelativeLayout ll = (RelativeLayout) findViewById(R.id.rl);
TextView tv = new TextView(this);
tv.setText("My Text fgfdgd fdgdfg dgdfg dffgdf dfgdfg My Text fgfdgd fdgdfg dgdfg dffgdf dfgdfg My Text fgfdgd fdgdfg dgdfg dffgdf dfgdfg");
tv.setId(1);
LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
ll.addView(tv, lp);
Button myButton = new Button(this);
myButton.setText("Push Me");
LayoutParams lp1 = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lp1.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
lp1.addRule(RelativeLayout.BELOW, tv.getId());
ll.addView(myButton, lp1);
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
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
I declared a RelativeLayout in a xml layout file. Now I want to add Views from code to the existing Layout. I added a Button dynamically to this existing layout as below through code:
rLayout = (RelativeLayout)findViewById(R.id.rlayout);
LayoutParams lprams = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
Button tv1 = new Button(this);
tv1.setText("Hello");
tv1.setLayoutParams(lprams);
tv1.setId(1);
rLayout.addView(tv1);
Now I need to add another Button to the right of the already added Button. I am not able to find the way in which I can add the new one to the right of the previously added button.
Add the rule RelativeLayout.RIGHT_OF for the second added Button LayoutParams:
// first Button
RelativeLayout rLayout = (RelativeLayout) findViewById(R.id.rlayout);
RelativeLayout.LayoutParams lprams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
Button tv1 = new Button(this);
tv1.setText("Hello");
tv1.setLayoutParams(lprams);
tv1.setId(1);
rLayout.addView(tv1);
// second Button
RelativeLayout.LayoutParams newParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
Button tv2 = new Button(this);
tv1.setText("Hello2");
newParams.addRule(RelativeLayout.RIGHT_OF, 1);
tv2.setLayoutParams(newParams);
tv2.setId(2);
rLayout.addView(tv2);
may be this can help you, try it.
rLayout = (RelativeLayout)findViewById(R.id.rlayout);
LayoutParams lprams = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
TableLayout tl=new TableLayout(this);
rLayout.addView(tl);
TableRow tr1=new TableRow(this);
tl.addView(tr1);
Button btn1 = new Button(this);
btn1.setText("Hello");
btn1.setLayoutParams(lprams);
btn1.setId(1);
tr1.addView(btn1);
TextView tv1 = new TextView(this);
tv1.setWidth(40);
tv1.setHeight(LayoutParams.WRAP_CONTENT);
tr1.addView(tv1);
Button btn2 = new Button(this);
btn2.setText("World");
btn2.setLayoutParams(lprams);
btn2.setId(2);
tr1.addView(btn2);
Create another button:
Button tv2 = new Button(this);
tv2.setText("World");
tv2.setLayoutParams(lprams);
tv2.setId(2);
Add add it into your RelativeLayout:
rLayout.addView(tv2);
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