Search code examples
androidandroid-edittextsettext

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


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

Solution

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