Search code examples
androidtextviewfocus

How to display result from calculator on focused textview on Android


I have a calculator input which displays the result in a textview.

I have two textviews and I want to display the result depending on which textview I clicked on, something like setting a focus and depending which textview was clicked, showing the calculator operation there.

I put

android:focusable="true"
android:focusableInTouchMode="true"

but this sets the textviews as edit text and I do not want it that way.


Solution

  • Set the texviews as edittext and block user input using

    edt1.setInputType(InputType.TYPE_NULL);
    edt2.setInputType(InputType.TYPE_NULL);
    

    then, based on the focus, set the result

    btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    
                    if(edt1.hasFocus()){
                        aux=aux+1;
                        edt1.setText(result);
                    }
                    if (edt2.hasFocus()){
                        aux = aux+1;
                        edt2.setText(result);
                    }
                }
            });