Reputation: 6371
I want to place textview on top of an imageview in android. Imageview is displaying a bitmap and I want to place textview on the top of imageview. I also want that wherever the imageview is touched, textview must appear there.What is the best way to go about doing that. Thanks
<LinearLayout
android:id="@+id/ll"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/iv_image2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:scaleType="fitXY"
android:adjustViewBounds="true"
/>
</LinearLayout>
// my activity
switch (action) {
case MotionEvent.ACTION_DOWN:
Bitmap.Config config = bm.getConfig();
int width = bm.getWidth();
int height = bm.getHeight();
bm2 = Bitmap.createBitmap(width, height, config);
c = new Canvas(bm2);
c.drawBitmap(bm, 0, 0, null);
iv2.setImageBitmap(bm2);
}
break;
Upvotes: 3
Views: 2354
Reputation: 50
Imageview
as per below. <ImageView
android:id="@+id/arrangementImageView"
android:layout_width="0dp"
android:layout_height="wrap_content"
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_arrangement);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
panelLayout = (ConstraintLayout) findViewById(R.id.panelLayout);
arrangementImageView = findViewById(R.id.arrangementImageView);
arrangementImageView.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
switch (event.getActionMasked()) {
case MotionEvent.ACTION_DOWN:
int offsetX = 10; // Up to you to set
int offsetY = 10;
TextView myObj = new TextView(v.getContext().getApplicationContext());
myObj.setCompoundDrawablesWithIntrinsicBounds(R.drawable.myImage, 0, 0, 0);
myObj.setTextViewId(1001);
panelLayout.addView(myObj, -1);
ConstraintSet set = new ConstraintSet();
set.clone(panelLayout);
set.connect(myObj.getId(), ConstraintSet.TOP, panelLayout.getId(), ConstraintSet.TOP, (int) event.getRawY() - offsetY);
set.connect(myObj.getId(), ConstraintSet.LEFT, panelLayout.getId(), ConstraintSet.LEFT, (int) event.getRawX() - offsetX);
set.applyTo(panelLayout);
break;
}
return false;
}
}
Upvotes: 0
Reputation: 569
This should solve your second problem! I haven't tested this code, but it should work:
Make sure you add implements View.OnTouchListener
to your class declaration!
imageView.setOnTouchListener(this);
// later on the in the code, add this method
public boolean onTouch(View v, MotionEvent event) {
switch(v.getId()) {
case R.id.imageViewId:
float xPosition = event.getX();
float yPosition = event.getY();
textView.setX(xPosition);
textView.setY(yPosition);
break;
}
}
Also, make the changes to the XML that 0mach0 suggested.
Upvotes: 1
Reputation: 5531
You can do it from different approaches like this one, using RealtiveLayout:
First the XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:src="@drawable/ic_launcher" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/textView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
Then the activity code:
public class LogDetailActivity extends Activity {
TextView mTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_log_detail);
ImageView imageView = (ImageView) findViewById(R.id.imageView);
mTextView = (TextView) findViewById(R.id.textView);
mTextView.setText("Example Text");
mTextView.setVisibility(View.GONE);
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mTextView.setVisibility(View.VISIBLE);
}
});
}
}
Upvotes: 2