Reputation: 47
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
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
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
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