I am making an meme maker application so i am allowing the user to add textviews to the image and drag the textview anywhere in the imageview. Initially all textviews are in 'GONE' visibility and when user clicks on 'Add text' button, i will make one textview visible at a time. I am able to drag and drop the textview anywhere i like but the onclicklistener is not working. I want to show the user that they clicked on the textview and give them options to edit the text like color,size etc. But the toast is never showing up, no errors as well. The code for it is as follows:
class MemeActivity : AppCompatActivity(){
var mLayoutRoot: RelativeLayout? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_meme)
tv1.setClickable(true);
setSupportActionBar(meme_toolbar)
supportActionBar!!.setDisplayHomeAsUpEnabled(true)
meme_toolbar.setNavigationOnClickListener {
finish()
}
val intent=intent
val memeImage=intent.getStringExtra("image")
Picasso.get().load(memeImage).into(meme_image)
mLayoutRoot=findViewById(R.id.rl_meme)
val added=findViewById<Button>(R.id.btn_add_text)
val save=findViewById<ImageView>(R.id.save_meme)
added.setOnClickListener {
if(tv1.visibility==View.GONE){
tv1.visibility=View.VISIBLE
}
else if (tv2.visibility==View.GONE){
tv2.visibility=View.VISIBLE
}
else if (tv3.visibility==View.GONE){
tv3.visibility=View.VISIBLE
}
else{
Toast.makeText(this,"No More",Toast.LENGTH_SHORT).show()
}
}
tv1.setOnClickListener {
Toast.makeText(this,"clicked tv1",Toast.LENGTH_SHORT).show()
}
tv1.setOnTouchListener(mOnTouchListenerTv2)
tv2.setOnTouchListener(mOnTouchListenerTv2)
tv3.setOnTouchListener(mOnTouchListenerTv2)
}
override fun onBackPressed() {
if (!mLayoutRoot!!.isFocused) {
mLayoutRoot!!.requestFocus()
mLayoutRoot!!.requestFocusFromTouch()
} else {
super.onBackPressed()
}
}
val mOnTouchListenerTv2: View.OnTouchListener = object : View.OnTouchListener {
var prevX = 0
var prevY = 0
override fun onTouch(v: View, event: MotionEvent): Boolean {
val par =
v.layoutParams as RelativeLayout.LayoutParams
when (event.action) {
MotionEvent.ACTION_MOVE -> {
par.topMargin += event.rawY.toInt() - prevY
prevY = event.rawY.toInt()
par.leftMargin += event.rawX.toInt() - prevX
prevX = event.rawX.toInt()
v.layoutParams = par
return true
}
MotionEvent.ACTION_UP -> {
par.topMargin += event.rawY.toInt() - prevY
par.leftMargin += event.rawX.toInt() - prevX
v.layoutParams = par
return true
}
MotionEvent.ACTION_DOWN -> {
prevX = event.rawX.toInt()
prevY = event.rawY.toInt()
par.bottomMargin = -2 * v.height
par.rightMargin = -2 * v.width
v.layoutParams = par
return true
}
}
return false
}
}
}
The XML code goes as below:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rl_meme"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MemeActivity">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/app_bar_layout_meme"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="0dp"
android:layout_marginTop="0dp"
android:background="@android:color/white">
<androidx.appcompat.widget.Toolbar
android:id="@+id/meme_toolbar"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@color/colorDarkGrey">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/app_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Meme"
android:textSize="20sp"
android:maxLines="1"
android:textStyle="bold"
android:textColor="@android:color/white"
android:layout_centerVertical="true"/>
<ImageView
android:id="@+id/save_meme"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentEnd="true"
android:src="@drawable/ic_save_black_24dp"
android:layout_marginEnd="5dp"
android:contentDescription="Meme" />
</RelativeLayout>
</androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.AppBarLayout>
<LinearLayout
android:id="@+id/ll_overall_textview"
android:layout_width="match_parent"
android:layout_height="60dp"
android:gravity="center"
android:background="@color/colorDarkGrey"
android:orientation="vertical"
android:layout_below="@id/app_bar_layout_meme">
<LinearLayout
android:id="@+id/ll_textview"
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal"
android:gravity="center"
android:padding="2dp"
android:layout_marginTop="2dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="10dp">
<ImageView
android:id="@+id/iv_textsize"
android:layout_width="1dp"
android:layout_height="wrap_content"
android:background="@drawable/box"
android:src="@drawable/textsize1"
android:textColor="#6E6B6B"
android:textSize="13sp"
android:layout_margin="2dp"
android:layout_weight="1"
android:textAllCaps="false"
style="?android:attr/buttonBarButtonStyle"/>
<ImageView
android:id="@+id/iv_textcolor"
android:layout_width="1dp"
android:layout_height="wrap_content"
android:background="@drawable/box"
android:scaleType="fitCenter"
android:textColor="@color/colorGrey"
android:layout_margin="2dp"
android:textAllCaps="false"
android:layout_weight="1"
android:src="@drawable/textcolor"
style="?android:attr/buttonBarButtonStyle"/>
<ImageView
android:id="@+id/iv_italic"
android:layout_width="1dp"
android:layout_height="wrap_content"
android:scaleType="fitCenter"
android:background="@drawable/box"
android:src="@drawable/italic"
android:layout_margin="2dp"
android:textColor="@color/colorGrey"
android:textAllCaps="false"
android:layout_weight="1"
style="?android:attr/buttonBarButtonStyle"/>
<ImageView
android:id="@+id/iv_bold"
android:layout_width="1dp"
android:layout_height="wrap_content"
android:scaleType="fitCenter"
android:layout_margin="2dp"
android:background="@drawable/box"
android:textColor="@color/colorGrey"
android:src="@drawable/bold"
android:textAllCaps="false"
android:layout_weight="1"
style="?android:attr/buttonBarButtonStyle"/>
<ImageView
android:id="@+id/iv_delete"
android:layout_width="1dp"
android:layout_height="wrap_content"
android:scaleType="fitCenter"
android:layout_margin="2dp"
android:background="@drawable/box"
android:textColor="@color/colorGrey"
android:src="@drawable/delete"
android:textAllCaps="false"
android:layout_weight="1"
style="?android:attr/buttonBarButtonStyle"/>
</LinearLayout>
</LinearLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentStart="true"
android:layout_marginTop="0dp">
<RelativeLayout
android:id="@+id/fl_meme_container"
android:layout_width="match_parent"
android:layout_height="420dp"
android:layout_marginTop="152dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:gravity="center_horizontal">
<ImageView
android:id="@+id/meme_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY" />
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textColor="#000000"
android:text="TEXT"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:visibility="gone"
tools:visibility="visible"
android:tag="1"/>
<TextView
android:id="@+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textColor="#000000"
android:text="TEXT"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:visibility="gone"
tools:visibility="visible"
android:tag="2"/>
<TextView
android:id="@+id/tv3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textColor="#000000"
android:text="TEXT"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:visibility="gone"
tools:visibility="visible"
android:tag="3"/>
</RelativeLayout>
<Button
android:id="@+id/btn_add_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@id/fl_meme_container"
android:layout_marginStart="40dp"
android:layout_marginEnd="40dp"
android:text="Add Text"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/fl_meme_container" />
</androidx.constraintlayout.widget.ConstraintLayout>
</RelativeLayout>
please help me how to make onClicklistener work?