Android:How do I center TextView horizontally in LinearLayout Programatically - android

I have a textview view whose width doesn't cover all the parent's width. I have used valueTV.setGravity(Gravity.CENTER_HORIZONTAL);
and it puts the text in center of textview.
Now I want to take the whole textview in the center of the parent ( which apparently covers the whole scrren). How can it be done programatically. I want to align the textview to center.Basically I know a lot of questions have already been asked which are quite similar to this but not exactly the same as they didnot solve my problem.

// Make sure textview width match parent
// Use param: ViewGroup.LayoutParams.MATCH_PARENT
LinearLayout.LayoutParams layoutParams =
new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
// Make sure text is center
layoutParams.gravity = Gravity.CENTER_HORIZONTAL;
linearLayout.addView(valueTV, layoutParams);

Hmmm... Try this perhaps
TextView tv = findViewById(R.id.yourTextView);
LinearLayout.LayoutParams layoutParams =
(LinearLayout.LayoutParams)tv.getLayoutParams();
layoutParams.gravity = Gravity.CENTER_HORIZONTAL;
tv.setLayoutParams(layoutParams);

Related

How can I remove effect of gravity using LayoutParams method in Android? (Gravity.NO_GRAVITY doesn't work.)

I have expected that TextView was aligned to the right because of NO_GRAVITY, however it was aligned to the left. Shouldn't NO_GRAVITY leave the gravity setting as it is, instead of aligning it to the left. If I also remove this two lines belove, it still aligns the TextView to the left even if I specify android:layout_gravity="center" in xml code.
params.gravity = Gravity.RIGHT;
params.gravity = Gravity.NO_GRAVITY;
How can I change other properties of the layout without effecting its gravity? What is exactly the difference between LEFT and NO_GRAVITY?
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.RIGHT;
params.gravity = Gravity.NO_GRAVITY;
textView.setLayoutParams(params);
Gravity.NO_GRAVITY might (its just a guess) mean "default" gravity, which in your locale is left, but in some countries it might be right (arabic e.g., due to writting direction). if you want centered aligning then you can set Gravity.CENTER, obviusly...
if you want to combine two Gravities, then you can use flag add
params.gravity = Gravity.RIGHT | Gravity.CENTER_VERTICAL;
besides that if you are setting LinearLayout.LayoutParams then textViews parent must be LinearLayout. in rare cases with dynamic layouts and multiple threads change in params shoud be continued with requestLayout() method call for forcing "redraw" in proper position and thread

Android: How to dynamically place views with relative spacing

I am dynamically placing views into a RelativeLayout. Currently, I am doing:
layoutParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(0, position, 0, 0);
relativeLayout.addView(textViewInstance, layoutParams);
where position is incremented by a fixed amount after each TextView object is added to the layout. The problem is that some of the TextViews contain long strings, which makes for awkward formatting on devices with smaller screen sizes because the string will spill into multiple lines and bleed into the whitespace.
Is there any way I can add these TextViews with spacing relative to the TextView right above it? I want to achieve something to the effect of:
layoutParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(0, previousTextViewInstance.bottomPixel + 50, 0, 0);
relativeLayout.addView(textViewInstance, layoutParams);
where previousTextViewInstance will be saved after each new TextView is added.
Is this possible?
Instead of setting margin for each view, you can dynamically add each view below the previous one in relative layout.
For that, first set an id for each of your textView.
firstTextView.setId(textView_id_1);
while adding second textView, set rule for layout params as:
layoutParams.addRule(RelativeLayout.BELOW, textView_id_1);
secondTextView.setLayoutParams(layoutParams);
secondTextView.setId(textView_id_2);

ImageView and TextView of LinearLayout not equal width

I've been stuck on this for quite some time now. The problem is that I have a linearlayout consisting of multiple textViews and an imageView. However, the imageview has a smaller width than the textviews. I tried everything i could find, but I couldn't find it. I am able to set the image to the left or right by using gravity, but when i use fill, it just centers and keeps the small width. The only thing i can think of is that the image is downloaded in a different thread. Can the width only be set after the image is downloaded?
// Download photo
this.post_body.setPhoto(url);
// Add imageview to linear layout
this.ll.addView((View) this.post_body.photo);
LinearLayout.LayoutParams layoutParams;
layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.gravity = Gravity.FILL_HORIZONTAL;
this.post_body.photo.setLayoutParams(layoutParams);
Just try to set width=0 and set weight 1 for all the textview and buttons, imageviews whatever you want.
Just try like this here 1f is weight.
layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT, 1f);

LayoutParams Gravity Right not enforcing

I'm extending a LinearLayout to make a compound widget. The widget is set to layout_width="MATCH_PARENT".
mLabelTextView = new TextView(context);
mLabelTextView.setText("Testing");
LayoutParams labelParams = new
LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT, 0.0f);
labelParams.gravity = Gravity.CENTER_VERTICAL;
addView(mLabelTextView, labelParams);
mContentView = new Checkbox(content);
contentParams = new LinearLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT, 1.0f);
contentParams.gravity = Gravity.CENTER_VERTICAL | Gravity.Right;
addView(mContentView, contentParams);
My problem is that the Gravity.Right property is taking effect on mContentView, but Gravity.CENTER_VERTICAL is. I know that mContentView is getting stretched to fill the remaining space. I've already tried just getting gravity=right by itself and that didn't work.
I have a similar code path in which I inflate a view that already has right gravity set in XML and that is working. So I must be missing something somewhere.
If your parent LinearLayout has horizontal orientation (which you don't specify, but I'm guessing), then it will ignore all horizontal layout gravity bits. Likewise, if it has vertical orientation, then it would have ignored the CENTER_VERTICAL setting.
One trick is to wrap mContentView in a vertically oriented LinearLayout, give that wrapper a layout weight of 1 (so it fills the remaining space) and then the wrapper will honor the RIGHT gravity.
A better alternative might be to extend RelativeLayout instead of LinearLayout. That gives you 2-D control over gravity (unlike LinearLayout).

Set EditText to percentage of parents's width in LinearLayout

I have a LinearLayout class in which I have:
TexView | ImageView | EditText | ImageView.
I have the last ImageView all the way to the right side of the LinearLayout its wrapped in. The EditText runs very long and in some case pushes the last ImageView out of view (or so it seems to push it out of view).
I want to to have the EditText set to a percentage of the total width. I tried using weight with LinearLayout parameters but it seems to cause the view to get all out of wack. For example here it is for the EditText:
LinearLayout.LayoutParams lpEt = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT,
.50f);
All the other views follow suit but have lesser weights (.2, .2, .1) and they all add up to 1.0. But the LinearLayout row is never spaced correctly.
Should I find out the width of the parent (which is a ListView) and then set the width of the EditText explicitly based on the parent's width or is there a better way?
Im not sure what your linear layout orientation is but im guessing its horizontal. if so try this.
LayoutParams textViewParams = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
textView.setLayoutParams(textViewParams);
LinearLayout.LayoutParams editTextParams = new LinearLayout.LayoutParams(
0,LayoutParams.WRAP_CONTENT,1f);
editText.setLayoutParams(editTextParams);
LayoutParams imageViewParams = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
imageView.setLayoutParams(imageViewParams);
This will make the edit text to fill the remaining space between the textview and imageview. If you need something else, just modify the code a bit. When using weights, set the weight or height as 0 for which you are setting weight for.

Categories

Resources