I wrote my own GridView and i using it with XML setting layout_width as match_parent. My question is how to get width in pixels from attrs. I know how to get my cyston attrs but dont know how to read width value before view is drawed.
In android you can't access the attribute of size before drawing because the dimensions are determined when the drawing process begin. It is the same for all views describe by xml.
I found this link that try to find a way to get the height so you could use the same to find the width of the screen (if your grid fill the size of the screen) : Android - get Height of the parent layout
They give :
getApplicationContext().getResources().getDisplayMetrics().heightPixels
Hopes it helps
If you find it useful please check Resolved
I solve this. Just need to getMeasuredDimensions after onMeasure method is called.
Related
I have a custom View I'm making which has a fixed proportion between width and height. I want the programmer to set the height and I would like the View to set its own width according to a formula based on height.
The View is going to be a child of a RelativeLayout so I can't use the height of the parent element to calculate anything.
How can I accomplish this? I don't know how to do it in the onMeasure hook because the View's width and height are not available at that time.
By the way, I am making this view solely programmatically, no XML involved.
use View.setlayoutParams()
There's an example using an adView here.
Is there a direct programmatic way to get a fill_parent View's pixel height and width? For instance a view in a grid layout in a tab. Or do I have to get the window size and subtract the static sizes of the views around it?
You can get the views parent by calling myView.getParent() and with getWidth() and getHeight() you get the pixel you need.
the documentation is providing more information to this topic
http://developer.android.com/reference/android/view/View.html
There's the onMeasure() method, not sure if that will do what you need. http://developer.android.com/guide/topics/ui/custom-components.html
Can I using xml layout attributes put image to the following position? Or I must calculate position on src?
http://i.stack.imgur.com/gox4d.png
You can do it using the XML attributes.
Just use follwing attribute in your ImageView:
Android:layout_marginTop="20dip"
Android:layout_height="20dip"
Only use DIP as these are density independent Pixels
dont use PX.
Yes, you can, for the simple example you posted I would use a vertical LinearLayout, with a FrameLayouts to 'fill' the blank space. Set the LinearLayout weightSum to 1, then set the first FrameLayout weight attribute to 0.2 (1/5), and imageview to 0.2. Also set both the frame layout and imageview layout_height values to 0px.
While this solution works, I'm sure there is a better and cleaner way out there. Hopefully someone will post it.
You can use AbsoluteLayout, calculate exact coordinates based on screen size and orientation and then position the image with absolute coordinates.
You can just use weights for different containers in a vertical linear layout.
I am creating a dynamic table whose rows contains a imageview and a textview. My problem is this imageview is taking full size of original image but I want to change the size of imageview.I have used setLayoutParams which has no effect.
As an alternative I also used textview instead of imageview and set image as textview's background and used setWidth and setHeight but it has the same problem.
Plz Help Me.
Have you tried the scaleType-Attribute?
Check out the setMaxWidth and setMaxHeight-methods of the ImageView-class:
To set an image to be a maximum of 100
x 100 while preserving the original
aspect ratio, do the following: 1) set
adjustViewBounds to true 2) set
maxWidth and maxHeight to 100 3) set
the height and width layout params to
WRAP_CONTENT.
Link
Assuming your image is a drawable resource, you can use ScaleDrawable instead, and use the xml attributes android:scaleHeight and android:scaleWidth to shrink your image.
OK I found the Solution
Resizing an ImageView within a TableLayout programmatically
Try changing your ImageView's layout_width and layout_height inside the layout file to some constant values measured in sp, for example 100sp. This will make all ImageViews look the same.
I would like change the width of a Horizontal Progressbar programmatically (initial size is set in XML using RelativeLayout...but I would like to dynamically change it based on certain values).
I have tried setMinimumWidth(50) in my code, but that did not make a difference. I have also tried setting 'android:layout_width="wrap_content", but that did not work either.
Here is my XML:
<ProgressBar android:id="#+id/progress_horizontal"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="100dip"
android:layout_height="wrap_content"
/>
Thanks in advance for any assistance!
What layout is your ProgressBar in? This makes a difference as to how the layout_width and height are interpreted.
However, I think you are seeing a conflict between the View's width, and the layout_width. Each time the view is rendered, its preferred size is determined based on the minWidth, width, etc. set on it. The layout that the view sits in can then update the actual size to render based on the layout_width information. See How Android Draws Views and View size documentation for more info on the two phase process.
In your case, you have the layout_width set to 100, and the minimum width set to 50, so I would think it would always show 100.
Try setting the layout_width to wrap_content, and then updated the preferred width for the ProgressBar.
I would imagine the setmindwidth would be met when you give it an initial value of 100. the system would receognize it exceeds 50 and then continue to do as the rest of what it's told. try making the min width 150, set the xml for an initial value of 75 and in your onCreate fire off a Log.d("progbar", String.valueOf(myprogress.width));
disclaimer - written when in front of work computer without development environment for reference.