blackberryapplication-designblackberry-editfield

Change color of only the label for TextField in Blackberry


I am trying to a add a TextField. I am using EditField _textBox = new EditField("Subject", "Some text"); for creating a textbox with label as Subject. I want to change the color of only the label(Subject) of the textbox.


Solution

  • You're going to need a custom field to do this because it is not possible to change the colour of the EditField's label, even if you override EditField.paint().

    My suggestion is:

    Here's the code:

    import net.rim.device.api.ui.component.EditField;
    import net.rim.device.api.ui.component.LabelField;
    import net.rim.device.api.ui.container.HorizontalFieldManager;
    import net.rim.device.api.ui.Graphics;
    
    public class CustomEditField extends HorizontalFieldManager{
    
        private static final int COLOR = 0x00FF0000; //colour for the label 
        private LabelField labelField; //for the label
        private EditField editField; //for the editable text
    
        public CustomEditField(String label, String initialValue){
    
            labelField = new LabelField(label){
    
                public void paint(Graphics g){
    
                g.setColor(COLOR);
                    super.paint(g);
                }
    
            };
    
            editField = new EditField("", initialValue); //set the label text to an empty string
    
            add(labelField);
            add(editField);     
        }   
    }
    

    Of course, you're still going to need to add in your methods to set and get the text from your EditField, and any other specific methods which you need from EditField, but as a proof of concept this works.