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: 329
Registered: ‎12-17-2010
My Device: Not Specified
My Carrier: developer

to use month calender display in my app

hiiiiiiiiiiii in my application i want take a Date from Displayed calender But i dont how to do that i did that by using date picker but my requirement is when i click button i have to puch a Screen (this Screen will contain the Calender Month Display )  when i click one particular date the Screen will pops then i have to take that date back to application please help me...  

Developer
Posts: 467
Registered: ‎07-16-2009
My Device: Z10 LE
My Carrier: Vodafone NL

Re: to use month calender display in my app

I don't know about such Screen in API. You have to implement it yourself. Look onGridFieldManager when you are going to start.

 

Thanks,

Eugen

Trusted Contributor
Posts: 151
Registered: ‎06-17-2010
My Device: Curve 8900
My Carrier: Vodafone

Re: to use month calender display in my app

Hi satish4ru,

 

You can achieve this by creating calendar popup screen.

 

you can add one button in your application and in click event of that button you have to open calendar pop up..

 

Here is the code to make calendar pop up..

 

 

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

public class CalendarPopupScreen extends PopupScreen {

    private static SimpleDateFormat sdfWeekDay = new SimpleDateFormat("E");
    private static SimpleDateFormat sdfMonth = new SimpleDateFormat("MMMM yyyy");

    private static String SINGLE_BLANK = " ";
    private static String FIELD_SIZE_STRING = " 30 ";

    private int _selectedDay = -1; // None selected

    private int _currentFocusDay;
    private int _currentMonth; // This is the month as usually defined.
                               // In Calendar, January is month 0.
    private String _currentMonthString;
    private int _currentYear;
    private Calendar _cl = Calendar.getInstance();

    private LabelField _prevMonthButton;
    private LabelField _currentMonthField;
    private LabelField _nextMonthButton;

    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 Field _initialFocusField = null;

    private Font _normalFont;
    private Font _boldFont;

    public CalendarPopupScreen() {
        this(new Date());
    }

    public CalendarPopupScreen(long dateMilliSecs) {
        this(new Date(dateMilliSecs));
    }

    public CalendarPopupScreen(Date selectedDate) {
        super(new VerticalFieldManager());
        _cl.setTime(selectedDate);
        createScreen(_cl.get(Calendar.DAY_OF_MONTH), _cl.get(Calendar.MONTH) + 1, _cl.get(Calendar.YEAR));
    }

    public CalendarPopupScreen(int focusDay, int startMonth, int startYear) {
        super(new VerticalFieldManager(VerticalFieldManager.VERTICAL_SCROLL | VerticalFieldManager.VERTICAL_SCROLLBAR));
        createScreen(focusDay, startMonth, startYear);
    }

    protected void onDisplay() {
        if ( _initialFocusField != null ) {
            _initialFocusField.setFocus();
            _initialFocusField = null;
        }
        super.onDisplay();
    }

    private void createScreen(int focusDay, int startMonth, int startYear) {
        // Initial values for Screen
        _currentFocusDay = focusDay;
        _currentMonth = startMonth;
        _currentYear = startYear;

        // Try to set Font based on Display Size so thjat Popup fits on Screen
        _normalFont = this.getFont().derive(Font.PLAIN);
        if ( _normalFont.getAdvance(FIELD_SIZE_STRING) > Display.getWidth()/10 ) {
            // Font too big, make it a bit smaller
            do {
                _normalFont = _normalFont.derive(Font.PLAIN, _normalFont.getHeight()-1);
            } while ( _normalFont.getAdvance(FIELD_SIZE_STRING) > Display.getWidth()/10 );
            this.setFont(_normalFont);
        }
        _boldFont = _normalFont.derive(Font.BOLD);

        // Create Month and button Fields once
        HorizontalFieldManager hfm = new HorizontalFieldManager(HorizontalFieldManager.FIELD_HCENTER);
        _prevMonthButton = new ButtonBorderedLabelField(SINGLE_BLANK + Characters.BLACK_LEFT_POINTING_TRIANGLE + SINGLE_BLANK,LabelField.FOCUSABLE);
        hfm.add(_prevMonthButton);
        _currentMonthField = new LabelField(SINGLE_BLANK + _currentMonthString + SINGLE_BLANK);
        hfm.add(_currentMonthField);
        _nextMonthButton = new ButtonBorderedLabelField(SINGLE_BLANK + Characters.BLACK_RIGHT_POINTING_TRIANGLE + SINGLE_BLANK,LabelField.FOCUSABLE);
        hfm.add(_nextMonthButton);
        this.add(hfm);
        this.add(new VerticalSpacerField(3));

        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);
        this.add(_monthManager);

        displayMonth();

    }


    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());
        _currentMonthField.setText(SINGLE_BLANK + _currentMonthString + SINGLE_BLANK);

        // Figure out where the month display needs to start
        int workDay = _cl.get(Calendar.DAY_OF_WEEK);
        int startAt = 0;
        switch (workDay) {
            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 < 7; i++ ) {
            String weekDay = sdfWeekDay.formatLocal(dayTime);
            HeaderBorderedLabelField hblf = new HeaderBorderedLabelField(weekDay.substring(0,1), LabelField.USE_ALL_WIDTH | DrawStyle.HCENTER);
            hblf.setFont(_boldFont);
            _monthManager.add(hblf);
            dayTime = dayTime + DateTimeUtilities.ONEDAY;
        }
        for ( int i = 0; i < 42; i++ ) { // Need at most 6 rows
            workDate.setTime(workDateTime);
            _cl.setTime(workDate);
            workDateTime = workDateTime + DateTimeUtilities.ONEDAY;
            BorderedLabelField blf = null;
            int actualDate = _cl.get(Calendar.DAY_OF_MONTH);
            String tempDateString = Integer.toString(actualDate);
            if ( _cl.get(Calendar.MONTH) == _currentMonth - 1 ) {
                blf = new BorderedLabelField(tempDateString, LabelField.USE_ALL_WIDTH | DrawStyle.HCENTER | LabelField.FOCUSABLE);
                if ( _currentFocusDay == actualDate ) {
                    _initialFocusField = blf;
                }
            } else
            if ( (i % 7 == 0) && (startOfMonth < _cl.getTime().getTime()) ) {
                // We have finished the month
                break;
            } else {
                blf = new UnfocusableBorderedLabelField(tempDateString, LabelField.USE_ALL_WIDTH | DrawStyle.HCENTER);
            }
            _monthManager.add(blf);
        }
        if ( this.isDisplayed() && _initialFocusField != null ) {
            _initialFocusField.setFocus();
            _initialFocusField = null;
        }
    }

    public boolean keyChar(char key, int status, int time) {
        boolean retval = false;
        switch (key) {
            case Characters.ENTER: {
                // We have selected something
                processFocus();
                retval = true;
                break;
            }
            case Characters.ESCAPE: {
                close();
                retval = true;
                break;
            }
            default:
                retval = super.keyChar(key, status, time);
                break;
       }
       return retval;
    }



    private boolean processFocus() {
        // Should deal with whatever the focsu is currently on
        Field focusField = this.getDelegate().getLeafFieldWithFocus();
        if ( focusField instanceof ButtonBorderedLabelField ) {
            _currentFocusDay = -1; // Leave focus on 'button'
            _initialFocusField = focusField;
            int monthIncrement = 33;
            if ( focusField == _prevMonthButton ) {
                monthIncrement = -3;
            }
            _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();
            return true;
        } else
        if ( focusField instanceof BorderedLabelField ) {
            LabelField lab = (LabelField) focusField;
            _selectedDay = Integer.parseInt(lab.getText());
            close();
            return true;
        }
        return false;
    }

    protected boolean trackwheelClick( int status, int time ) {
        // Use navigationClick if only supporting devices post 4.2
        if ( processFocus() ) {
            return true;
        }
        return super.trackwheelClick(status, time);
    }

    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();
    }

}

class BorderedLabelField extends LabelField {
    int width;
    int height;
    public BorderedLabelField(String text, long style) {
        super(text, style);
    }
    public void paint(Graphics g) {
        super.paint(g);
        width = this.getWidth();
        height = this.getHeight();
        g.drawRect(0, 0, width, height);
    }
}

class HeaderBorderedLabelField extends BorderedLabelField {
    public HeaderBorderedLabelField(String text, long style) {
        super(text, style | LabelField.NON_FOCUSABLE);
    }
    public void paint(Graphics g) {
        g.setBackgroundColor(Color.LIGHTBLUE);
        g.clear();
        super.paint(g);
    }
}

class UnfocusableBorderedLabelField extends BorderedLabelField {
    public UnfocusableBorderedLabelField(String text, long style) {
        super(text, style | LabelField.NON_FOCUSABLE);
    }
    public void paint(Graphics g) {
        g.setBackgroundColor(Color.WHITE);
        g.clear();
        super.paint(g);
    }
}

class ButtonBorderedLabelField extends BorderedLabelField {
    public ButtonBorderedLabelField(String text, long style) {
        super(text, style);
    }
    public void paint(Graphics g) {
        g.setBackgroundColor(Color.LIGHTGREEN);
        g.clear();
        super.paint(g);
    }
}

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

 Regards,

Albert Anks

 


 

feel free to press the kudos button  on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.

 

Developer
Posts: 329
Registered: ‎12-17-2010
My Device: Not Specified
My Carrier: developer

Re: to use month calender display in my app

what about TablelayoutManager in that class i think it is now theer in Api can you tell me about that...

Trusted Contributor
Posts: 151
Registered: ‎06-17-2010
My Device: Curve 8900
My Carrier: Vodafone

Re: to use month calender display in my app

 

Uses TableLayoutManager, which you can find here:

 

How To - Create a rich UI layout with TableLayoutManager
Article Number: DB-00783

 

http://www.blackberry.com/knowledgecenterpublic/livelink.exe/fetch/2000/348583/800332/800505/800508/...

 

 


feel free to press the kudos button  on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.

 

 

New Developer
Posts: 134
Registered: ‎01-11-2011
My Device: curve

Re: to use month calender display in my app

DateTimePicker datePicker = DateTimePicker.createInstance(Calendar.getInstance(), "yyyy-MM-dd", null);
                             if(datePicker.doModal()){
                             Calendar cal = datePicker.getDateTime();
                             int month1=cal.get(Calendar.MONTH);
                             int month=month1+1;
                             int year=cal.get(Calendar.YEAR);

 

 

 

It will work for the Ok button.