TextView.getMinHeight() in < 16 API - android

TextView.getMinHeight() appears in API 16. But TextView.setMinHeight() appears in API 1. Where is any possible way to get min height in pre API 16 version without reflection?

Use ViewCompat.getMinimumHeight(view) from support-v4 library.

There's no way to get it without reflection.
The field is called mMaximum. It can hold a value in pixels or in lines of text, that's why it's not called mMaxHeight.
Saving the value in setMaxHeight() is not ideal, because there's also setMaxLines() method, which changes the max mode and effectively clears the max height value. You would have to overload all methods writing mMaximum field.

short answer:
View.getLayoutParams().height
View.getLayoutParams().width
other possibilities :
The size of a view is expressed with a width and a height. A view actually possess two pairs of width and height values.
The first pair is known as measured width and measured height. These dimensions define how big a view wants to be within its parent (see Layout for more details.) The measured dimensions can be obtained by calling getMeasuredWidth() and getMeasuredHeight().
The second pair is simply known as width and height, or sometimes drawing width and drawing height. These dimensions define the actual size of the view on screen, at drawing time and after layout. These values may, but do not have to, be different from the measured width and height. The width and height can be obtained by calling getWidth() and getHeight().
ways:
using the famous OnGlobalLayoutListener
This is one of the most used mechanisms to get the view dimensions. You attach a Global Layout Listener to the view hierarchy. It helps you actually get the width of all the views in your view heirarchy:
by forcing a measurement of the View
View.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
int widht = View.getMeasuredWidth();
int height = View.getMeasuredHeight();

Related

What is the difference between MeasureSpec.getSize(widthMeasureSpec) and getWidth()?

I konw the difference between getWidth() and getMeasuredWidth(),
but I can not understand the difference between MeasureSpec.getSize(widthMeasureSpec) and getWidth(),
which width does MeasureSpec.getSize(widthMeasureSpec) get?
thx~~
MeasureSpec.getSize(widthMeasureSpec) returns the preferred width or the specified witdth of a view you want to create. This may or may not be the same as getWidth() after the view has been created, as views may change size based on the width of other views, screen orientation etc.
Ok lets see.
getMeasuredWidth() returns the width measurement set in onMeasure() via setMeasuredDimension().
getWidth() returns the final width of the view. The Parent view may accept the measurements set in onMeasure (set via setMeasuredDimension()). In which case getWidth and getMeasuredWidth will return the same value. If the Parent view decides that the measurements you set cannot be used, either because it's too big or too small then it will set the final width to something more appropiate. In which case getWidth will have the final valid width and will be different from getMeasuredWidth(). Always use getWidth() in onDraw(). only use getMeasuredWidth() if you want to know if the parent changed the width you previously set in onMeasure().
Finally to explain MeasureSpec.getSize(widthMeasureSpec) you need to understand the following from the official documentation:
If you need finer control over your view's layout parameters,
implement onMeasure(). This method's parameters are View.MeasureSpec
values that tell you how big your view's parent wants your view to be,
and whether that size is a hard maximum or just a suggestion. As an
optimization, these values are stored as packed integers, and you use
the static methods of View.MeasureSpec to unpack the information
stored in each integer. Custom View Components
As you can read from the snippet. WidthMeasureSpec and WeightMeasureSpec are not pixel measurments. To get the actual pixel value use MeasureSpec.getSize():
int width = MeasureSpec.getSize(widthMeasureSpec)
PD:This all applies to the height as well
For more Infomation see my answer here(its not marked as the answer):
MeasureSpec returns a 0 value

How does Android achieve the attribute "wrap_content"?

These days I am learning how to customize the view on Android.I know if we set the minimum width, then the system will compare the min width we set and the measured width and choose the proper one to fit the view.What I am confusing is that how does android system change the size of the view according to the content of the view as the word "wrap_content" means.I want to know more details about how to achieve "wrap_content". Thanks a lot.
This is what Official Documentation says (I am adding just a part of it for a quick read)
When a View object's measure() method returns, its getMeasuredWidth() and getMeasuredHeight() values must be set, along with those for all of that View object's descendants. A View object's measured width and measured height values must respect the constraints imposed by the View object's parents. This guarantees that at the end of the measure pass, all parents accept all of their children's measurements. A parent View may call measure() more than once on its children. For example, the parent may measure each child once with unspecified dimensions to find out how big they want to be, then call measure() on them again with actual numbers if the sum of all the children's unconstrained sizes is too big or too small (that is, if the children don't agree among themselves as to how much space they each get, the parent will intervene and set the rules on the second pass).
The measure pass uses two classes to communicate dimensions. The ViewGroup.LayoutParams class is used by View objects to tell their parents how they want to be measured and positioned. The base ViewGroup.LayoutParams class just describes how big the View wants to be for both width and height. For each dimension, it can specify one of:
MATCH_PARENT, which means the View wants to be as big as its parent (minus padding)
WRAP_CONTENT, which means that the View wants to be just big enough to enclose its content (plus padding).
MeasureSpec objects are used to push requirements down the tree from parent to child. A MeasureSpec can be in one of three modes:
UNSPECIFIED: This is used by a parent to determine the desired dimension of a child View. For example, a LinearLayout may call measure() on its child with the height set to UNSPECIFIED and a width of EXACTLY 240 to find out how tall the child View wants to be given a width of 240 pixels.
EXACTLY: This is used by the parent to impose an exact size on the child. The child must use this size, and guarantee that all of its descendants will fit within this size.
AT MOST: This is used by the parent to impose a maximum size on the child. The child must guarantee that it and all of its descendants will fit within this size.

Difference between getheight() and getmeasuredheight()

I was going through this http://developer.android.com/guide/topics/ui/declaring-layout.html when i cam across a line stating that the height may, but do not have to, be different from the measured height, I was thinking of how can the measured height be different from height of a layout.
The methods View#getMeasuredWidth() and View#getMeasuredHeight() represents the dimensions the view wants to be, before all views in the layout are calculated and laid in the screen.
After View#onMeasure(int, int) and View#onLayout(boolean, int, int, int, int), views measurements could be change to accommodate everything. These (possible) new values are then accessible through View#getWidth() and View#getHeight().
From the View class reference:
The size of a view is expressed with a width and a height. A view actually possess two pairs of width and height values.
The first pair is known as measured width and measured height. These dimensions define how big a view wants to be within its parent (see Layout for more details.) The measured dimensions can be obtained by calling getMeasuredWidth() and getMeasuredHeight().
The second pair is simply known as width and height, or sometimes drawing width and drawing height. These dimensions define the actual size of the view on screen, at drawing time and after layout. These values may, but do not have to, be different from the measured width and height. The width and height can be obtained by calling getWidth() and getHeight().

Get size of GridView when creating items within its adapter

I want to arrange a set of custom widgets inside a GridView. There are predefined templates which describe a count of elements and arrangement config. Max width and heigh for each of them have to be provided in order to avoid scrolling. How can I get a size of the displayed GridView when computing items size in adapter's "GetView" method?
You have access to few functions in order to get a GridView Height and Width as any other View. Look at the documentation here.
The size of a view is expressed with a width and a height. A view
actually possess two pairs of width and height values.
The first pair is known as measured width and measured height. These
dimensions define how big a view wants to be within its parent (see
Layout for more details.) The measured dimensions can be obtained by
calling getMeasuredWidth() and getMeasuredHeight().
The second pair is simply known as width and height, or sometimes
drawing width and drawing height. These dimensions define the actual
size of the view on screen, at drawing time and after layout. These
values may, but do not have to, be different from the measured width
and height. The width and height can be obtained by calling getWidth()
and getHeight().
Try doing a quick search on their doc next time, even if it's Xamarin, the Android doc is pretty reliable and you only need some adjustment to make it work.
(e.g.) : grivView.Width; instead of gridView.getWidth(); one being a property on Xamarin to follow C# standards, the other is a function.

setTop alternative for api under 11

I've been searching for an alternative for the android.widget.ImageView.setTop# method in android which would work for api's under 11 or Honeycomb. Is such an alternative even available?
For anything below Honeycomb (API Level 11) you'll have to use setLayoutParams(...).
you can dynamically set the position of view in Android. for example if you have an ImageView in LinearLayout of your xml file.So you can set its position through LayoutParams.But make sure to take LayoutParams according to the layout taken in your xml file.There are different LayoutParams according to the layout taken.
Here is the code to set:
FrameLayout.LayoutParams layoutParams=new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(int left, int top, int right, int bottom);
imageView.setLayoutParams(layoutParams);
From android documentations: doc
Sets the top position of this view relative to its parent. This method is meant to be called by the layout system and should not generally be called otherwise, because the property may be changed at any time by the layout.
This method shouldn't be called directly, mainly because different ViewGroups have different approach to layout which makes this method not consistent (framelayout).
as #nitesh goel suggested, use Margin or padding to set the spacing between a child view and its original position in the ViewGroup. doc
Size, padding and margins
The size of a view is expressed with a width and a height. A view actually possess two >pairs of width and height values.
The first pair is known as measured width and measured height. These dimensions define how big a view wants to be within its parent (see Layout for more details.) The measured dimensions can be obtained by calling getMeasuredWidth() and getMeasuredHeight().
The second pair is simply known as width and height, or sometimes drawing width and drawing height. These dimensions define the actual size of the view on screen, at drawing time and after layout. These values may, but do not have to, be different from the measured width and height. The width and height can be obtained by calling getWidth() and getHeight().
To measure its dimensions, a view takes into account its padding. The padding is expressed in pixels for the left, top, right and bottom parts of the view. Padding can be used to offset the content of the view by a specific amount of pixels. For instance, a left padding of 2 will push the view's content by 2 pixels to the right of the left edge. Padding can be set using the setPadding(int, int, int, int) or setPaddingRelative(int, int, int, int) method and queried by calling getPaddingLeft(), getPaddingTop(), getPaddingRight(), getPaddingBottom(), getPaddingStart(), getPaddingEnd().
Even though a view can define a padding, it does not provide any support for margins. However, view groups provide such a support. Refer to ViewGroup and ViewGroup.MarginLayoutParams for further information.

Categories

Resources