skip to Main Content

What is the method is to assign a default value in edittext? Like if user did not enter any value how to perform the required task by default value.

int hft=Integer.parseInt(takehtft.getText().toString());
int hin=Integer.parseInt(takehtin.getText().toString());

This is a simple code to take the height(in feet)and the height(in inch).
How to calculate the total height in feet if the user did not enter the inch height, by assuming the value of hin=0?

3

Answers


  1. You can auto-fill the inch edit text with a 0 value in onCreate.

    takehtin.setText("0");

    or you can check for

    takehtin.getText().isEmpty()

    if it is empty set the inch value to 0

    Login or Signup to reply.
  2. Try setting default value in XML file :-

    <EditText
            android:id="@+id/takehtft"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:imeOptions="actionDone"
            android:inputType="number"
            android:text="0"
            android:textColor="@color/black"
            android:textColorHint="@color/black"
            android:textSize="13sp"/>
    
    Login or Signup to reply.
  3. Check if edittext’s are empty, then don’t assign any value:

    // By Default, '0'
    int hft = 0;
    int hin = 0;
    if (takehtft.getText().toString() != "") {
        hft = Integer.parseInt(takehtft.getText().toString());
    }
    if (takehtin.getText().toString() != "") {
        hft = Integer.parseInt(takehtin.getText().toString());
    }
    Log.d("TAG", "onCreate: Ft:" + hft);
    Log.d("TAG", "onCreate: In:" + hin);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search