I want it to form a word on the letters with the touchlistener. But when it comes to a letter, it repeatedly writes that letter. How do I get this to be taken in one go?
So if "harf1" is written as the letter of the word, it will not be written again.
Here is the code:
linearLayout.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getActionMasked()){
case MotionEvent.ACTION_DOWN:
if (isDownWord1(event.getX(),event.getY()))
yazi.setText("A");
if (isDownWord2(event.getX(),event.getY()))
yazi.setText("B");
if (isDownWord3(event.getX(),event.getY()))
yazi.setText("C");
if (isDownWord4(event.getX(),event.getY()))
yazi.setText("D");
break;
case MotionEvent.ACTION_MOVE:
if (isDownWord1(event.getX(),event.getY()))
yazi.setText(yazi.getText().toString() + "A");
if (isDownWord2(event.getX(),event.getY()))
yazi.setText(yazi.getText().toString() + "B");
if (isDownWord3(event.getX(),event.getY()))
yazi.setText(yazi.getText().toString() + "C");
if (isDownWord4(event.getX(),event.getY()))
yazi.setText(yazi.getText().toString() + "D");
break;
case MotionEvent.ACTION_UP:
yazi.setText("");
break;
}
return true;
}
});
}
public boolean isDownWord1(float x,float y){
if(x < harf1.getRight() && x > harf1.getLeft() && y > harf1.getTop() && y < harf1.getBottom())
return true;
else
return false;
}
public boolean isDownWord2(float x,float y){
if(x < harf2.getRight() && x > harf2.getLeft() && y > harf2.getTop() && y < harf2.getBottom())
return true;
else
return false;
}
public boolean isDownWord3(float x,float y){
if(x < harf3.getRight() && x > harf3.getLeft() && y > harf3.getTop() && y < harf3.getBottom())
return true;
else
return false;
}
public boolean isDownWord4(float x,float y){
if(x < harf4.getRight() && x > harf4.getLeft() && y > harf4.getTop() && y < harf4.getBottom())
return true;
else
return false;
}
That's XML File:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
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"
android:orientation="vertical"
android:background="@drawable/game_screen_bg"
tools:context=".GameLevel1">
<TextView
android:id="@+id/harf1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="93dp"
android:layout_marginLeft="93dp"
android:layout_marginTop="475dp"
android:text="A"
android:textSize="50dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/harf2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="408dp"
android:layout_marginEnd="63dp"
android:layout_marginRight="63dp"
android:text="B"
android:textSize="50dp"
app:layout_constraintEnd_toStartOf="@+id/harf3"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/harf3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="471dp"
android:layout_marginEnd="95dp"
android:layout_marginRight="95dp"
android:text="C"
android:textSize="50dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/harf4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="188dp"
android:layout_marginRight="188dp"
android:layout_marginBottom="114dp"
android:text="D"
android:textSize="50dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<TextView
android:id="@+id/yazi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="108dp"
android:layout_marginLeft="108dp"
android:layout_marginTop="200dp"
android:text="TextView"
android:textSize="50dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<LinearLayout
android:id="@+id/xxxx"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#10000000"
android:orientation="horizontal" />
</androidx.constraintlayout.widget.ConstraintLayout>
Before appending a character to the yazi
TextView
, you just need to check whether the last character of yazi
is not the same as the new character; so change the switch case of the MotionEvent.ACTION_MOVE
to:
case MotionEvent.ACTION_MOVE:
String text = yazi.getText().toString();
String lastChar = text.substring(text.length() - 1);
if (isDownWord1(event.getX(), event.getY()) && !lastChar.equals("A"))
yazi.setText(yazi.getText().toString() + "A");
if (isDownWord2(event.getX(), event.getY()) && !lastChar.equals("B"))
yazi.setText(yazi.getText().toString() + "B");
if (isDownWord3(event.getX(), event.getY()) && !lastChar.equals("C"))
yazi.setText(yazi.getText().toString() + "C");
if (isDownWord4(event.getX(), event.getY()) && !lastChar.equals("D"))
yazi.setText(yazi.getText().toString() + "D");
break;