I have a layout with 4 Buttons and text view and would make a small game by select letters to create a word.
My question is about how to get more than one button value in one single touch and move over the button? I tried by this code but get just one value every single touch and when move not get any value of other buttons
layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context=".MainActivity">
<TextView
android:id="@+id/txtresult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="102dp"
android:textColor="@android:color/holo_red_dark"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="25dp"
android:text="A" />
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="160dp"
android:text="T" />
<Button
android:id="@+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="90dp"
android:text="C" />
<Button
android:id="@+id/btn4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:layout_marginTop="82dp"
android:text="F" />
</RelativeLayout>
Activty
public class MainActivity extends AppCompatActivity implements View.OnTouchListener {
Button btn1, btn2, btn3, btn4;
TextView txtresult;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (Button) findViewById(R.id.btn1);
btn2 = (Button) findViewById(R.id.btn2);
btn3 = (Button) findViewById(R.id.btn3);
btn4 = (Button) findViewById(R.id.btn4);
txtresult = (TextView) findViewById(R.id.txtresult);
btn1.setOnTouchListener(this);
btn2.setOnTouchListener(this);
btn3.setOnTouchListener(this);
btn4.setOnTouchListener(this);
}
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
switch (view.getId()) {
case R.id.btn1:
txtresult.setText(txtresult.getText().toString() + btn1.getText().toString());
break;
case R.id.btn2:
txtresult.setText(txtresult.getText().toString() + btn2.getText().toString());
break;
case R.id.btn3:
txtresult.setText(txtresult.getText().toString() + btn3.getText().toString());
break;
case R.id.btn4:
txtresult.setText(txtresult.getText().toString() + btn4.getText().toString());
break;
}
}
return false;
}
}
Result should be like this:
I think you should set a touch listener on your layout's view and try to get X and Y and detect if X and Y of the touch point are on your buttons.
Take a look at Detect touch event on a view when dragged over from other view