How to set layout_gravity for Textview inside Relative layout [duplicate] - android

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

Related

Android - positioning TextView in layout programmatically

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 :)

Why is this ImageView not aligning properly with its parent?

I'm building an application with the following layout:
A RelativeLayout container has a ImageView child as its background and two other RelativeLayouts as the actual content of the page.
The rules of these views are set up as follows:
// Define rules and params for views in the container
ImageView backgroundImg = new ImageView(this);
RelativeLayout.LayoutParams lpImg = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
lpImg.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
backgroundImg.setImageResource(R.drawable.baggrund_smartphones_retina);
backgroundImg.setLayoutParams(lpImg);
RelativeLayout mainLayout = (RelativeLayout)findViewById(R.id.fragment_main_layout);
RelativeLayout storeLayout = (RelativeLayout)findViewById(R.id.fragment_store_layout);
RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(screenWidth, LayoutParams.MATCH_PARENT);
RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(screenWidth, LayoutParams.MATCH_PARENT);
lp1.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
lp2.addRule(RelativeLayout.RIGHT_OF, R.id.fragment_main_layout);
mainLayout.setLayoutParams(lp1);
storeLayout.setLayoutParams(lp2);
root.removeAllViews();
//Add background first to ensure its in the back
snapView.addInfoPage(backgroundImg);
snapView.addInfoPage(mainLayout);
Now here is my problem. But my RelativeLayouts are aligning properly to their parent, but the ImageView is not. Instead the imageview is places like this:
What am I doing wrong?
may be your image dimensions is smaller than the screen dimensions of your parentLayout .
So if you want the image to Fit all the screen use the attribut android:scaleType in your imageView Tag via xml :
<ImageView android:id="#+id/img"
blabla
blabla
android:scaleType="fitXY" />
or programmatically :
imgView.setScaleType(ScaleType.FIT_XY);
change like this
RelativeLayout.LayoutParams lpImg = new RelativeLayout.LayoutParams(screenWidth, LayoutParams.MATCH_PARENT);
In place of
RelativeLayout.LayoutParams lpImg = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
And fix imageview like this
backgroundImg.setLayoutParams(ScaleType.FIT_XY);

fixed button on the linearlayout

A have the button on the bottom of linear layout. The other content in this layout may change its height. Due to this the button moves up. How to make the button to be "fixed" on the layout?
you have to use RelativeLayout....
dynamic of your code
RelativeLayout rl = new RelativeLayout(this);
LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
rl.setLayoutParams(params);
Button button = new Button(this);
button.setText("Previous");
LayoutParams params1 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params1.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
button.setLayoutParams(params1);
rl.addView(button);

Programmatically adding three buttons in line in a LinearLayout

I'm creating a LinearLayout programmatically and I'm adding to this layout three buttons, but they're showed one on top of the other.
How I can show the buttons in line?
Reading around I probably understood that I need to set up a LayoutParams but I didn't figured out how..
I've tried with this but it didn't did the trick..
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT
);
ll.addView(b1, layoutParams);
ll.addView(b2, layoutParams);
ll.addView(b3, layoutParams);
Thanks for any help!
EDIT:
Probably I needed to add more details.
I have also other stuff in the Layout but I does'n matter, I've created an additional layout just for the buttons.
Now the buttons are in line but they have different width.. : /
I've tried with this but it didn't help..
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT,
1f
);
ll.setOrientation(LinearLayout.HORIZONTAL);
Lets you set buttons in horizontal line allignment.
But to give balanced space to all three buttons. you must set weight property for all the three Button objects to 1.
Edit:
Do this for all buttons.
LinearLayout.LayoutParams params = button.getLayoutParams();
params.weight = 1;
button.setLayoutParams(params);
to set weight for all buttons.
Regards,
Aqif Hamid
Try this:
ll.setOrientation(LinearLayout.HORIZONTAL);
All have MATCH_PARENT should have LayoutParams.WRAP_CONTENT in width or height (in at least one as per LinearLayout orientation )
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT
Try it:
LinearLayout gvDivisao = (LinearLayout) findViewById(R.id.gvDivisao);
LayoutInflater inflater = getLayoutInflater();
LayoutParams btDivLayoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f);
Button btDivA = (Button)inflater.inflate(R.layout.button_divisao, null);
btDivA.setText("A");
gvDivisao.addView( btDivA, btDivLayoutParams);

Setting parameters on child views of a RelativeLayout

I'm having trouble finding exactly the syntax I need to use to set the paramters on child views of a relative layout. I have a root relative layout that I want to set 2 child textviews next to each other like this
---------- ---------
| Second | | First |
---------- ---------
So I have
public class RL extends RelativeLayout{
public RL(context){
TextView first = new TextView(this);
TextView second = new TextView(this);
first.setText('First');
first.setId(1);
second.setText('Second');
second.setId(2);
addView(first, new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT,
LayoutParams.ALLIGN_PARENT_RIGHT ???);
addView(first, new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT,
LayoutParams.ALLIGN_RIGHT_OF(first.getId()) ???);
}
}
How do I set the relative alignments?
public class RL extends RelativeLayout {
public RL(Context context) {
super(context);
TextView first = new TextView(context);
TextView second = new TextView(context);
first.setText("First");
first.setId(1);
second.setText("Second");
second.setId(2);
RelativeLayout.LayoutParams lpSecond = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
addView(second, lpSecond);
RelativeLayout.LayoutParams lpFirst = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lpFirst.addRule(RelativeLayout.RIGHT_OF, second.getId());
addView(first, lpFirst);
}
}
You only need ALIGN_PARENT_RIGHT if you want the right edge of the view to line up with the right edge of its parent. In this case, it would push the 'first' view off the side of the visible area!
Falmarri, you'll need to use the 'addRule(int)' method.
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams
(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lp.addRule(RIGHT_OF, first.getId());
The full list of constants that can be used for addRule can be found here:
http://developer.android.com/reference/android/widget/RelativeLayout.html
And here is the addRule method reference:
http://developer.android.com/reference/android/widget/RelativeLayout.LayoutParams.html#addRule(int,%20int)

Categories

Resources