changing the position of ImageView dynamically in android - android

all
I need to change the position of a ImageView dynamically and iam using the following code
int x=100,y=100;
RelativeLayout.LayoutParams mparam = new RelativeLayout.LayoutParams((int)(LayoutParams.FILL_PARENT),(int)(LayoutParams.FILL_PARENT));
mparam.topMargin=x;
mparam.leftMargin=y;
ball.setLayoutParams(mparam);
x+=100;
y+=100;
but i didnt get any change.
Is it possible? and How?

You change margin, but not its position and, it seems, it's not a good solution.
Try to use method View.layout(int, int, int, int)
Documentation says:
public void layout (int l, int t, int r, int b)
Since: API Level 1
Assign a size and position to a view and all of its descendants
This is the second phase of the layout mechanism. (The first is measuring).
In this phase, each parent calls layout on all of its children to position them.
This is typically done using the child measurements that were stored in
the measure pass(). Derived classes should not override this method. Derived
classes with children should override onLayout. In that method, they should call
layout on each of their children.
Parameters
l Left position, relative to parent
t Top position, relative to parent
r Right position, relative to parent
b Bottom position, relative to parent

You can call setPadding on your View. So, in your code (which by the way you should put in a code block!), just add a call to this method:
int x=100,y=100;
LayoutParams mparam = new LayoutParams((int)(LayoutParams.FILL_PARENT),(int) (LayoutParams.FILL_PARENT));
ball.setLayoutParams(mparam);
ball.setPadding(x,y,0,0);
x+=100; y+=100;
ball.setPadding(x,y,0,0);

Related

getTop for custom view always returning zero

I have built a custom view, where when the user taps on the view it expands. Before it expands I call getTop() to get the current Y coordinate, however it returns zero always. Im calling getTop() from within the custom view class. Is calling getTop() from the class that implements the view not allowed?
public void expand(float targetHeight) { //This custom view extends FrameLayout
int top = getTop(); // returns zero
rootLayout.setLayoutParams(new FrameLayout.LayoutParams(widthPixels, dpToPx((int) targetHeight)));
webView.setLayoutParams(new FrameLayout.LayoutParams(widthPixels, dpToPx((int) targetHeight)));
}
The method getTop() returns the Top position of the view relative to its parent. So maybe is in the position 0 of his parent.
Can you please share your XML so I can have a better understanding of the layout?

Custom LinearLayout cannot add view after overrided onMeasure or set to match_parent

I am not a native English user. Sorry for the bad grammar and spelling.
I have a Dialog that contains a View. The View is inflated by an XML file which contains a CustomView. The CustomView, which inherited from a LinearLayout, add views with addView() method.
My CustomView: https://gist.github.com/TomazWang/e1ddcc32fb2f733d874022ee9c4cffdd
Codes that generate the dialog: https://gist.github.com/TomazWang/61d9ad5df3c24bfaa06a78d8a1bcab13
The problems were:
When the layout_height of CustomView is set to match_parent, the child views in CustomView won't show.
After the layout_height of CustomView is set to wrap_content, the child views were shown, but the onMeasure() method takes too long.
I override the onMeasure() method. The method was not slowing my app. However, the child views are gone again.
Can't test it, but there seem to be a number of mistakes in your onMeasure() method. The first one is about missing parantheses.
int desiredHeight = mChildHeight * mEditors.size() - 1
+ (mViewOther == null ? 0 : mViewOther.getHeight())
+ (mViewTitle == null ? 0 : mViewTitle.getHeight());
should probably read
int desiredHeight = mChildHeight * (mEditors.size() - 1)
+ (mViewOther == null ? 0 : mViewOther.getHeight())
+ (mViewTitle == null ? 0 : mViewTitle.getHeight());
The second mistake is about your call to setMeasuredDimension(). It is not necessary since this is what the call to super.onMeasure(...) does. Go check the source.
The third mistake is about when to call super.onMeasure(...). You want to change how the superclass measures, so do not call super.onMeasure(...) at the beginning, but at the end of your custom onMeasure.
The fourth mistake is connected to the third; it is about the parameters you use when calling super.onMeasure(...). Do not use the regular LinearLayout parameters, but your computed values. In order to make a new MeasureSpec from your height variable, do:
int newHeightMeasureSpec = MeasureSpec.makeMeasureSpec(height, heightMode);
And then call super.onMeasure(widthMeasureSpec, newHeightMeasureSpec);.
I guess that problem is that default orientation of LinearLayout is horizontal (doc), so if the first child's width of your custom view fits parent's width (match_parent) you will see only one item - first.
Try to modify init() method of your custom view with following changes:
private void init() {
this.mEditors = new HashMap<>();
mEditors.put("Other", new DataVO("other"));
setOrientation(VERTICAL);
}
I hope it will help.

Remove all children overflowed of their parent bounds

In a special android application, I want to remove all child views from their parents if entire or a part of them was overflowed of their parents bounds (on window focus changed).
I have tried to write manual calculations on children and parent widths but I am looking for an object oriented solution to be applicable on all kinds of views.
This would probably the easiest by just extending the ViewGroup you are using and removing the views after layouting them. (ViewGroups include LinearLayout, RelativeLayout, ...)
By overwriting onLayout you could iterate over your children and remove any views with bounds not within of their parent.
Things to note: This will probably trigger another layout call, so be sure to properly handle things.
#Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
// call super to layout children and have their bounds set
super.onLayout(changed, left, top, right, bottom);
// Iterate over all children
for (int i = 0; i < getChildCount(); i++) {
View child = getChildAt(i);
if (child.getLeft() < left
// ... add top / bottom
|| child.getRight() > right) {
removeView(child);
}
}
}
And if you want to handle things without extending the ViewGroup you can always handle things by using the same principle after the view got layouted.

get position of view with respect to top-most parent in android

To get a view relative to its parent, I can use getLeft(), getRight(), getTop(), and getBottom(). But how do I get a view relative to the top-most parent of the layout file?
So say my eve layout is RelativeLayout. Then say inside eve I have a number of nested layouts. And then deep within the hierarchy I have a EditText view. How do I get the location of the EditText view with respect to eve?
To find out absolute view location, you can use view.getLocationOnScreen() or view.getLocationInWindow().
View target = findViewById(R.id.target_id);
View parent = (View)target.getParent();
int x = target.getLeft();
int y = target.getTop();
while(parent != null){
x += parent.getLeft();
y += parent.getTop();
parent = (View)parent.getParent()
}
These code may be help you, because I didn't test it. I hope it would work.

Android Viewgroup: fade/blend two views on top of each other

I want to fade from one view to another on in a ViewGroup.
At the moment I'm doing the transition using setAlpha, but the problem is that only one view is being rendered, the one that was on top and is fading out.
Is the view-array inside ViewGroup an order by z-axis?
Is only the top view being rendered?
My layout method looks like this:
#Override
protected void onLayout(final boolean changed, final int l, final int t, final int r, final int b) {
L.debug("laying out {} children", this.getChildCount());
for (int i = 0; i < this.getChildCount(); i++) {
L.debug("layout out {}", i);
View view = this.getChildAt(0);
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
}
}
Why don't you want to use ViewSwitcher? It does exactly what you want. Here is an example.
Is the view-array inside ViewGroup an order by z-axis?
There is no such thing like Z-order in android. Views are drawn in oder they were added to ViewGroup. First added draws first.
Is only the top view being rendered
No, android will draw all views in visible rect even if they are totally overlaped by others.
I think you should fix this place this.getChildAt(0) and layout all childrens in your ViewGroup.

Categories

Resources