-1

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:

enter image description here

Ehsan Mashhadi
  • 2,568
  • 1
  • 19
  • 30
menasoft
  • 135
  • 1
  • 2
  • 12
  • What is the issue with your approach ? thats how it should be done, no ?.Give some example if possible – Krishna Sharma Aug 18 '18 at 13:36
  • @KrishnaSharma issue when i touch for example "C" get letter c and when move to another letter like "A" not get any thing i wouldto get any letter when i move ti it – menasoft Aug 18 '18 at 13:40
  • Ran your code, I could get all letters in bottom result textview. I press C texresult value "C", then pressed A now texresult value "CA" and so on... Please explain your requirement with good example. – Krishna Sharma Aug 18 '18 at 13:49
  • @menasoft: Did you solver the problem? – user1090751 Feb 29 '20 at 10:09
  • @user1090751 yes by this question answer https://stackoverflow.com/questions/12980156/detect-touch-event-on-a-view-when-dragged-over-from-other-view – menasoft Mar 01 '20 at 11:08
  • @menasoft: That code is not working for me. It shows only the last view i.e if i drag from ViewA to ViewB then it logs onTouch ViewB and if i drag from ViewB to ViewA then it logs onTouch ViewA. Please suggest. Thanks – user1090751 Mar 01 '20 at 15:25
  • @user1090751 you can make main array and temp array log views if you grag on viewA remove it from main array and add it to temp array – menasoft Mar 02 '20 at 17:33

2 Answers2

0

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

Ehsan Mashhadi
  • 2,568
  • 1
  • 19
  • 30
0

this is my idea, try it

@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
    if (motionEvent.getAction() == MotionEvent.ACTION_DOWN || motionEvent.getAction() == MotionEvent.ACTION_MOVE) {
        if(get_letter(motionEvent.getX(), motionEvent.getY() != "")
            result.append(get_letter(motionEvent.getX(), motionEvent.getY());
    }
}

private String get_letter(float x, float y) {
    if(is_contain(x, y, btn1)) return btn1.getText();
    else if(is_contain(x, y, btn2)) return btn2.getText();
    else if(is_contain(x, y, btn3)) return btn3.getText();
    else if(is_contain(x, y, btn4)) return btn4.getText();
    else return "";
}

private boolean is_contain(float x, float y, Button view) {
    RectF rect = new RectF();
    view.getHitRect(rect);
    return rect..contains((int)x, (int)y);
}
tainguyen
  • 85
  • 1
  • 14