Android Tutorial: Default field value and showing it as hint in edittext

Default field value and showing it as hint in edittext

When I started with Android development I was playing around with the notepad example app as a reference.
In this example they create a provider that inserts a default value in the title field. I wanted to use this example to create an editor that also uses a default value. I ran into the problem that when starting the edit activity the edit had the default value as text. This was expected because the provider fills the field with this default value, but it’s not really user friendly to make the user erase this default value. So I created a check to see if the text holds the default value, and if so only display it as a hint.

Default value at new record

First we fill the default value in our provider, in this example we filled the title field with a default value.
Note that we could also expand our MyColumns class with a default value property.

private Uri insertItem(Uri uri, ContentValues initialValues) {
    ContentValues _Values;
    if (initialValues != null) {
      	_Values = new ContentValues(initialValues);
    } else {
       	_Values = new ContentValues();
    }

    // Make sure that the fields are all set
    if (_Values.containsKey(MyColumns.TITLE) == false) {
        Resources _R = Resources.getSystem();
        _Values.put(MyColumns.TITLE, _R.getString(android.R.string.untitled));
    }
     
    //Insert record here
}

Setup the edit

Now when creating our activity set the hint of the edit

private EditText mEditTitle;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //Put your initialization code here

    //Get edit views and set events
    mEditTitle = (EditText) findViewById(R.id.my_editor_title);
    mEditTitle.setHint(getString(android.R.string.untitled));
} 

When setting the value of the edit we must check if the value equals our hint value.

private void loadRecord() {
    //Put your code here to set the cursor to correct position

    //Fill edits, check for default values
    String _Title = mCursor.getString(COLUMN_INDEX_TITLE);
    if (_Title.equals(mEditTitle.getHint().toString())){
       	mEditTitle.setText("");
    } else {
       	mEditTitle.setTextKeepState(_Title);
    }
}    


Check for default value

And last, when saving our values check if edit is empty

private final void saveMyItem() {
    ContentValues _values = new ContentValues();

    // Write our text back into the provider.
    String _Title = mEditTitle.getText().toString(); 
    if (_Title.equals("")){
        _values.put(ItemColumns.TITLE, mEditTitle.getHint().toString());           	
    } else {
        _values.put(ItemColumns.TITLE, _Title);            	
    }            	
        
    //Commit your item here
}    

And there you have it, a simple solution to show a default value but still have a user friendly GUI.

Leave a Reply

Your email address will not be published. Required fields are marked *