I want to see the sum of textview's height and of textview's padding.
I set padding with method:
textView.setPadding(0,x,0,x);
But if I use
textView.getHeight();
I will receive 0.
If I use
ViewGroup.LayoutParams params = (ViewGroup.LayoutParams) textView.getLayoutParams();
Log.d(TAG,params.height+"");
I will receive -2 that's impossible.
What have I to do?
Edit: This is the XML code
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/try"
android:textColor="#fff"
android:id="#+id/tv"
android:gravity="center_horizontal" />
Edit2: I use this code for setting width of text view and it work correctely.
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
final int width = size.x;
final int height = size.y;
ViewGroup.LayoutParams params = (ViewGroup.LayoutParams) textView.getLayoutParams();
params.width = width/2;
I don't want to set the height programmately because I set a padding. But I want to get the measure
Edit3:
I try to use
Rect bounds = new Rect();
textView.getPaint().getTextBounds(textView.getText(), 0,textView.getText().length(), bounds);
bounds.height(); //This should give you the height of the wrapped_content
as Slartibartfast told me. But there is a problem:
Probably this is the measurement of text size... But it isn't the correct measurement of height of textview. I tried to sum this number and the 2 measure of padding and try to set:
params.height = bounds.height() + 2*x;
The view is smaller than the view without this line of code.
Take a look at : How to retrieve the dimensions of a view?
Basically the size is known only after the layout is finished, one way is to create a custom text view and use onSizeChanged method
EDIT:
How about
Rect bounds = new Rect();
textView.getPaint().getTextBounds(textView.getText(), 0,textView.getText().length(), bounds);
bounds.height(); //This should give you the height of the wrapped_content
Related
Set i have a fragment CustomSwitchFragment, that i want to use as followed:
<fragment android:name="m6world.test_toggle_yes_no.CustomSwitchFragment"
android:id="#+id/custom_switch_fragment"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:orientation="horizontal" />
i don't want to have to input 80dp, i would like to be able to put wrap_content and the fragment will be set whatever width i used when created it.
Basically i don`t want to have to tell people who are gonna use my fragment, to set it to 80dp specifically.
I guess you can use something like this
Display display = getWindow().getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
WindowManager.LayoutParams params = getWindow().getAttributes();
params.width = width * 70 / 100;
params.height = height * 30 / 100;
getWindow().setAttributes(params);
getWindow().getAttributes().verticalMargin = 0.2F;
I have gridview which contains 2 columns. The width is set to fill available space.
I need height to be set to specific size according to width, because in every screen the width dimension is different, I can't set height in xml as constant. I need to set ratio, so height should be about 1.5 * width.
Thanks
You can set Height, but you want to get Width First.
You told 2 columns of grid so understand phone half of your phone width
Display display = getWindowManager().getDefaultDisplay();
int imgWidth = display.getWidth() / 2;
int imgHeight = (int)(imgWidth * 1.5f);
You get height of that.Now you can set your Grid View Width and Height
LayoutParams lp = HERE_YOUR_VIEW.getLayoutParams();
lp.width = imgWidth;
lp.height = imgHeight ;
HERE_YOUR_VIEW.setLayoutParams(lp);
How can I get and set the size of ImageButton programmatically?
Android seems to make button size bigger than image size.
Maybe I am wrong.
I want to make it the same size as image size.
try to set padding to 0dp in xml layout:
<ImageButton ...
android:padding="0dp" />
The image should be as big as button.
If you want to make button (but not an image) transparent set button background color to #null:
<ImageButton ...
android:background="#null" />
int height = button.getLayoutParams().height;
int width = button.getLayoutParams().width;
button.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT));
To programmatically get the height and width:
ViewGroup.LayoutParams layoutParams = mImageButton.getLayoutParams();
int height = layoutParams.height;
int width = layoutParams.width;
To programmatically set the height and width:
int myHeight = ContextCompat.getDrawable(getContext(), R.drawable.myImageAsset).getIntrinsicHeight();
/*any desired height, but for OP's sake ^^*/
int myWidth = ContextCompat.getDrawable(getContext(), R.drawable.myImageAsset).getIntrinsicWidth();
/*any desired width, but for OP's sake ^^*/
ViewGroup.LayoutParams layoutParams = mImageButton.getLayoutParams();
layoutParams.height = myHeight;
layoutParams.width = myWidth;
mImageButton.setLayoutParams(layoutParams);
I want to reset a textView height after I have added it to the main window in the xml file.
inside a RelativeLayout,
<TextView
android:id="#+id/text_l"
android:layout_width="50sp"
android:layout_height="50sp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginLeft="10sp"
android:layout_marginTop="145dp"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#000000" >
</TextView>
I just want to change it from 50 to 70:
I tried:
TextView text = (TextView)findViewById(R.id.text_l);
text.setHeight(70);
but nothing changed.
You should change it via LayoutParams:
LayoutParams params = (LayoutParams) textView.getLayoutParams();
params.height = 70;
textView.setLayoutParams(params);
EDIT
You should not use sizes in pixels in you code, use dimensions for this:
dimens.xml:
<dimen name="text_view_height">50dp</dimen>
In code:
params.height = getResources().getDimensionPixelSize(R.dimen.text_view_height);
Pragmatically you can set textview height like:
private TextView mTxtView;
int height = 50; //your textview height
mTxtView.getLayoutParams().height = height;
you can dynamically set width and height of textview by
private TextView mTxtView;
private int screenWidth, screenHeight;
Display display = getWindowManager().getDefaultDisplay();
screenWidth = display.getWidth();
screenHeight = display.getHeight();
LayoutParams params = mTxtView.getLayoutParams();
params.width = screenWidth-30;
mTxtView.setLayoutParams(params);
In Kotlin with DP to pixels translation
changeTextHeight.setOnClickListener { view ->
// random height for testing
val randomHeightInDP = Random().nextFloat() * (50.0f - 10.0f) + 10
// set Height in pixels
hello.layoutParams.height = convertDpToPixel(randomHeightInDP, applicationContext)
//refresh layout
hello.requestLayout()
}
Convert DP to pixels, see this post:
fun convertDpToPixel(dp: Float, context: Context): Int {
return (dp * (context.resources.displayMetrics.densityDpi.toFloat() / DisplayMetrics.DENSITY_DEFAULT)).toInt()
}
the sample way for this if you want to use dimen
first, you should set size in dimen XML file.
<dimen name="text_height">50dp</dimen>
<dimen name="text_width">50dp</dimen>
and
text_l.getLayoutParams().height =
getResources().getDimensionPixelSize(R.dimen.text_height);
text_l.getLayoutParams().width =
getResources().getDimensionPixelSize(R.dimen.text_width);
Or
if you want to set just int (for example we wanna set 50dp height and 100dp width)
text_l.getLayoutParams().height = 50 * 3;
text_l.getLayoutParams().width= 100 * 3;
I know this is an old question, but for the sake of others who might find this, there are scenarios where you should call textView.requestLayout() after changing the layout parameters. While it my be fine to omit it if you are simply changing the layout parameters as a one time thing before the layout is drawn. In my case, I wanted to change the height parameter of a TextView based on a radio button selection using onCheckedChangedListener, but the TextView height would only update the first time it was drawn. Adding requestLayout() solved this problem.
TextView tv;
ViewGroup.LayoutParams params = tv.getLayoutParams();
params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
if(!tv.isInLayout()) {//make sure it isn't currently in a layout pass
tv.requestLayout();//request a layout pass
}
So I need to change the size of an image depending on the area of the screen. The image will have to be half of the screen height, because otherwise it overlaps some text.
So Height= 1/2 Screen Height.
Width = Height*Aspect Ratio (Just trying to keep the aspect ratio the same)
I found something that was:
Display myDisplay = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int width =myDisplay.getWidth();
int height=myDisplay.getHeight();
But how would I change image height in java? or even XML if possible? I can't seem to find a working answer.
You can do this with LayoutParams in code. Unfortunately there's no way to specify percentages through XML (not directly, you can mess around with weights, but that's not always going to help, and it won't keep your aspect ratio), but this should work for you:
//assuming your layout is in a LinearLayout as its root
LinearLayout layout = (LinearLayout)findViewById(R.id.rootlayout);
ImageView image = new ImageView(this);
image.setImageResource(R.drawable.image);
int newHeight = getWindowManager().getDefaultDisplay().getHeight() / 2;
int orgWidth = image.getDrawable().getIntrinsicWidth();
int orgHeight = image.getDrawable().getIntrinsicHeight();
//double check my math, this should be right, though
int newWidth = Math.floor((orgWidth * newHeight) / orgHeight);
//Use RelativeLayout.LayoutParams if your parent is a RelativeLayout
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
newWidth, newHeight);
image.setLayoutParams(params);
image.setScaleType(ImageView.ScaleType.CENTER_CROP);
layout.addView(image);
Might be overcomplicated, maybe there's an easier way? This is what I'd first try, though.