android update view at runtime - android

I have a custom imageView and I change it's background drawable at runtime. Images don't have the same height and when a resource have higher height, the top margin does not update itself (if top margin is 2 dp and after I set another resource as background which is higher with 2 do, the top margin becomes 0 because layout hasn't updated). How to force layout to update itself at runtime and not schedule a layout pass in the future? I've tried with:
imageView.invalidate()
imageView.requestLayout()
imageView.forceLayout()
parent.updateViewLayout(imageView, layoutParams)
but none of these methods works. The layout is updated only after I switch device's orientation. Do you have any ideas how to force layout to be refreshed at runtime?

Related

Android Dynamically changing one child Layout height

I wanted to make a layout which has three children layout.
the bottom layout must visible to bottom
screen size is enough:
the center layout need Dynamically change to wrap_content and we need the bottom layout is under the center layout (not top of the parent).
screen size is not enough:
fix size of center layout will fixed and it is a scrollview, it's height is dependency on the reminding width(if screen size is not enough)
I think a lot of ideas but i don't know how can do using xml. It can be only calculate in runtime?
this is the intend image:
you can use wrap_content in xml
OR
To change programatically you can use following code
Your_Layout.getLayoutParams().height = 100;

Android relative layout align center of background

I have a background image, which is simply colored panel such as silver purple, etc.,
This background image is set as andorid:background of RelativeLayout, which consist of a ImageView
I want that imageview's image resource to be appear like in center of the background image.
I tried alignParentTop and marginTop to position the combo image in center.
It works as expected, but when trying on different devices, as marginTop is hardcoded such as 10dp,, or 5dp, the alignment looks differently on different devices with different resolution.
I have even created various dimens.xml files for various configuration with various marginTop dp values. Still it only works on specific devices and doing fro all devices is obviously impossible. So i need to set or align the background and foreground images such that foreground image is centered over background image.
Note: Using Framelayout is not of my concern. I want this by RelativeLayout.
On the ImageView and the parent RelativeLayout, remove all of the padding and margin stuff you have, then add android:layout_centerInParent="true" to the ImageView. If you know the height and width of the image in the ImageViewbeforehand, set the ImageView to match it identically (in dp's). Otherwise, try setting the ImageView height and width to wrap_content and potentially android:adjustViewBounds="true".

Android - How can I determine an appropriate height for my ListView items?

I am building an Android app with a ListView. Coming from iOS I am used to setting fixed pixel heights for list view items, since the screen sizes of the used devices are always the same. Now for Android, I am wondering what is a good way to dynamically set the heights of ListView items so that it it looks nice on all screen sizes?
In android there are two famous properties. They are:
MATCH_PARENT formerly FILL_PARENT using this property for layout width or height will expand the view to the parents width or height minus margins
WRAP_CONTENT using this property for layout width or height will allow the view to take as much space required or available(if it exceeds screen dimension exception is inside scrollable views)
So for your tag set both width and height to match_parent. And in the custom row that you might be populating set the root layout width to match_parent and height to wrap_content.
Note: in android while we give fixed height at times but it is generally not a good practice.

How To Scale A Layout While Keep Its Children Original Scale

Can I Scale A Layout(e.g LinearLayout) from 0.5 to 1.0 while Keeping the size And position of the TextView inside the layout to 1.0 During the whole transformation (the overflow parts should hidden)?
I see That The Scale Animation in IOS Keep the scale of the Children And How Can I Achieve this in Android ?
Plus,On Starting Animation, The Parent Layout of the animated block has already make space for it,But in IOS ,the parent makes room gradually during the animation, does it have an option for me to do the same thing in Android ?
This can be done pretty easily with the Property Animation API.
havent tried it on LinearLayout but if the view children are ordered correctly with their "layout_width" & "layout_height" you can use the ValueAnimator class to scale the parent view via LayoutParams.width & height.
Only setback is API 11 and above.

Set custom frame of tab in Android

Is it possible to set height and width of tabs in Android? I tried setting customized textview as the indicator of tabspec, setting its height and width to different values, but no matter what its height and width value is, it's always set to the default height and width value.
Yes, but it is quite a bit of a hassle. The layout for the tab widgets uses a fixed height, so you'd have to change that layout directly. One way of doing that is to extend TabWidget, override the addView method and change the widgets according to your needs.

Categories

Resources