Question
On Android Studio Java, I have to make it so that when I click anywhere on an ImageView, which is placed near the middle of
On Android Studio Java, I have to make it so that when I click anywhere on an ImageView, which is placed near the middle of the screen in a Constraint Layout, a textView is programmatically placed there.
IN MY IMAGEVIEW TOUCH LISTENER:
image.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { DisplayMetrics displaymetric = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(displaymetric); //int width, height in pixel coordinates width = cookiePic.getLayoutParams().width; height = cookiePic.getLayoutParams().height; if(event.getAction() == MotionEvent.ACTION_DOWN){ //float xX, yY are the coordinates xX= (event.getX() / displaymetric.density) / (width / displaymetric.density); yY= (event.getY() / displaymetric.density) / (height / displaymetric.density); } return false; } });
The floats xX and yY give me the x and y positions within the imageView.
Given that I have:
TextView textview; //this is the textView I need to make appear wherever I click on the imagView ConstraintLayout layoutCon; //this is the ID of my layout
AND IN MY IMAGEVIEW CLICK LISTENER:
image.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) {
textview = new TextView(MainActivity.this); textview.setId(View.generateViewId()); textview.setText("APPEAR");
ConstraintLayout.LayoutParams layoutparams = new ConstraintLayout.LayoutParams(ConstraintLayout.LayoutParams.WRAP_CONTENT, ConstraintLayout.LayoutParams.WRAP_CONTENT); textview.setLayoutParams(layoutparams);
layoutCon.addView(textview);
ConstraintSet constraintSet = new ConstraintSet();
constraintSet.clone(layoutCon);
constraintSet.connect(textview.getId(), ConstraintSet.TOP, layoutCon.getId(), ConstraintSet.TOP);
constraintSet.connect(textview.getId(), ConstraintSet.BOTTOM, layoutCon.getId(), ConstraintSet.BOTTOM);
constraintSet.connect(textview.getId(), ConstraintSet.START, layoutCon.getId(), ConstraintSet.START);
constraintSet.connect(textview.getId(), ConstraintSet.END, layoutCon.getId(), ConstraintSet.END);
constraintSet.setHorizontalBias(textview.getId(), xX); constraintSet.setVerticalBias(textview.getId(), yY); constraintSet.applyTo(layoutCon);
} });
WITH THIS CODE, THE TEXTVIEW THAT NEEDS TO APPEAR GETS PLACED IN A DIFFERENT PLACE FROM WHERE I CLICK. THIS IS BECAUSE THE xX and yY ARE THE COORDINATES WITHIN THE IMAGEVIEW BUT WITH THIS CODE, IT IS USING THE xX and yY COORINATES AS A BIAS FOR THE ENTIRE SCREEN. How can I make it so that the textView appears exactly where I click on the ImageView? I need the modified code for that in Java Android Studio.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started