Sumita Das
Sumita Das

Reputation: 47

How to Set a Default value in EditText

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?

Upvotes: 1

Views: 104

Answers (3)

UMESH0492
UMESH0492

Reputation: 1731

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

Upvotes: 2

Sohaib Ahmed
Sohaib Ahmed

Reputation: 3088

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

Upvotes: 1

Umang
Umang

Reputation: 158

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

Upvotes: 0

Related Questions