I try to implement a Chat window( Chat functionality work fine & smooth). I am facing problem in designing Middle & Bottom part. In middle part chat messages & bottom i want to add editable field. If i fixed editable field align bottom chat messages not show, & if i add editable field after bottom, then chat messages shown on screen.
I have used NegativeMarginVerticalFieldManager.
I want field attached bottom of screen & messages show middle with scrollbar. Here i also attach code that i used in chat with dummy messages(without Json data). Thanks
package mypackage;
import net.rim.device.api.system.*;
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;
import net.rim.device.api.ui.decor.*;
public class ChatList extends MainScreen
{
Manager _foreground = new NegativeMarginVerticalFieldManager( USE_ALL_WIDTH | USE_ALL_HEIGHT | VERTICAL_SCROLL );
BasicEditField msg;
public ChatList() {
super( NO_VERTICAL_SCROLL );
setTitle( "Chat" );
// Set the linear background.
this.getMainManager().setBackground(
BackgroundFactory.createLinearGradientBackground(0x91e7ff,0x0099CCFF,0x00336699,0x91e7ff)
);
// Add Field Bottom
HorizontalFieldManager hfm = new HorizontalFieldManager(USE_ALL_HEIGHT | USE_ALL_WIDTH);
VerticalFieldManager vfm = new VerticalFieldManager(USE_ALL_WIDTH | FIELD_BOTTOM);
msg = new BasicEditField();
msg.setBorder(BorderFactory.createRoundedBorder(new XYEdges(3, 3, 3, 3), 0x999999, Border.STYLE_FILLED));
msg.setBackground(BackgroundFactory.createSolidBackground(0xe0e0e0));
msg.setPadding(5,0,5,0);
msg.setMargin(0,10,0,10);
vfm.add(msg);
hfm.add(vfm);
add(hfm);
}
public boolean keyDown(int keycode, int time) {
if (Keypad.KEY_ENTER == Keypad.key(keycode)) {
String message = msg.getText();
if(!message.equals(""))
{
Border rightBorder = BorderFactory.createBitmapBorder( new XYEdges( 16, 23, 27, 16 ), Bitmap.getBitmapResource( "border_bubble_right.png" ) );
Border leftBorder = BorderFactory.createBitmapBorder( new XYEdges( 16, 16, 27, 23 ), Bitmap.getBitmapResource( "border_bubble_left.png" ) );
addHeading( "Hello Adil!", leftBorder, Field.FIELD_LEFT );
addHeading( "Yeah, I see it", rightBorder, Field.FIELD_RIGHT );
addHeading( "have any update , related to this??", leftBorder, Field.FIELD_LEFT );
addHeading( "No worries, I'll finish it", rightBorder, Field.FIELD_RIGHT );
addHeading( "Middle part Messages", leftBorder, Field.FIELD_LEFT );
addHeading( "Vertically Scroll add in messages", leftBorder, Field.FIELD_LEFT );
addHeading( "have any update!", rightBorder, Field.FIELD_RIGHT );
addHeading( "Better get on that", leftBorder, Field.FIELD_LEFT );
addHeading( "No worries, I'll finish it", rightBorder, Field.FIELD_RIGHT );
addHeading( "Bottom Alignment Basic Editable Field?", leftBorder, Field.FIELD_LEFT );
addHeading( "Probably", rightBorder, Field.FIELD_RIGHT );
addHeading( "Better get on that", leftBorder, Field.FIELD_LEFT );
addHeading( "No worries, I'll finish it", rightBorder, Field.FIELD_RIGHT );
msg.setText("");
// ADD ALL FIELDS
add( _foreground );
}// if condition
else{ Dialog.alert("Please insert message");}
return true;
}
//let the system to pass the event to another listener.
return false;
}
private void addHeading( String label, Border border, long style )
{
LabelField header = new LabelField( label, Field.FOCUSABLE | style );
header.setBorder( border );
header.setMargin( 5, 5, -15, 5 );
_foreground.add( header );
}
}
try this - (i modified your code little bit.)
public class ChatList extends MainScreen
{
Manager _foreground = new NegativeMarginVerticalFieldManager( USE_ALL_WIDTH | USE_ALL_HEIGHT | VERTICAL_SCROLL );
BasicEditField msg;
public ChatList() {
super( NO_VERTICAL_SCROLL );
setTitle( "Chat" );
// Set the linear background.
this.getMainManager().setBackground(
BackgroundFactory.createLinearGradientBackground(0x91e7ff,0x0099CCFF,0x00336699,0x91e7ff)
);
// Add Field Bottom
HorizontalFieldManager hfm = new HorizontalFieldManager(){
protected void sublayout(int maxWidth, int maxHeight)
{
super.sublayout(Display.getWidth()/2+60,20); setExtent(Display.getWidth()/2+60,20);
}
};
msg = new BasicEditField(){
};
msg.setBorder(BorderFactory.createRoundedBorder(new XYEdges(3, 3, 3, 3), 0x999999, Border.STYLE_FILLED));
msg.setBackground(BackgroundFactory.createSolidBackground(0xe0e0e0));
// msg.setPadding(5,0,5,0);
msg.setPadding(5,0,5,0);
msg.setMargin(0,10,0,10);
hfm.add(msg);
final ButtonField b=new ButtonField("send");
JustifiedHorizontalFieldManager jfm=new JustifiedHorizontalFieldManager(hfm, b,true);
setStatus(jfm);
FieldChangeListener listener=new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
if(field==b){
Border rightBorder = BorderFactory.createBitmapBorder( new XYEdges( 16, 23, 27, 16 ), Bitmap.getBitmapResource( "bubble_right.png" ) );
Border leftBorder = BorderFactory.createBitmapBorder( new XYEdges( 16, 16, 27, 23 ), Bitmap.getBitmapResource( "bubble_left.png" ) );
addHeading( "Hello Adil!", leftBorder, Field.FIELD_LEFT );
addHeading( "Yeah, I see it", rightBorder, Field.FIELD_RIGHT );
addHeading( "have any update , related to this??", leftBorder, Field.FIELD_LEFT );
addHeading( "No worries, I'll finish it", rightBorder, Field.FIELD_RIGHT );
addHeading( "Middle part Messages", leftBorder, Field.FIELD_LEFT );
addHeading( "Vertically Scroll add in messages", leftBorder, Field.FIELD_LEFT );
addHeading( "have any update!", rightBorder, Field.FIELD_RIGHT );
addHeading( "Better get on that", leftBorder, Field.FIELD_LEFT );
addHeading( "No worries, I'll finish it", rightBorder, Field.FIELD_RIGHT );
addHeading( "Bottom Alignment Basic Editable Field?", leftBorder, Field.FIELD_LEFT );
addHeading( "Probably", rightBorder, Field.FIELD_RIGHT );
addHeading( "Better get on that", leftBorder, Field.FIELD_LEFT );
addHeading( "No worries, I'll finish it", rightBorder, Field.FIELD_RIGHT );
msg.setText("");
// ADD ALL FIELDS
add( _foreground );
}
}
};
b.setChangeListener(listener);
}
private void addHeading( String label, Border border, long style )
{
LabelField header = new LabelField( label, Field.FOCUSABLE | style );
header.setBorder( border );
header.setMargin( 5, 5, -15, 5 );
_foreground.add( header );
}
}
JustifiedHorizontalFieldManager.java is given below -
public class JustifiedHorizontalFieldManager extends Manager
{
private static final int SYSTEM_STYLE_SHIFT = 32;
public Field _leftField;
public Field _rightField;
private boolean _giveLeftFieldPriority;
public JustifiedHorizontalFieldManager( Field leftField, Field rightField, boolean giveLeftFieldPriority )
{
this( leftField, rightField, giveLeftFieldPriority, Field.USE_ALL_WIDTH );
}
public JustifiedHorizontalFieldManager( Field leftField, Field rightField, boolean giveLeftFieldPriority, long style )
{
super( style );
_leftField = leftField;
_rightField = rightField;
add( _leftField );
add( _rightField );
_giveLeftFieldPriority = giveLeftFieldPriority;
}
public JustifiedHorizontalFieldManager( boolean giveLeftFieldPriority, long style )
{
super( style );
_giveLeftFieldPriority = giveLeftFieldPriority;
}
public void addLeftField( Field field )
{
if( _leftField != null ) {
throw new IllegalStateException();
}
_leftField = field;
add( _leftField );
}
public void addRightField( Field field )
{
if( _rightField != null ) {
throw new IllegalStateException();
}
_rightField = field;
add( _rightField );
}
public int getPreferredWidth()
{
return _leftField.getPreferredWidth() + _rightField.getPreferredWidth();
}
public int getPreferredHeight()
{
return Math.max( _leftField.getPreferredHeight(), _rightField.getPreferredHeight() );
}
protected void sublayout( int width, int height )
{
Field firstField;
Field secondField;
if( _giveLeftFieldPriority ) {
firstField = _leftField;
secondField = _rightField;
} else {
firstField = _rightField;
secondField = _leftField;
}
int maxHeight = 0;
int availableWidth = width;
availableWidth -= _leftField.getMarginLeft();
availableWidth -= Math.max( _leftField.getMarginRight(), _rightField.getMarginLeft() );
availableWidth -= _rightField.getMarginRight();
layoutChild( firstField, availableWidth, height - firstField.getMarginTop() - firstField.getMarginBottom() );
maxHeight = Math.max( maxHeight, firstField.getMarginTop() + firstField.getHeight() + firstField.getMarginBottom() );
availableWidth -= firstField.getWidth();
layoutChild( secondField, availableWidth, height - secondField.getMarginTop() - secondField.getMarginBottom() );
maxHeight = Math.max( maxHeight, secondField.getMarginTop() + secondField.getHeight() + secondField.getMarginBottom() );
availableWidth -= secondField.getWidth();
if( !isStyle( Field.USE_ALL_HEIGHT ) ) {
height = maxHeight;
}
if( !isStyle( Field.USE_ALL_WIDTH ) ) {
width -= availableWidth;
}
setPositionChild( _leftField, _leftField.getMarginLeft(), getFieldY( _leftField, height ) );
setPositionChild( _rightField, width - _rightField.getWidth() - _rightField.getMarginRight(), getFieldY( _rightField, height ) );
setExtent( width, height );
}
private int getFieldY( Field field, int height )
{
switch( (int)( ( field.getStyle() & FIELD_VALIGN_MASK ) >> SYSTEM_STYLE_SHIFT ) ) {
case (int)( FIELD_BOTTOM >> SYSTEM_STYLE_SHIFT ):
return height - field.getHeight() - field.getMarginBottom();
case (int)( FIELD_VCENTER >> SYSTEM_STYLE_SHIFT ):
return field.getMarginTop() + ( height - field.getMarginTop() - field.getHeight() - field.getMarginBottom() ) / 2;
default:
return field.getMarginTop();
}
}
public Field getLeftField()
{
return _leftField;
}
public Field getRightField()
{
return _rightField;
}
public void replace( Field oldField, Field newField )
{
if( oldField == newField ) {
// Nothing to do
return;
}
if( oldField == _leftField ) {
_leftField = newField;
} else if( oldField == _rightField ) {
_rightField = newField;
}
add( newField );
delete( oldField );
}
}
I want to display an image at the bottom of the screen and make it clickable ,i have created a custom bitmap field ImageButtonField and trying to use it ,but i am getting ImageButtonField cannot be resolved.
public class ImageButtonField extends BitmapField
{
public ImageButtonField(Bitmap image) {
super(image);
}
public ImageButtonField(Bitmap image,Field location) {
//super(image,location);
}
public boolean isFocusable() {
return true;
}
protected boolean navigationClick(int status, int time) {
fieldChangeNotify(0);
return true;
}
protected boolean trackwheelClick(int status, int time) {
fieldChangeNotify(0);
return true;
}
protected boolean keyChar(char character, int status, int time) {
if(Characters.ENTER == character || Characters.SPACE == character) {
fieldChangeNotify(0);
return true;
}
return super.keyChar(character, status, time);
}
}
public NativeScreen() {
super();
LabelField title = new LabelField("Calendar DatePicker",
LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH);
hrzManager = new HorizontalFieldManager() {
protected void paintBackground(Graphics graphics) {
graphics.setBackgroundColor(0x0007F5ED);
graphics.clear();
super.paint(graphics);
}
};
hrzManager.add(title);
this.add(hrzManager);
//The background image.
backgroundBitmap = Bitmap.getBitmapResource("cryptodemo_jde.png");
// MainScreen mainScreen = new MainScreen(MainScreen.NO_VERTICAL_SCROLL | MainScreen.NO_HORIZONTAL_SCROLL);
VerticalFieldManager verticalFieldManager = new VerticalFieldManager(Manager.NO_VERTICAL_SCROLL | Manager.NO_HORIZONTAL_SCROLL |
Manager.USE_ALL_HEIGHT | Manager.USE_ALL_WIDTH ) ;
BasicEditField bef = new BasicEditField("To: ","",50,BasicEditField.FILTER_EMAIL);
//The LabelField will show up through the transparent image.
LabelField labelField = new LabelField("This is a label");
HorizontalFieldManager horizontalFieldManager = new HorizontalFieldManager(HorizontalFieldManager.USE_ALL_WIDTH | HorizontalFieldManager.USE_ALL_HEIGHT);
//A bitmap field with a transparent image.
//The background image will show up through the transparent BitMapField image.
ImageButtonField bitmapField = new ImageButtonField(Bitmap.getBitmapResource("pimdemo_jde.png"), Field.FIELD_BOTTOM);
horizontalFieldManager.add(bitmapField);
BitmapField bitmapField1 = new BitmapField(Bitmap.getBitmapResource("attachmentdemo_jde.png"), Field.FIELD_BOTTOM);
horizontalFieldManager.add(bitmapField1);
//Add the fields to the manager.
// verticalFieldManager.add(bef);
// verticalFieldManager.add(labelField);
verticalFieldManager.add(horizontalFieldManager);
//Add the manager to the screen.
this.add(verticalFieldManager);
There's no constructor in your class that lets you do this:
ImageButtonField ImageButtonField bitmapField = new ImageButtonField(Bitmap.getBitmapResource("pimdemo_jde.png"), Field.FIELD_BOTTOM);
The constructor that you're trying to use requires a Bitmap instance and a long primitive (for Field.FIELD_BOTTOM). You should add a constructor with the following signature for your code to work:
public ImageButtonField (Bitmap bitmap, long style){
super(bitmap, style);
}
After loading the screen If i click on the button directly, the setChangeListener event is not being invoked, rather when the focus is changed to button/hfm, the setChangeListener event is getting called and the desired result is also obtained.
What might be the possible reason and please help me fix this issue...
HorizontalFieldManager hfm = new HorizontalFieldManager();
ButtonField buttonF = new ButtonField(ButtonField.CONSUME_CLICK);
buttonF.setLabel("View Key");
FieldChangeListener listeneronClick = new FieldChangeListener()
{
public void fieldChanged(Field field, int context)
{
System.out.println("in fieldchange");
ButtonField buttononClick = (ButtonField) field;
buttononClick.setLabel("Hide Key");
}
};
hfm.add(buttonF);
buttonF.setChangeListener(listeneronClick);
Thanks in advance.
Try creating your button like the following
ButtonField button = new ButtonField("Ok", ButtonField.CONSUME_CLICK);
ButtonField.CONSUME_CLICK Indicates to the button consume the click event.
Your FieldChangeListener won't do anything because it creates a new button and does not add it to the screen. I'm guessing you just want to change the text on your existing button so try the following code:
HorizontalFieldManager hfm = new HorizontalFieldManager();
final ButtonField buttonF = new ButtonField(ButtonField.CONSUME_CLICK);
buttonF.setLabel("View Key");
FieldChangeListener listeneronClick = new FieldChangeListener(){
public void fieldChanged(Field field, int context)
{
System.out.println("in fieldchange");
buttonF.setLabel("Hide Key");
}
};
hfm.add(buttonF);
buttonF.setChangeListener(listeneronClick);
try calling hfm.setFocus() before adding hfm to the screen manager.
Hi please tell me how to make this screen in blackberry.
my data is not adding on it.i took vertical field manager to add all components.
Its working.. I created it for you. use this code
public class home extends UiApplication {
public static void main(String[] args)
{
home app = new home();
app.enterEventDispatcher();
}
MainScreen screen = new MainScreen();
private int deviceWidth = Display.getWidth();
private int deviceHeight = Display.getHeight();
LabelField lbl1 = new LabelField("label");
final Bitmap backgroundBitmap = Bitmap.getBitmapResource("bg1.jpg");
final Bitmap backgroundBitmap1 = Bitmap.getBitmapResource("bg2.jpg");
final BitmapField mybitmapField = new BitmapField(Bitmap.getBitmapResource("facebook-logo.jpg"),DrawStyle.HCENTER);
public home()
{
super();
pushScreen(screen);
VerticalFieldManager mainManager = new VerticalFieldManager(Manager.NO_VERTICAL_SCROLL | Manager.NO_VERTICAL_SCROLLBAR )
{
public void paint(Graphics graphics)
{
graphics.clear();
graphics.drawBitmap(0, 0, deviceWidth, deviceHeight, backgroundBitmap, 0, 0);
super.paint(graphics);
}
};
//this manger is used for adding the componentes
VerticalFieldManager subManager = new VerticalFieldManager(Manager.NO_VERTICAL_SCROLL | Manager.NO_VERTICAL_SCROLLBAR )
{
public void paint(Graphics graphics)
{
graphics.clear();
graphics.drawBitmap(0, 0, deviceWidth, deviceHeight, backgroundBitmap1, 0, 0);
super.paint(graphics);
}
};
screen.add(mainManager);
mainManager.add(lbl1);
mainManager.add(subManager);
subManager.add(mybitmapField);
subManager.add(new LabelField("Data 1"));
subManager.add(new LabelField("Data 1"));
subManager.add(new LabelField("Data 1"));
}
}
output will be like this.
Find an example of how to make this screen below:
VerticalFieldManager manager = new VerticalFieldManager();
VerticalFieldManager first = new VerticalFieldManager();
VerticalFieldManager second = new VerticalFieldManager();
first.add(label);
second.add(image);
second.add(data);
second.add(data);
second.add(data);
manager.add(first);
manager.add(second);
add(manager);
How to add click event in blackberry banner ad.
Here is my code:
public class DemonstrationScreen extends MainScreen
{
public DemonstrationScreen()
{
final Bitmap customPlaceholder = Bitmap.getBitmapResource("arrow.png");
Banner bannerAd = new Banner(add.APID,null,10000, customPlaceholder);
bannerAd.setMMASize(Banner.MMA_SIZE_EXTRA_LARGE);
VerticalFieldManager vfm = new VerticalFieldManager
(VerticalFieldManager.NO_VERTICAL_SCROLL
| VerticalFieldManager.NO_VERTICAL_SCROLLBAR
| VerticalFieldManager.USE_ALL_WIDTH);
HorizontalFieldManager hfm = new HorizontalFieldManager
(HorizontalFieldManager.FIELD_HCENTER
| HorizontalFieldManager.FIELD_VCENTER);
hfm.add(bannerAd);
vfm.add(hfm);
add(vfm);
FieldChangeListener listener=new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
if(field==bannerAd){
Dialog.alert("Banner clicked");
}
}};
bannerAd.setChangeListener(listener);
}
}
this is not working. when i click the ad, then its not showing anythig.
I do think this is unexpected/improper usage of Banner.
However you could potentially do this by overriding navigationClick() at Banner:
public class DemonstrationScreen extends MainScreen
{
public DemonstrationScreen()
{
final Bitmap customPlaceholder = Bitmap.getBitmapResource("arrow.png");
Banner bannerAd = new Banner(add.APID,null,10000, customPlaceholder) {
protected boolean navigationClick(int status, int time) {
Dialog.alert("Banner clicked");
return super.navigationClick(status, time);
}
};
bannerAd.setMMASize(Banner.MMA_SIZE_EXTRA_LARGE);
VerticalFieldManager vfm = new VerticalFieldManager
(VerticalFieldManager.NO_VERTICAL_SCROLL
| VerticalFieldManager.NO_VERTICAL_SCROLLBAR
| VerticalFieldManager.USE_ALL_WIDTH);
HorizontalFieldManager hfm = new HorizontalFieldManager
(HorizontalFieldManager.FIELD_HCENTER
| HorizontalFieldManager.FIELD_VCENTER);
hfm.add(bannerAd);
vfm.add(hfm);
add(vfm);
}
}
But since RIM made Banner class final you can not do this. So I think your request has no simple solution. A hard solution would be to "figure out" what field is clicked at the MainScreen level (in navigationClick of MainScreen you can check what field is in focus and do smth).