position view in relativelayout runtime - android

i want to add a view to a relativelayout.
this view must be added to a specified position.
i have this:
int x = ...;
int y = ...;
button.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
int x = (int) event.getRawX();
int y = (int) event.getRawY();
v.layout(x, y, x + v.getWidth(), y + v.getHeight());
return true;
}
});
relativelayout.addView(button);
button.layout(x, y, x + button.getWidth(), y + button.getHeight());
the ontouchlistener works great.. the view stays with my finger.
however the startpostion is always 0, 0... it doesn't matter what my x and y is.
anyone a idee?

i have a workaround.
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
lp.leftMargin = x;
lp.topMargin = y;
relativelayout.addView(tmpTextButtonView, lp);
it is not great to use margin for positioning floating views. however it works for me.

You might want to try setting the position of your button before you add it to the parent view. So just swap these two lines:
relativelayout.addView(button);
button.layout(x, y, x + button.getWidth(), y + button.getHeight());
to be this:
button.layout(x, y, x + button.getWidth(), y + button.getHeight());
relativelayout.addView(button);

Related

Creating button dynamically inside relative layout ontouch android

What I am doing:
I am trying to create a button dynamically onTouch
What is Happening:
I am able to create the button but the button is not created exactly
in the place I touch, instead its little bit in the bottom
right(might be adjustment of button in pixel).
How can i make sure i create the button exactly in the same place i
touch
#Override
public boolean onTouch(View v, MotionEvent event) {
//Get the x & y co-ordinates from the event
float x = event.getX();
float y = event.getY();
//Convert into Integer
int mX = (int) x;
int mY = (int) y;
//Perform Event on touch of canvas
performEventOnTouchOfCanvas(event, mX, mY);
return true;
}
private void performEventOnTouchOfCanvas(MotionEvent event,int mX, int mY) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
Point mPoint=new Point(mX,mY);
createButton(mPoint.x,mPoint.y);
break;
}
}
private void createButton(float x, float y) {
Button btn = new Button(ActDrawAreaTwo.this);
RelativeLayout.LayoutParams bp = new RelativeLayout.LayoutParams(40, 40);
bp.leftMargin = (int) x;
bp.topMargin = (int) y;
//Assign the Id to the button
btn.setLayoutParams(bp);
CommonFunctions.setBackgroundDrawable(btn, ActDrawAreaTwo.this, R.drawable.white_circle_dot);//Set Button Drawable
String mTag=String.valueOf((int)x )+","+ String.valueOf((int) y);
btn.setTag(mTag);
canvasLayoutId.addView(btn);
}
Note: canvasLayoutId is a relative layout
Android start drawing from views topmost left side. So when you pass coordinates, it will assume those are topmost left side of the button. If you want your button to appear in the middle of where you touch you need to change your coordinates with these:
x = x + button_width / 2
y = y + button_height / 2
In Android default padding is also same button's border, so you can find button's width and height with using this:
button_width = mButton.getPaddingRight() - mButton.getPaddingLeft();
button_height = mButton.getPaddingBottom() - mButton.getPaddingTop();
You can also use button.getWidth() and button.getHeight() assuming you are not using MATCH_PARENT or WRAP_CONTENT as your paramters.

GraphView how to Display Toast near the user click point

Hello i would like to ask, how can i Display Toast near the user click point Like on image below using GraphView library:
http://android-graphview.org/
Thanks for any advice
EDIT:
I tried it using
seriesSin = new GraphViewSeries("Sinus curve", new GraphViewSeries.GraphViewSeriesStyle(Color.rgb(200, 50, 00), 3), data);
// SET ON TOUCH
graphView.setOnTouchListener(new View.OnTouchListener(){
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN) {
int size = seriesSin.size();
float screenX = event.getX();
float screenY = event.getY();
float width_x = v.getWidth();
float viewX = screenX - v.getLeft();
float viewY = screenY - v.getTop();
float percent_x = (viewX/width_x);
int pos = (int) (size*percent_x);
System.out.println("X: " + viewX + " Y: " + viewY +" Percent = " +percent_x);
System.out.println("YVal = " +seriesSin.getY(pos));
return true;
}
return false;
}
});
But I cannot to get seriesSin.size and seriesSin.getY(pos)
You can't decide where to display the Toast. However, you could set up a custom view and show it using the coordinates (x and y) you get when detecting the touch. When you show the view, simply use those values to set up the layout params of the view, and voila.
Version 4.0.0 added an onTap() listener, that replicates your onTouch method above, it doesn't solve your Toast positioning issue, but could save you a few lines of code.

View animation while moving on Screen in android?

I had 2 views in an activity. One full screen (View1) and other small (View2), half the size of View1.
I can move View2, over View1 by catching onTouch listeners (ACTION_UP/ACTION_DOWN). But i want to show animation/dragging image of View2 while moving on View1. Any suggestion on how to implement this, will be appreciated.
Currently, for moving the View2 in four corner, i'm setting the Layout params in ACTION_UP.
Code::
public boolean onTouch(View view, MotionEvent motionEvent) {
RelativeLayout.LayoutParams layoutParams =(RelativeLayout.LayoutParams)view.getLayoutParams();
int dx=0,dy=0;
switch (motionEvent.getActionMasked()) {
case MotionEvent.ACTION_DOWN:
int x = (int) motionEvent.getX();
int y = (int) motionEvent.getY();
dx = (int) motionEvent.getRawX() - layoutParams.leftMargin;
dy = (int)motionEvent.getRawY() - layoutParams.topMargin;
break;
case MotionEvent.ACTION_MOVE:
layoutParams.leftMargin = (int) ((int) motionEvent.getRawX()-dx);
layoutParams.topMargin = (int) ((int)motionEvent.getRawY()- dy);
view.setLayoutParams(layoutParams);
break;
case MotionEvent.ACTION_UP:
view.setLayoutParams(layoutParams);
break;
}
return true;
}
Use ACTION_MOVE and calculate the offset by checking a pointer's x or y value. After that set weight or height/width parameter of the floating View.

Drawing line using drawPoint()

I am trying to draw line in android.But i am not satisfy with that because it doesn't draw full line it draws a dotted line when i move object fastly and it draws a complete line when i move object slowly.Plaese help me why this happenes.I want just complete line not dotted line.My code is here:
On Touch event of view:
public boolean onTouch(View view, MotionEvent event) {
// TODO Auto-generated method stub
final int X = (int) event.getRawX();
final int Y = (int) event.getRawY();
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) view
.getLayoutParams();
_xDelta = X - lParams.leftMargin;
Log.e("ACTION DOWN X", "" + Y + "---" + lParams.leftMargin);
_yDelta = Y - lParams.topMargin;
Log.e("ACTION DOWN Y", "" + Y + "---" + lParams.leftMargin);
break;
case MotionEvent.ACTION_UP:
break;
case MotionEvent.ACTION_POINTER_DOWN:
break;
case MotionEvent.ACTION_POINTER_UP:
break;
case MotionEvent.ACTION_MOVE:
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view
.getLayoutParams();
layoutParams.leftMargin = X - _xDelta;
Log.e("ACTION Move left margin", "" + (X - _xDelta));
layoutParams.topMargin = Y - _yDelta;
Log.e("ACTION Move top margin", "" + (Y - _yDelta));
layoutParams.rightMargin = -250;
layoutParams.bottomMargin = -250;
view.setBackgroundColor(random.nextInt());
view.setLayoutParams(layoutParams);
draw = new DrawLine(MainActivity.this, X - _xDelta, Y - _yDelta);
root.addView(draw);
break;
}
root.invalidate();
return true;
}
And my draw method is like this:
#Override
protected void onDraw(final Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
// paint.setColor(random.nextInt());
paint.setStyle(Style.STROKE);
paint.setStrokeWidth(4);
canvas.drawPoint(startX, startY, paint);
}
And i also want to know how to clear all the drawing.
Please help me to solve this.
Thank you.
So how do you expect that a line should be drawn when you only draw dots? The touch event registration/handling is not fast enough to get fired on each new pixel your finger touched. Use a path to store your points and draw a line/path using the points in your path.

Weird bug with setting LayoutParams margins

I'm having this weird issue with a custom TextView I'm working on. I'm trying to move the view by dragging it with a finger, so I get the touch position, do some math, and set the corresponding leftMargin and topMargin. It works, but I get some very weird behavior. The leftMargin part works perfectly, but the topMargin is very jumpy. It seems like it oscillates between the correct position and a position 25 pixels below it. When I only tap on the view instead of continuously dragging it, it moves down 25 pixels with every touch. Does anyone have any idea why this could be? The relevant code is here:
case MotionEvent.ACTION_MOVE :
{
final float x = event.getX();
final float y = event.getY();
final float newMarginX;
final float newMarginY;
positionX = x;
positionY = y;
newMarginX = oldMarginX - (lastTouchX - positionX);
newMarginY = oldMarginY - (lastTouchY - positionY);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(this.getWidth(), this.getHeight());
params.leftMargin = (int) newMarginX;
params.topMargin = (int) newMarginY;
this.setLayoutParams(params);
this.setText(Float.toString(y));
lastTouchX = positionX;
lastTouchY = positionY;
oldMarginX = newMarginX;
oldMarginY = newMarginY;
break;
}
Don't you need to call requestLayout?
You would just call it after your modifications (in this case after setLayoutParams).
http://developer.android.com/reference/android/view/View.html#requestLayout%28%29
make sure to set gravity:
params.gravity = Gravity.LEFT
before adjusting the margin.

Categories

Resources