4

How to set text programmatically for EditText control when its inputType is number ?

<EditText
    android:id="@+id/WorkingHoursET"
    android:layout_width="fill_parent"
     android:textColor="@color/white"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/WorkingHoursTV"
    android:layout_below="@+id/WorkingHoursTV"
    android:layout_marginTop="14dp"
    android:ems="10"
    android:inputType="number" />


    EditText workedHours = (EditText)findViewById(R.id.WorkedHoursET);
    workedHours.setText(8); //this is not working
    workedHours.setText("8"); //neither this
Joy
  • 1,707
  • 7
  • 29
  • 44

1 Answers1

4

Try with

workedHours.setText("8", BufferType.EDITABLE);

If you check the docs for EditText, you'll find a setText() method. It takes in a String and a TextView.BufferType.


NOTE : As Shailendra Singh Rajawat noted you might using wrong id of EditText. You should look into that too. That code should be

EditText workedHours = (EditText)findViewById(R.id.WorkingHoursET);

Community
  • 1
  • 1
Pankaj Kumar
  • 81,967
  • 29
  • 167
  • 186