EditField And Buttonfield On same Line - blackberry

I want to add editfield and labelfield on same line . first editfield and then buttonfield . I have tried many times but it didn't work for me .I have added horizontal field manager and also used tables but none of it can help . The issue is it is not showing the buttonfield,i can see only editfield. enter code here
public class MyScreen extends MainScreen {
/**
* Creates a new MyScreen object
*/
public MyScreen()
{
// Set the displayed title of the screen
setTitle("MyTitle");
HorizontalFieldManager m= new HorizontalFieldManager(HorizontalFieldManager.FIELD_HCENTER);
final GridFieldManager grid = new GridFieldManager(4,4,0);
grid.setColumnPadding(5);
grid.setRowPadding(5);
EditField c= new EditField("","",5,EditField.EDITABLE);
// m.add(c);
Border border=BorderFactory.createRoundedBorder(new XYEdges(10,10,10,10),Border.STYLE_SOLID);
grid.setBorder(border);
ButtonField b= new ButtonField("Select ",ButtonField.CONSUME_CLICK);
//m.add(b);
grid.insert(b,1);
grid.insert(c,0);
//add(m);
add(grid);
}
}

Override getPreferredWidth() of both EditField and ButtonField like this:
public int getPreferredWidth() {
return Display.getWidth()/2;
}

Override sublayout() and layout() method of your EditField & ButtonField than set its width Ex: 100 .. now add both this field in your m.add(); .. its works ..

Related

List field in blackberry , event click listener not working

I am working on BB OS v5.0. I have managed to get the list to appear on the screen. I am getting data from webservice and adding it into a Vector.
Now I want to find out onclick, which is the item that is clicked and accordingly perform some operation. For that i am trying to display an alert. But I'm not getting the alert.
Here is my code :
In my mainscreen , i added fieldmanager=new VerticalFieldManager(); and add(fieldmanager);
void fetchAlbumsForLetter(String letter) {
Status.show("Processing ....", 3000);
fieldManager.deleteAll();
VerticalFieldManager top = new VerticalFieldManager(Manager.NO_HORIZONTAL_SCROLL | Manager.NO_HORIZONTAL_SCROLLBAR | Manager.VERTICAL_SCROLL | Manager.VERTICAL_SCROLLBAR | Field.USE_ALL_WIDTH){
public void paint(Graphics graphics) {
graphics.setBackgroundColor(0x00290008);
graphics.setColor(Color.WHITE);
graphics.clear();
graphics.drawBitmap(0, 0, sha.getWidth(),
sha.getHeight(), sha, 0, 0);
super.paint(graphics);
}
};
add(top);
CustomListField4 list4 = new CustomListField4(null){
protected boolean navigationClick(int status, int time) {
getValue4();
return true;
}
};
fieldmanager.add(list4);
}
protected void getValue4() {
Field f = getFieldWithFocus();
if (f instanceof ListField) {
ListField l = (ListField) f;
final int index = l.getSelectedIndex();
HistoryItem _contactslist = (HistoryItem) CustomListField4.val4.elementAt(index);
final String id = _contactslist.getName();
Dialog.alert(id+"");
}
}
Please help me to resolve this
EDIT
class CustomListField4 extends ListField implements ListFieldCallback {
public CustomListField4(Vector data) {
super(0, ListField.MULTI_SELECT);
final TableRowManager row = new TableRowManager() {
public void paint(Graphics g) {
// g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(0x0f3e19b);
g.clear();
super.paint(g);
}
};
Bitmap icon = Bitmap.getBitmapResource("Devil Skype.png");
HorizontalFieldManager h=new HorizontalFieldManager();
h.add(new BitmapField(icon));
//h.add(new BitmapField(song.getThumb()));
h.add(new LabelField(song.getAlbumName()));
//h.add(new LabelField(row1.getLanguage()));
//h.setMargin(0,0,50,0);
//Dialog.alert(song.getName());
VerticalFieldManager vfm=new VerticalFieldManager();
vfm.add(h);
//vfm.add(new LabelField(song.getArtist()));
row.add(vfm);
contacts.addElement(row);
}
setSize( contacts.size());
}
// ListFieldCallback Implementation
public void drawListRow(ListField listField, Graphics g, int index, int y, int width) {
listField.setRowHeight(index,107);
CustomListField4 list = (CustomListField4) listField;
TableRowManager rowManager = (TableRowManager) CustomListField4.contacts.elementAt(index);
rowManager.drawRow(g, 0, y, width, list.getRowHeight());
}
public class TableRowManager extends Manager {
public TableRowManager() {
super(0);
}
You are calling getFieldWithFocus() which will give you the manager. You need to get the leaf field
protected void getValue4() {
Field f = getLeafFieldWithFocus();
if (f instanceof ListField) {
//Your code
}
}
I think your hierarchy of Field and Manager objects is incorrect, and this is causing problems with your detection of field focus/selection.
It wasn't obvious from the original code you posted, but by looking at your update, I assume that you are calling fetchAlbumsForLetter() once for every row. That's not right.
fetchAlbumsForLetter() is creating a new CustomListField4 each time it's called. And, CustomListField4 is a ListField.
A ListField is not meant to represent only one row. It's meant to represent all the rows. You should only create one instance of CustomListField4.
I would do either one of two things:
1. Continue to Use a ListField
If you want CustomListField4 to be a ListField (extends ListField), then in your implementation of
public void drawListRow(ListField listField, Graphics g, int index, int y, int width);
you should actually draw graphics objects, using all the Graphics#draw methods. These are primitive graphics items, like filled areas, lines, text, or bitmaps. You would not be using Field objects inside each ListField row, as you're trying to do with your TableRowManager class.
See here for a sample ListField, or here for a more sophisticated example
2. Imitate ListField with a Manager
change your code to
public class CustomListField4 extends VerticalFieldManager {
or
public class CustomListField4 extends Manager {
Then, you can use a TableRowManager for each row, and add LabelField or BitmapField objects to it.
See here for an example of this
If you fix these problems, then I think the way you are overriding navigationClick() will work fine for detecting the row click, and doing something with the selected row.
You can try this
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
Dialog.alert(id+"");
}
});

Blackberry Field Manager Scrolling Shows NullpointerException

I am having problem with Blackberry horizontal and vertical Field Managers. In my sample application I have two vertical field managers added to one horizontal field manager and the GUI is in the following figure.
In the field change listener of button I am removing the left VFM, then only the Home button is displayed, and in home button listener I am adding the left VFM, then it get back to the above screen.
The problem is occurring after scrolling the left content and deleting the left VFM, Then only Home button is displayed and track pad navigation (scrolling) leads to Null Pointer exception screen. The following figure contains the simulator screen shot of error.
And here is the complete source code for reproducing the error.
public class MyApp extends UiApplication{
public static void main(String[] args)
{
// Create a new instance of the application and make the currently
// running thread the application's event dispatch thread.
MyApp theApp = new MyApp();
theApp.enterEventDispatcher();
}
/**
* Creates a new MyApp object
*/
public MyApp()
{
// Push a screen onto the UI stack for rendering.
pushScreen(new MyScreen());
}
}
public final class MyScreen extends MainScreen{
private HorizontalFieldManager hfmMainManager = null;
private VerticalFieldManager menuButtonManager = null;
private String[] buttons = new String[]{"BUTTON ID 1",
"BUTTON ID 2",
"BUTTON ID 3",
"BUTTON ID 4",
"BUTTON ID 5",
"BUTTON ID 6",
"BUTTON ID 7",
"BUTTON ID 8",
"BUTTON ID 9",
"BUTTON ID 10"};
private boolean menuShowing = false;
/**
* Creates a new MyScreen object
*/
public MyScreen()
{
super( Manager.NO_VERTICAL_SCROLL | MainScreen.NO_VERTICAL_SCROLLBAR
| Manager.NO_HORIZONTAL_SCROLL | Manager.NO_HORIZONTAL_SCROLLBAR);
hfmMainManager = new HorizontalFieldManager(Manager.NO_HORIZONTAL_SCROLL
| Manager.NO_HORIZONTAL_SCROLLBAR);
////////////////////MENU BUTTONS
menuButtonManager = new VerticalFieldManager(Manager.NO_VERTICAL_SCROLL | Manager.NO_VERTICAL_SCROLLBAR);
menuButtonManager.add(new ButtonField("Header Button", Field.FIELD_HCENTER));
VerticalFieldManager vfmScrollable = new VerticalFieldManager(Manager.VERTICAL_SCROLL | Manager.VERTICAL_SCROLLBAR);
for (int i = 0; i < buttons.length; i++) {
ButtonField buttonField = new ButtonField(buttons[i], Field.FIELD_HCENTER);
buttonField.setChangeListener(listenerButton);
vfmScrollable.add(buttonField);
}
menuButtonManager.add(vfmScrollable);
hfmMainManager.add(menuButtonManager);
VerticalFieldManager vfmMenuContentManager = new VerticalFieldManager(Manager.NO_VERTICAL_SCROLLBAR | Manager.NO_VERTICAL_SCROLL);
ButtonField buttonField = new ButtonField("Home Button", Field.FIELD_HCENTER);
buttonField.setChangeListener(listenerButton);
buttonField.setMargin(10, 0, 10, 0);
vfmMenuContentManager.add(buttonField);
hfmMainManager.add(vfmMenuContentManager);
add(hfmMainManager);
menuShowing = true;
}
private FieldChangeListener listenerButton = new FieldChangeListener() {
public void fieldChanged(Field arg0, int arg1) {
synchronized (UiApplication.getEventLock()) {
if(menuShowing){
menuShowing = false;
hfmMainManager.delete(menuButtonManager);
}else{
menuShowing = true;
hfmMainManager.insert(menuButtonManager, 0);
}
}
}
};
}
How do I solve this Issue?
Thanks in advance
Regards
Anish
I've seen similar behavior when trying to change the field structure from a focus or unfocus event. When a field removes itself from the screen in the middle of the focus event, the focus tracking can get out of sync, and cause unusual errors like this. Try changing from grabbing the UI lock to calling invokeLater instead. That way the change listener can finish executing before you delete the VFM, keeping all the invariants in place.
Get rid of this:
synchronized (UiApplication.getEventLock()) {
Replace it with this:
UiApplication.getInstance().invokeLater(new Runnable{ public void run() {

How to add a custom Manager inside a pop-up screen in blackberry

i am trying to add a custom window in blackberry but before this i am trying to add a custom label in that popup screen for my satisfaction that i can add or cant . so at the time when i am adding that i am facing the problem of IllegalArguementException error , so can you please tell me how can i solve that problem . i am doing like this .
see this is my MYScreen class which i am using to add pop-up . so pop-up is added when ever i clicked that press button , which is added in the Screen .
public final class MyScreen extends MainScreen implements FieldChangeListener
{
private ButtonField btn;
public MyScreen()
{
setTitle("MyTitle");
btn = new ButtonField ("press");
btn.setChangeListener(this);
add(btn) ;
}
public void fieldChanged(Field field, int context)
{
if ( field == btn )
{
Dialog.inform("hello");
pop_manager manager_object = new pop_manager(0);
UiApplication.getUiApplication().pushScreen( new up_pop_test( manager_object ) );
}
}
}
so in this i have added , new_up_pop_test class which is :
public class up_pop_test extends PopupScreen
{
public up_pop_test( pop_manager delegate)
{
super(delegate);
add(delegate);
}
}
and pop_manager is :
public class pop_manager extends Manager
{
protected pop_manager(long style)
{
super(style);
}
protected void sublayout(int w, int h)
{
Field f = getField(0);
layoutChild( f , w/3+w/3 , 50 ) ;
setPositionChild ( f , w/33 + w/33 , w/67+w/104 );
setExtent(w,h);
}
}
You don't need to do as much work as you were trying to...
All you need is a PopupScreen, which you can customise at will, just like a normal screen.
public class MyPopup extends PopupScreen {
public MyPopup() {
super(new VerticalFieldManager());
LabelField infoLabel = new LabelField("Here is a label in a popup");
add(infoLabel);
}
}
In order to call it, all you need to do is push like a normal screen.
UiApplication.getUiApplication().pushScreen(new MyPopup());
You cannot gain control over the Dialog class, these are for simple operations like informing the user or asking a question, and are standardised

Setting a background color to my Blackberry application very basic!

This is my screen:
final class GeneralExpenseViewScreen extends MainScreen {
public GeneralExpenseViewScreen() {
super();
LabelField title = new LabelField("TeamMate TEC | Expenses",
LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH);
setTitle(title);
Background bg = BackgroundFactory.createSolidBackground(0xBDBDDB);
setBackground(bg);
HorizontalFieldManager headerAreaManager = new HorizontalFieldManager();
HorizontalFieldManager filterAreaManager = new HorizontalFieldManager();
HorizontalFieldManager expenseListAreaManager = new HorizontalFieldManager();
HorizontalFieldManager totalAreaManager = new HorizontalFieldManager();
HorizontalFieldManager addNewAreaManager = new HorizontalFieldManager();
add(headerAreaManager);
add(filterAreaManager);
add(expenseListAreaManager);
add(totalAreaManager);
add(addNewAreaManager);
/**Begin form layouts**/
Bitmap headerImage = Bitmap.getBitmapResource("sergioheader.png");
BitmapField header = new BitmapField(headerImage);
headerAreaManager.add(header);
}
public boolean onClose() {
Dialog.alert("AH!");
System.exit(0);
return true;
}
}
Notice that I'm calling setBackground directly to the class but it's not working how I think it would work.
How can I set a background color to my application form?
Thanks.
I've used this code with success:
protected void paint(Graphics graphics) {
graphics.setBackgroundColor(0xBDBDDB);
graphics.clear();
super.paint(graphics);
}
Depending on the version you're developing for, you could use the following
getMainManager().setBackground(BackgroundFactory.createSolidBackground(Color.BLACK));
to set the screen managers background color.

not show the result

here is my code and through the source code is not available exception. in the code we require that popup screen that show but the value of the average speed is not show.
class popUpScreen extends PopupScreen
{
private EditField _sp;
// Speed s = new Speed();
//public double _averageSpeed =s._averageSpeed ;
StringBuffer sb = new StringBuffer();
popUpScreen()
{
super(new VerticalFieldManager(),Field.FOCUSABLE);
Speed s = new Speed();
double _averageSpeed =s._averageSpeed ;
_sp = new EditField(" The Average Speed is: ",""+_averageSpeed );
add(_sp);
//Speed._averageSpeed=sb.;
//sb.append("\n_averageSpeed : ");
//sb.append(_averageSpeed);
}
}
you should save that VerticalFieldManager you passed to the super; add your label to the manager.
It is not necessary to override the whole class you could do like this:
DialogFieldManager dfm = new DialogFieldManager();
dfm.add(_sp);
PopupScreen popUpScreen = new BasePopupScreen(dfm, Manager.VERTICAL_SCROLL);

Resources