Hello I want to display a String on a TextView when the user touches a button. But I want this TextView to be shown on the same position where the user touches the screen. At the moment I have following code but it doesnt work out:
private void set(Integer name, Float x, Float y, Typeface font) {
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT);
layoutParams.setMargins(10, 1, 10, 1);
RelativeLayout nlap = new RelativeLayout(this);
nlap.setLayoutParams(layoutParams);
TextView tv1 = new TextView(this);
tv1.setX(x);
tv1.setY(y);
tv1.setText("+ " + String.valueOf(name));
tv1.setTypeface(font);
tv1.setGravity(Gravity.CENTER);
tv1.setTextSize(20);
nlap.addView(tv1);
}
And in the OnTouchListener:
int action = motionEvent.getAction();
int x = (int)motionEvent.getX();
int y = (int)motionEvent.getY();
set(multiplicator, Float.intBitsToFloat(x), Float.intBitsToFloat(y), font);
I hope you can help me. Thank You!
Declare textView global to that activity
TextView tv;
then in oncreate()
method
tv=(TextView) findViewById(R.id.your_xml_textview);
tv.setText("your desired text");
tv.setVisibility(View.INVISIBLE);
Or you can simply use android:visibility="invisible"
in your xml
Now ontouch
of the screen we get the co ordinate and put the textView on that point by below code
@Override
public boolean onTouchEvent(MotionEvent event) {
int corx = (int)event.getX();
int cory = (int)event.getY();
tv.setX(corx);
tv.setY(cory);
tv.setVisibility(View.VISIBLE);
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_MOVE:
case MotionEvent.ACTION_UP:
}
return false;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.