Android: Get the X and Y coordinates of a TextView?
- by Jep Knopz
I am working now on a project. I have 2 draggable textView in a circle. I want to add those value inside the circle when the circle is drag over the other circle. the first option that I have is to get the X and Y of the circle, but I get it. Can anyone fix my code?
Here is the Code:
MainActivity
public class MainActivity extends Activity {
int windowwidth;
int windowheight;
TextView bola;
TextView bola2;
private float x;
private float y;
private android.widget.RelativeLayout.LayoutParams layoutParams;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
windowwidth = getWindowManager().getDefaultDisplay().getWidth();
windowheight = getWindowManager().getDefaultDisplay().getHeight();
bola = (TextView) findViewById(R.id.ball);
bola2 = (TextView) findViewById(R.id.ball2);
bola2.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
layoutParams = (RelativeLayout.LayoutParams) bola2
.getLayoutParams();
switch (event.getActionMasked()) {
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_MOVE:
int x_cord = (int) event.getRawX();
int y_cord = (int) event.getRawY();
if (x_cord > windowwidth) {
x_cord = windowwidth;
}
if (y_cord > windowheight) {
y_cord = windowheight;
}
layoutParams.leftMargin = x_cord - 25;
layoutParams.topMargin = y_cord - 75;
bola2.setLayoutParams(layoutParams);
break;
default:
break;
}
return true;
}
});
bola.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
layoutParams = (RelativeLayout.LayoutParams) bola
.getLayoutParams();
switch (event.getActionMasked()) {
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_MOVE:
int x_cord = (int) event.getRawX();
int y_cord = (int) event.getRawY();
if (x_cord > windowwidth) {
x_cord = windowwidth;
}
if (y_cord > windowheight) {
y_cord = windowheight;
}
layoutParams.leftMargin = x_cord - 25;
layoutParams.topMargin = y_cord - 75;
bola.setLayoutParams(layoutParams);
break;
default:
break;
}
// TODO Auto-generated method stub
return true;
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}}
Activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id= "@+id/ball"
android:background="@drawable/bgshape"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
tools:context=".MainActivity" />
<TextView
android:id= "@+id/ball2"
android:background="@drawable/bgshape"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2"
tools:context=".MainActivity"
android:layout_x="60dp"
android:layout_y="20dp"
/>
The bgshape.xml(for the circle)
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<padding
android:bottom="20dp"
android:left="25dp"
android:right="25dp"
android:top="20dp" />
<stroke
android:width="2dp"
android:color="#000000" />
<solid android:color="#ffffff" />
<corners
android:bottomLeftRadius="30dp"
android:bottomRightRadius="30dp"
android:topLeftRadius="30dp"
android:topRightRadius="30dp" />
This code works well. Could anyone fix this so that I can add the value inside the circle when they hit each other?