0
public void updateDisplay(View v)
{
    Button button = (Button) findViewById(R.id.button1);
    
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            tempNumber.setText(tempNumber.getText().toString() + ((Button) view).getText());
        }
    });
    tempNumber.setText( tempNumber.getText().toString() + ((Button) v).getText() );
    
    
}

My Java code:

<Button
     android:id="@+id/button1"
     android:layout_width="0dip"
     android:layout_height="wrap_content"
     android:layout_weight="0.1"
     android:background="@drawable/button"
     android:onClick="updateDisplay"
     android:text="1"
     android:layout_marginTop="5dp"
     android:layout_marginBottom="5dp"
     android:textColor="@drawable/button_tx_color"
     android:textSize="15sp" />

My XML code:

When I click buttons from 0 to 9, I create numbers. But I also got dot . and minus - signs. How can I check in my textview to allow only one dot in my numbers and allow minus at the top of my number?

Tot Zam
  • 8,406
  • 10
  • 51
  • 76
coreprojectz
  • 134
  • 1
  • 3
  • 12
  • If you use text input to add numbers then you can add digits in XML where you have to set allowed digits and the rest will not be allowed to input property is android:digits="0123456789" – Madhav Jan 13 '23 at 09:50

1 Answers1

0

I don't think you can achieve this via layout attribute alone.

Hoever you can base your validation on this answer, which is based on that older one. Basically you implement a TextWatcher for your EditText, which has its method invoked on every text change.

From then on you need only proper regex validation and something like that should do:

String pattern = "^[0-9]+\.[0-9]*$";
String yourEditTextValue;
if (yourEditTextValue.matches(pattern)) {
} else {
   // set edit text valeu to empty string, show erorr
}
Community
  • 1
  • 1
Boris Strandjev
  • 46,145
  • 15
  • 108
  • 135