Home > Net >  How to Set a Default value in EditText
How to Set a Default value in EditText

Time:12-26

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?

CodePudding user response:

You can auto fill the inch edit text with 0 value in onCreate itself.

takehtin.setText("0");

or you can check for

takehtin.getText().isEmpty()

if it is empty set the inch value to 0

CodePudding user response:

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"/>

CodePudding user response:

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);
  • Related