I don't know how I can set the TextView on the right side in my RelativeLayout. Adding gravity to the TextView didn't work. Thanks for helping me.
RelativeLayout relativeLayout = new RelativeLayout(context);
relativeLayout.setGravity(Gravity.CENTER_HORIZONTAL); // center in linearLayout_power_stations_bar
textView = new TextView(context);
textView.setText(String.valueOf(nuke_count));
textView.setTextColor(activity.getResources().getColor(R.color.game_powerStationsBar_textOverIcon));
textView.setTextSize(16);
relativeLayout.addView(textView); // TextView over image
relativeLayout.addView(imageView); // adding image
linearLayout_power_stations_bar.addView(relativeLayout); // adding to other layout
You can set layout parameters to TextView and also set alignment of parent right.
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
textViewsetLayoutParams(params);
Now You can Use this code :)
Related
This question already has answers here:
How to set layout_gravity programmatically?
(20 answers)
Closed 5 years ago.
I need to set layout gravity for Textview which is inside Relative layout,
I do this via xml but how to do it programmatically?
<Relativelayout>
<Textview
android:gravity="left"
android:layout_gravity="center_vertical|right"/>
</Relativelayout>
I need to set gravity and layout_gravity programmatically.
Try this
Use RelativeLayout.LayoutParams
Specifies how a view is positioned within a RelativeLayout. The relative layout containing the view uses the value of these layout parameters to determine where to position the view on the screen. If the view is not contained within a relative layout, these attributes are ignored.
Sample Code
TextView textView=findViewById(R.id.spn);
RelativeLayout.LayoutParams layoutParams =
(RelativeLayout.LayoutParams)textView.getLayoutParams();
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
textView.setLayoutParams(layoutParams);
Try this one:
RelativeLayout relativeLayout=new RelativeLayout(this);
TextView textView=new TextView(this);
RelativeLayout.LayoutParams layoutParams=new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
textView.setLayoutParams(layoutParams);
relativeLayout.addView(textView);
Create a layoutParams object, define your gravity there and set it to your textview.
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT);
params.weight = 1.0f;
params.gravity = Gravity.TOP;
button.setLayoutParams(params);
Replace LinearLayout.LayoutParamswith RelativeLayout.LayoutParams
Textview tx= new Textview(this);
RelativeLayout.LayoutParams params = new Relative.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT);
params.weight = 1.0f;
params.gravity = Gravity.center_vertical;
tx.setLayoutParams(params):
or another option check this link
https://android--code.blogspot.com/2015/05/android-textview-layout-gravity.html
I have an issue with the creation of a new RelativeLayout above an already existing TextView in my app.
My main layout is called contentLayout and inside it I have a TextView called addRoomButton :
RelativeLayout contentLayout = (RelativeLayout) findViewById(R.id.contentLayout);
TextView addRoomButton = (TextView) findViewById(R.id.addRoomText);
I just want to add a new RelativeLayout above my TextView so I wrote this :
RelativeLayout relativeLayout = new RelativeLayout(this);
relativeLayout.setId(R.id.roomRelativeLayout);
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
100
);
rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
rlp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
rlp.addRule(RelativeLayout.ABOVE, addRoomButton.getId());
contentLayout.addView(relativeLayout, rlp);
The RelativeLayout is well created on the top of my contentLayout, but my TextView is not visible. I think it's just behind relativeLayout but I'm not sure...
Is there any solutons to put the TextView below the new RelativeLayout without creating LinearLayout or other Layouts?
Why wouldn't you do it by XML?
layout_below=""
try out your idea with xml. put relativelayout with relativelayout(A) in it and button(B). set A to layout_above B and see there is no success. Set B bellow A and you get the effect you want. So you need to getLayoutParams() of your button and adjust it to be below your newly created layout.
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 am adding an imageView to a Relative layout.It shows up at the beginning of layout
I have two buttons in the layout.
I button to the layout left and one to layout right. Now i want to place these under the button to right.
how can i do that. My code is
ImageView image = new ImageView(this);
image.setImageBitmap(imageBitmap);
layout.addView(image);
Try using the
android:Layout_below="#id/yourButtonId" or
android:LayoutBelow="#id/yourButtonId"
Programmatically it means :
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
rlp.addRule(RelativeLayout.BELOW, yourButtonVariable.getId());
yourRelativeLayout.addView(yourImageView, rlp);