Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: how to create calendar popup

Sorry 'dead' was the wrong choice of word.  I really mean, redundant.  With later OS's you get, for free, a DateTimePicker that I think looks better and is more consistent than the code I created.  That is all.

 

Will have a look at this bug later. 

Regular Contributor
Posts: 62
Registered: ‎04-06-2011
My Device: Blackberry developer
My Carrier: Blackberry developer

Re: how to create calendar popup

Since which os we have datetimepicker? And how i can create it? I'm developing usually for 4.6.
Thaks a lot!
Albert
Contributor
Posts: 33
Registered: ‎04-04-2011
My Device: 8350
My Carrier: Amazon

problem with MainScreen calendar

 

I am trying to code calendar Mainscreen  myself

I have used Calendar and SimpleDateFormat  for the purpose incrementing absolute time by 1 day (msec precision) and it didn't displayed right for certain days on March and November 2011 (days for start and end of daylight saving in central timezone, simulator's default timezone).

 

 

What made it happen ?

 

 

 

 

 

Contributor
Posts: 33
Registered: ‎04-04-2011
My Device: 8350
My Carrier: Amazon

Re: problem with MainScreen calendar

i did

Calendar.getInstance(TimeZone.getTimeZone("GMT"));

However date displayed using SimpleDateFormat changes by offset from device's timezone setting.

Shouldn't it be independent of device's timezone setting ??
Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: problem with MainScreen calendar

Can you show us the code you are using?

 

Also, have a look at that the

<SimpleDateFormat>.format(Calendar...)

methods rather than the

<SimpleDateFormat>.formatLocal(long...)

Contributor
Posts: 33
Registered: ‎04-04-2011
My Device: 8350
My Carrier: Amazon

Re: problem with MainScreen calendar

I redid the code using a global TimeZone variable and it works fine now, must have been some silly error,

 

Thank you for helping

Super Contributor
Posts: 278
Registered: ‎11-04-2010
My Device: 4.5 and above
My Carrier: none

Re: problem with MainScreen calendar

import net.rim.device.api.ui.container.PopupScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.Field;
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.DrawStyle;
import net.rim.device.api.system.Characters;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.util.DateTimeUtilities;
import net.rim.device.api.i18n.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 
 */
public class CalendarPopup extends PopupScreen implements FieldChangeListener {
    
    private static SimpleDateFormat sdfWeekDay = new SimpleDateFormat("Ei");
    private static SimpleDateFormat sdfMonth = new SimpleDateFormat("MMM yyyy");
    private Calendar _cl = Calendar.getInstance();
    
    private Bitmap TopBitmap = Bitmap.getBitmapResource("brushed-steel.jpg");
    private Bitmap MiddleBitmap = Bitmap.getBitmapResource("gold.jpg");
    private Bitmap FocusBitmap = Bitmap.getBitmapResource("cool-bg.jpg");
        
    private Bitmap PrevYearFocus = Bitmap.getBitmapResource("nav-left-x2-f.png");
    private Bitmap PrevMonthFocus = Bitmap.getBitmapResource("nav-left-f.png");
    private Bitmap NextMonthFocus = Bitmap.getBitmapResource("nav-right-f.png");
    private Bitmap NextYearFocus = Bitmap.getBitmapResource("nav-right-x2-f.png");
    private Bitmap PrevYear = Bitmap.getBitmapResource("nav-left-x2.png");
    private Bitmap PrevMonth = Bitmap.getBitmapResource("nav-left.png");
    private Bitmap NextMonth = Bitmap.getBitmapResource("nav-right.png");
    private Bitmap NextYear = Bitmap.getBitmapResource("nav-right-x2.png");
    
    private int VerticalOffset;
    private int HorizontalOffset;
    private final static long animationTime = 200;
    private long animationStart = 0;
    
    private static int [] tableStyles = new int [] {
        TableLayoutManager.FIXED_WIDTH,
        TableLayoutManager.FIXED_WIDTH,
        TableLayoutManager.FIXED_WIDTH,
        TableLayoutManager.FIXED_WIDTH,
        TableLayoutManager.FIXED_WIDTH,
        TableLayoutManager.FIXED_WIDTH,
        TableLayoutManager.FIXED_WIDTH
    };
    private int [] tableSizes = new int [7];
    private TableLayoutManager _monthManager; // This is where we display the Dates
    
    private static String FIELD_SIZE_STRING = " 30 ";
    private static String SINGLE_BLANK = " ";
    
    private int _currentFocusDay;
    private int _currentMonth; // This is the month as usually defined. // In Calendar, January is month 0.
    private int _currentYear;
    private String _currentMonthString = "MMM yyyy";
    private Field _initialFocusField = null;
    private int _selectedDay = -1; // None selected
    
    private Font _normalFont;
    private Font _boldFont;
    
    private ClickbleImage PrevYearBut, PrevMonthBut, NextMonthBut, NextYearBut;
    private BorderedLabel MonthDisplay,Today;
    
    public CalendarPopup(Date selectedDate) {
        super(new VerticalFieldManager(){
                public void sublayout(int width, int height) {
                    super.sublayout(Display.getWidth(),height);
                }
            });
        _cl.setTime(selectedDate);
        createScreen(_cl.get(Calendar.DAY_OF_MONTH), _cl.get(Calendar.MONTH) + 1, _cl.get(Calendar.YEAR));
    }
    
    private void displayMonth() {
        _monthManager.deleteAll(); // Delete the stuff currently there
        
        // Determine start of the Month
        _cl.set(Calendar.DAY_OF_MONTH, 1);
        _cl.set(Calendar.MONTH, _currentMonth - 1);
        _cl.set(Calendar.YEAR, _currentYear);
        _cl.set(Calendar.HOUR_OF_DAY, 12);
        _cl.set(Calendar.MINUTE, 0);
        _cl.set(Calendar.SECOND, 0);
        _cl.set(Calendar.MILLISECOND, 1);
        long startOfMonth = _cl.getTime().getTime();  

        // set Month in 'header'
        _currentMonthString = sdfMonth.formatLocal(_cl.getTime().getTime());

        // Figure out where the month display needs to start
        int workDay = _cl.get(Calendar.DAY_OF_WEEK);
        int startAt = 0;
        switch (workDay) {
            case(Calendar.MONDAY): {
                startAt = 0;
                break;
            }
            case(Calendar.TUESDAY): {
                startAt = -1;
                break;
            }
            case(Calendar.WEDNESDAY): {
                startAt = -2;
                break;
            }
            case(Calendar.THURSDAY): {
                startAt = -3;
                break;
            }
            case(Calendar.FRIDAY): {
                startAt = -4;
                break;
            }
            case(Calendar.SATURDAY): {
                startAt = -5;
                break;
            }
            case(Calendar.SUNDAY): {
                startAt = -6;
                break;
            }
        }
        Date workDate = _cl.getTime();
        long workDateTime = workDate.getTime() + ((long)startAt) * ((long)DateTimeUtilities.ONEDAY);

        long dayTime = workDateTime;
        for ( int i = 0; i < 42; i++ ) { // Need at most 6 rows
            workDate.setTime(workDateTime);
            _cl.setTime(workDate);
            workDateTime = workDateTime + DateTimeUtilities.ONEDAY;
            BorderedLabel blf = null;
            int actualDate = _cl.get(Calendar.DAY_OF_MONTH);
            String tempDateString = Integer.toString(actualDate);
            int textColor = Color.BLACK;
            if ( _cl.get(Calendar.MONTH) == _currentMonth - 1 ) {
                if ( _currentFocusDay == actualDate ) {
                    _initialFocusField = blf;
                    textColor = Color.RED;
                }
                blf = new BorderedLabel(tempDateString, LabelField.USE_ALL_WIDTH | DrawStyle.HCENTER | LabelField.FOCUSABLE,textColor);
            } else
            if ( (i % 7 == 0) && (startOfMonth < _cl.getTime().getTime()) ) {
                // We have finished the month
                break;
            } else {
                blf = new BorderedLabel(tempDateString, LabelField.USE_ALL_WIDTH | DrawStyle.HCENTER,Color.WHITE);
            }
            blf.setChangeListener(this);
            _monthManager.add(blf);
        }
        if ( this.isDisplayed() && _initialFocusField != null ) {
            _initialFocusField.setFocus();
            _initialFocusField = null;
        }
        MonthDisplay.SetText(_currentMonthString);
    }
    
    private void createScreen(int focusDay, int startMonth, int startYear) {
        // Initial values for Screen
        _currentFocusDay = focusDay;
        _currentMonth = startMonth;
        _currentYear = startYear;
        final VerticalFieldManager topManager = new VerticalFieldManager(FIELD_HCENTER){
                public void paint(Graphics g){
                    for(int i = 1; i < getWidth(); i++){
                        g.drawBitmap(i,0,1,getHeight(),TopBitmap,0,0);
                    }
                    super.paint(g);
                }
            };
        
        int columnSize = this.getFont().getAdvance(FIELD_SIZE_STRING);
        for ( int i = 0; i < tableSizes.length; i++ ) {
            tableSizes[i] = columnSize;
        }
        _monthManager = new TableLayoutManager(tableStyles, tableSizes, 0, TableLayoutManager.FIELD_HCENTER){
                public void paint(Graphics g){
                    for(int i = 1; i < getWidth(); i++){
                        g.drawBitmap(i,0,1,getHeight(),MiddleBitmap,0,0);
                    }
                    super.paint(g);
                }
            };
        
        VerticalFieldManager bottomManager = new VerticalFieldManager(FIELD_HCENTER){
                public void paint(Graphics g){
                    for(int i = 1; i < getWidth(); i++){
                        g.drawBitmap(i,0,1,getHeight(),TopBitmap,0,0);
                    }
                    super.paint(g);
                }
                protected void sublayout(int width, int height) {
                    super.sublayout(width, height);
                    setExtent(topManager.getWidth(), 30);
                }  
            };
        PrevYearBut = new ClickbleImage(PrevYearFocus,PrevYear,FIELD_HCENTER | FOCUSABLE);
        PrevYearBut.setChangeListener(this);
        PrevMonthBut = new ClickbleImage(PrevMonthFocus,PrevMonth,FIELD_HCENTER | FOCUSABLE);
        PrevMonthBut.setChangeListener(this);
        MonthDisplay = new BorderedLabel(_currentMonthString,FOCUSABLE,Color.BLACK);
        MonthDisplay.setChangeListener(this);
        NextMonthBut = new ClickbleImage(NextMonthFocus,NextMonth,FIELD_HCENTER | FOCUSABLE);
        NextMonthBut.setChangeListener(this);
        NextYearBut = new ClickbleImage(NextYearFocus,NextYear,FIELD_HCENTER | FOCUSABLE);
        NextYearBut.setChangeListener(this);
        HorizontalFieldManager hfm  = new HorizontalFieldManager(FIELD_HCENTER);
        hfm.add(PrevYearBut);hfm.add(new VerticalSpacer(5,3));
        hfm.add(PrevMonthBut);hfm.add(new VerticalSpacer(5,3));
        hfm.add(MonthDisplay);hfm.add(new VerticalSpacer(5,3));
        hfm.add(NextMonthBut);hfm.add(new VerticalSpacer(5,3));
        hfm.add(NextYearBut);
        topManager.add(hfm);
        
        TableLayoutManager _daysManager = new TableLayoutManager(tableStyles, tableSizes, 0,TableLayoutManager.FIELD_HCENTER);
        String [] days = {"Mo","Tu","We","Th","Fr","Sa","Su"};
        for(int i = 0 ; i < days.length;i++){
            _daysManager.add(new BorderedLabel(days[i],NON_FOCUSABLE | FIELD_HCENTER,Color.WHITE));
        }
        topManager.add(_daysManager);
        displayMonth();
        
        HorizontalFieldManager hfm1  = new HorizontalFieldManager(FIELD_HCENTER |FIELD_VCENTER);
        Today = new BorderedLabel("Today",FOCUSABLE | FIELD_HCENTER |FIELD_VCENTER,Color.BLACK);
        Today.setChangeListener(this);
        hfm1.add(new VerticalSpacer(30,85));hfm1.add(Today);hfm1.add(new VerticalSpacer(30,85));
        bottomManager.add(hfm1);
        
        this.add(topManager);
        this.add(_monthManager);
        this.add(bottomManager);
    }
    protected void onDisplay() {
        if ( _initialFocusField != null ) {
            _initialFocusField.setFocus();
            _initialFocusField = null;
        }
        super.onDisplay();
    }
    protected void applyTheme(Graphics g, boolean on){}
    public void fieldChanged(Field field, int context){
        _currentFocusDay = -1; // Leave focus on 'button'
        _initialFocusField = field;
        int monthIncrement = 33;
        if(field instanceof ClickbleImage){
            if(field == PrevYearBut){
                _currentYear = _cl.get(Calendar.YEAR)-1;
                displayMonth();
            }else if(field == PrevMonthBut){
                monthIncrement = -3;
                processMonth(monthIncrement);
            }else if(field == NextMonthBut){
                processMonth(monthIncrement);
            }else if(field == NextYearBut){
                _currentYear = _cl.get(Calendar.YEAR)+1;
                displayMonth();
            }
        }else if(field instanceof BorderedLabel){
            if(field == MonthDisplay){
            }else if(field == Today){
                _cl.setTime(new Date());
                _currentFocusDay = _cl.get(Calendar.DAY_OF_MONTH);
                _currentMonth = _cl.get(Calendar.MONTH) + 1;
                _currentYear = _cl.get(Calendar.YEAR);
                displayMonth();
            }else {
                LabelField lab = (LabelField) field;
                _selectedDay = Integer.parseInt(lab.getText());
                close();
            }
        }
    }
    private void processMonth(int monthIncrement){
        _cl.set(Calendar.DAY_OF_MONTH, 1);
        _cl.set(Calendar.MONTH, _currentMonth-1);
        _cl.set(Calendar.YEAR, _currentYear);
        Date workDate = _cl.getTime();
        workDate.setTime(workDate.getTime() + (((long)monthIncrement) * ((long)DateTimeUtilities.ONEDAY)));
        _cl.setTime(workDate);
        _currentMonth = _cl.get(Calendar.MONTH) + 1;
        _currentYear = _cl.get(Calendar.YEAR);
        displayMonth();
    }
    public void close() {
        UiApplication.getUiApplication().popScreen(this);
    }
    public Date getSelectedDate() {
        if ( _selectedDay == -1 ) {
            return null;
        }
        Calendar cl = Calendar.getInstance();
        cl.set(Calendar.YEAR, _currentYear);
        cl.set(Calendar.MONTH, _currentMonth - 1);
        cl.set(Calendar.DAY_OF_MONTH, _selectedDay);
        cl.set(Calendar.HOUR_OF_DAY, 0);
        cl.set(Calendar.MINUTE, 0);
        cl.set(Calendar.SECOND, 0);
        cl.set(Calendar.MILLISECOND, 1);
        return cl.getTime();
    }
    public boolean keyChar(char key, int status, int time) {
        boolean retval = false;
        switch (key) {
            case Characters.ENTER: {
                // We have selected something
                break;
            }
            case Characters.ESCAPE: {
                close();
                retval = true;
                break;
            }
            default:
                break;
       }
       return retval;
    }
} 

class ClickbleImage extends Field {  
    private Bitmap bitmap1,bitmap2,img;
    private boolean isFocus = false;
    public ClickbleImage(Bitmap bitmap1,Bitmap bitmap2,long style) {         
        super(style);
        this.bitmap1 = bitmap1;
        this.bitmap2 = bitmap2;
        img = bitmap1;
    }      
    protected boolean navigationClick(int status, int time) {  
        fieldChangeNotify(0);
        return true;     
    }    
    protected void onUnfocus() {
        isFocus = false;          
        super.onUnfocus();  
        img =  bitmap1;    
        invalidate();   
    } 
    protected void onFocus(int direction) {
        super.onFocus(direction);   
        isFocus = true;  
        img =  bitmap2;    
        invalidate();
    }
    public void paint(Graphics g){
        g.drawBitmap((this.getWidth()-img.getWidth())/2,(this.getHeight()-img.getHeight())/2,img.getWidth(),img.getHeight(),img,0,0);
    }
    protected void layout(int w, int h){
        setExtent(20,20);
    }    
    protected void drawFocus(Graphics graphics, boolean on) {
    }
} 

class BorderedLabel extends LabelField {
    int width,height,color;
    private String text;
    private Font font;
    public BorderedLabel(String text, long style,int TextColor) {
        super(text, style);
        SetFont(Font.PLAIN,15);
        this.color = TextColor;
        this.text = text;
    }
    public void SetText(String text){
        this.text = text;
        invalidate();
    }
    public void paint(Graphics g) {
        width = this.getWidth();
        height = this.getHeight();
        if(isFocus()){
            g.setColor(Color.WHITE);  
        }else {
            g.setColor(color);  
        }
        g.drawText(text, (getWidth() - font.getAdvance(text))/2, (getHeight() - font.getHeight())/2);   
        if(isFocus()){
            g.setColor(Color.RED);
            g.drawRect(0,0,width,height);
        }
    }   
    private void SetFont(int font_style, int font_size) {
        font = Font.getDefault().derive(font_style,font_size);
        this.setFont(font);
    }
    protected boolean navigationClick(int status, int time) {  
        fieldChangeNotify(0);
        return true;     
    }  
}

class VerticalSpacer extends Field {
    // Only for VerticalFieldManagers
    private int _height;
    private int _width;
    public VerticalSpacer(int height, int width) {
        super(Field.NON_FOCUSABLE);
        _height = height;
        _width = width;
    }
    public void layout(int width, int hieght) {
        setExtent(_width, _height);
    }
    public void paint(Graphics g) {
    }
}

 Thanks & Regards

                pp

Hi,

 

here is a calendarPopup

i have tested in simulators -- 4.5 and storm..

i have used perter_strange code and designed like this

http://www.dynarch.com/projects/calendar/ and i have used buttons and background of this only.

 

here is the code.. if any error, please correct me.

 

Developer
Posts: 261
Registered: ‎08-11-2009
My Device: Not Specified

Re: problem with MainScreen calendar

Hi PP,

 

 

 Cn u plz add all those images too . so that it can be tested by all the other usrers .

 

 

Thanks

SujithRavindran

Super Contributor
Posts: 278
Registered: ‎11-04-2010
My Device: 4.5 and above
My Carrier: none

Re: problem with MainScreen calendar

Hi, sujith

 

   can u able to see the link down... go to that and download that css file and you will get all images. and wat i have did is made it one pixel width and given inside for loop.

 

and i think for BorderedLabel class i have not given code for focus and unfocus so u can paste this inside that class


    protected void onFocus(int direction) {
        super.onFocus(direction);         
        invalidate();  
    }
   
    protected void onUnfocus() {
        super.onUnfocus();      
        invalidate();  
    }

 

if u dint get the images then just give some color and remove those bitmaps... but u should give images for next and prev buttons.
   

 

Thanks & Regards

             pp

New Contributor
Posts: 4
Registered: ‎09-22-2011
My Device: Storm 2
My Carrier: wifi

Re: how to create calendar popup

Hi Peret .. Thanx .. This is really great Calendar .. it is working fine... But i have little problem ...i have implemented this calendar in my my way using some images for battter look..  i have increse the height of this calendar when i check this in different device .. it is working fine in 9550 & 9700 but when i open this in 8520 than only few day which is display in screen i can see.. and bottom date i cant see ... i have tried to invalidate this Table but remaining date which is not display in scroll i cant see that... so please suggest me what can i do to resolve this ?