Text in TextView gravity not working - android

I written the following function to create text view.But some the text not visible completely so i get gravity to center_horizontal|center|vertical but it is not working.I the following images IN DATE & OUT DATE not visible completely.So i want to set all text to center vertical and center horizontal
protected TextView createDefaultTabView(Context context) {
TextView textView = new TextView(context);
textView.setGravity(Gravity.CENTER_VERTICAL|Gravity.CENTER_HORIZONTAL);
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, TAB_VIEW_TEXT_SIZE_SP);
textView.setTypeface(Typeface.DEFAULT_BOLD);
//textView.setPadding(0,0,0,25);
textView.setLayoutParams(new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
[![enter image description here][1]][1] TypedValue outValue = new TypedValue();
getContext().getTheme().resolveAttribute(android.R.attr.selectableItemBackground,
outValue, true);
textView.setBackgroundResource(outValue.resourceId);
textView.setAllCaps(true);
int padding = (int) (TAB_VIEW_PADDING_DIPS * getResources().getDisplayMetrics().density);
textView.setPadding(padding, padding, padding, padding + 10);
return textView;
}

Your TextView's height is set to WRAP_CONTENT, so it's internal Gravity has no effect on it's vertical positioning (because there's no extra bounds to position itself in).
If you want to get it vertically aligned, you need to either set the TextView's PARENT gravity to CENTER_VERTICAL, or set the TextView's height to MATCH_PARENT.
Either of those will give you the effect you seek.

Related

Derive layout param width from textView text length

I want to set the width of a textView programmatically. I want the width to vary with the length of the text (value) of the textView. The text of the textView must not wrap. If the text of a textView is "OLA OMO ALARE", how can I use the length of this string (13) to derive the correct value to set lp.width to, in the code below ?
LayoutParams lp = tv.getLayoutParams();
lp.width =
tv.setLayoutParams(lp);
I tried the code below, but the width was not long enough, thus text was auto wrapped in textView.
lp.width = Math.round( tv.getPaint().measureText(tv.getText().toString())) )
Thanks.
1) You have to use Rect instance for this.
2) Then you need to use this in textView's getPaint() and getTextBound() methods which allows you to put your text of the textview.
3) At the end just get width from the Rect instance.
Code is here with all the steps implemented,
I tested this and it works fine. Hope it helps.
TextView textView = new TextView(this);
textView.setText("AALAP");
Rect rect = new Rect();
textView.getPaint().getTextBounds(textView.getText().toString(), 0, textView.getText().length(), rect);
Log.d(TAG, "onCreate: width: "+rect.width());
Use the ViewGroup.LayoutParams.WRAP_CONTENT constant for this.
ViewGroup.LayoutParams params = myTextView.getLayoutParams();
params.width = ViewGroup.LayoutParams.WRAP_CONTENT;
myTextView.setLayoutParams(params);

set top,bottom padding on textview with wrap_content

viewParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
viewParams.setMargins(15, 15, 15, 0);
TextView text = new TextView(mContext);
text.setLayoutParams(viewParams);
text.setGravity(Gravity.CENTER_VERTICAL);
text.setPadding(50,50,50,50);
text.setBackground(mContext.getResources().getDrawable(R.drawable.exp_comments));
I want my textview to have top and bottom padding.
I set background with green-rectangular border around the textview.
I set height with wrap_content.
and it means that the border is just above the text.
I want to increase the top and bottom gap between the border and the text.
So, I tried with setPadding() and setPaddingRelative() method.
But it doesn't work....
The setPadding method values is in pixels. You should convert your dp to pixels and then set the padding.
int valueInDp = 50;
Resources r = getResources();
int px = (int) TypedValue
.applyDimension(TypedValue.COMPLEX_UNIT_DIP, valueInDp, r.getDisplayMetrics());
text.setPadding(px,px,px,px);
Change top and bottom padding,
text.setPadding(50,100,50,100);

Align image and text on textview

In my app, I populate a linearlayout with a text and a little icon (like a listview with a custom adapter).
I need the text and the picture to have the same size. Is there a way to do that?
Here's the code:
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
LinearLayout LayoutPadre = new LinearLayout(this);
TextView Tag = new TextView(this);
ImageView img = new ImageView(this);
Tag.setText(data);
if(stato.equalsIgnoreCase("presente")){
img.setImageResource(R.drawable.ic_presente);
}else{
img.setImageResource(R.drawable.ic_assente);
}
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(75, LayoutParams.WRAP_CONTENT); //Here I need the img to have same size as the text, but Tag.getHeight() returns 0
layoutParams.setMargins(50, 0, 0, 0);
img.setLayoutParams(layoutParams);
LayoutPadre.setLayoutParams(params);
LayoutPadre.addView(Tag);
LayoutPadre.addView(img);
The problem is that the image is not horizontally centered like the img.
Look at the screen
Thanks to all.
keep the layout_height of text and image as Match_Parent and not Wrap_Content... By that
If you are preserving the android:layout_height="wrap_content" or it did not work, you can also do android:gravity="center"

Android place TextView depends on its center

How to place TextViews on layout depends on TextView's center from code?By default we can create it depends on its top left angle. Method setGravity(Gravity.CENTER_HORIZONTAL) doesn't help.
EDIT
I want to place TextViews in circle. For what I use RelativeLayout. Within layot TextViews place in for-loop with some angle step. Here some code
for (int i = 0; i < 62; i++) {
TextView word = new TextView(context);
word.setTextSize(13);
// word.setGravity(Gravity.CENTER_HORIZONTAL);
word.setTextColor(Color.BLACK);
RotateAnimation rotateAnimation = new RotateAnimation(0,
angle.get(i), 0, (float) (textHeight * 0.9 + rotateCircle));
rotateAnimation.setFillAfter(true);
rotateAnimation.setDuration(100);
rotateAnimation.setStartOffset(100);
word.setAnimation(rotateAnimation);
word.setGravity(Gravity.CENTER);
word.setPadding(10, 10, 10, 10);
RelativeLayout.LayoutParams layoutparams = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
layoutparams.leftMargin = Math.round(x_coords.get(i));
layoutparams.topMargin = (int) (Math.round(y_coords.get(i)
- textHeight * 0.9 - rotateCircle));
textviews.add(word);
words_layout.addView(word, layoutparams);
}
The gravity of a TextView controls how its content is aligned (the text), not how the view itself is aligned in the layout. To control how the View is aligned in the layout use the android::layout_gravity attribute instead, or the gravity field in the LayoutParams.
Use the android:layout_gravity = CENTER_HORIZONTAL to make text view in center.

android layout - WRAP_CONTENT from right to left possible?

I have a TextView and an ImageButton in a linear layout (horizontal). Total width I have is 300 pixel. Button image is 50x50. Max width I can use for text is 250. The code below works perfect if the text width is less than 250 pixels (WRAP_CONTENT work nice).
// create relative layout for the entire view
LinearLayout layout = new LinearLayout(this);
layout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
layout.setOrientation(LinearLayout.HORIZONTAL);
// create TextView for the title
TextView titleView = new TextView(this);
titleView.setText(title);
layout.addView(titleView);
// add the button onto the view
bubbleBtn = new ImageButton(this);
bubbleBtn.setLayoutParams(new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT));
layout.addView(bubbleBtn);
Problem comes when the text occupies more than 250 pixels. Button gets pushed out and becomes invisible within that 300 pixel space.
What I want is this: Allocate 50 pixels width for the image. WRAP_CONTENT in the remaining 250 pixels. In other words, instead of filling in from left, fill in from the right. Is Gravity the right thing to use in this context? How and where should I use it in the code?
Or any other better way of doing this?
Use a RelativeLayout instead of a LinearLayout. Set the LayoutParams of each View as follows:
// add the button onto the view
bubbleBtn = new ImageButton(this);
bubbleBtn.setId(1); // should set this using a ids.xml resource really.
RelativeLayout.LayoutParams bbLP = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
bbLP.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
bbLP.addRule(RelativeLayout.CENTER_VERTICAL);
layout.addView(bubbleBtn, bbLP);
// create TextView for the title
TextView titleView = new TextView(this);
titleView.setText(title);
titleView.setGravity(Gravity.RIGHT);
RelativeLayout.LayoutParams tvLP = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
tvLP.addRule(RelativeLayout.LEFT_OF, 1);
tvLP.addRule(RelativeLayout.CENTER_VERTICAL);
layout.addView(titleView, tvLP);

Categories

Resources