set top,bottom padding on textview with wrap_content - android

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

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

Text in TextView gravity not working

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.

How to set the left and right margin in dp of EditText in Alert Dialog

Hello I am trying to set the left and right margin of the EditText which is in AlertDialog. I want to set them in dp so that it will work correctly to all device densities. I am trying to do this same like below but still no margin at all.
AlertDialog.Builder alerBuilder = new AlertDialog.Builder(PhotoOptions.this);
alerBuilder.setMessage(getString(R.string.input_compress_value));
final EditText compressValueEditText = new EditText(PhotoOptions.this);
compressValueEditText.setInputType(InputType.TYPE_CLASS_NUMBER);
LayoutParams params = new LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT
);
params.leftMargin = 100;
alerBuilder.setView(compressValueEditText);
compressValueEditText.setLayoutParams(params);
Thanks in advance.
You have to convert the dp value to a pixel value, e.g., via
params.leftMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 100, getResources().getDisplayMetrics());

Android negative margin does not work

I have encountered a problem when i try to give a negative left margin to a LinearLayout.
The negative margin does not appear.
Here is my code
HorizontalScrollView hview = new HorizontalScrollView(context); // HorizontalScrollView is the outer view
RelativeLayout.LayoutParams hs_lot_params = new RelativeLayout.LayoutParams(164, 164);
hs_lot_params.setMargins(100, 100, 0, 0); // set the positions
ImageView image = new ImageView(context);
image.setBackgroundResource(R.drawable.leder);
LinearLayout.LayoutParams img_lot_params = new LinearLayout.LayoutParams(164, 164);
img_lot_params.setMargins(0, 0, 0, 0);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(164, 164);
layoutParams.setMargins(-132, 0, 0, 0);
ll.addView(image, img_lot_params);
hview.addView(ll, layoutParams);
Note: my plan is to scroll the image from left to right.
First, the left part of the image is hidden and can scroll to right to see the full image
ViewGroup.MarginLayoutParams params =
(ViewGroup.MarginLayoutParams)view.getLayoutParams(); params.topMargin = -100;
Negative margins should work in LinearLayout and RelativeLayout. What you probably need, is to scroll the HorizontalScrollView with scrollBy(int x, int y) or scrollTo(int x, int y) to achieve the "peek and scroll" effect you described.
Also keep in mind that using raw pixel units is generally a bad idea as the actual size will depend on the pixel density of the screen. Prefer dp measurements instead.

Android, programmatically layout a button view?

I am trying to programmatically define my program layout and add a button to it at a certain position. I am not using the layout xml as the content view.
RelativeLayout mainLayout;
mainLayout = new RelativeLayout(this);
mainLayout.setLayoutParams(new
LayoutParams(LayoutParams.FILL_PARENT,android.view.ViewGroup.LayoutParams.FILL_PARENT));
I have then added a button that I want to apply the properties
layout align center
parent align left
height 60px
width 60px
here is the button so far
Button BtnNext = new Button(this);
BtnNext.setWidth(60);
BtnNext.setHeight(60);
BtnNext.setFocusable(true);
BtnNext.setId(idBtnNext);
BtnNext.setText("Next");
mainLayout.addView(BtnNext, 1);
Height and width DON'T work correctly.
Hi you can try by setting layout params
RelativeLayout.LayoutParams rel_btn = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
rel_btn.height = 60;
rel_btn.width = 60;
BtnNext.setLayoutParams(rel_btn);
You can also add rules and set margins for Button by setting relative layout params like
rel_btn.addRule(RelativeLayout.CENTER_VERTICAL);
rel_btn.leftMargin = 220;
Height and width will not be as u wished because your are not using Density-independent pixel (dip)
The value you set here is in pixel
You can convert the pixel into dip by
final float scale = getResources().getDisplayMetrics().density;
int dip = (int) (60 * scale + 0.5f);
You will get more accurate result

Categories

Resources