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
Highlighted
Developer
Posts: 271
Registered: ‎12-24-2010
My Device: BlackBerry 10
My Carrier: Airtel
Accepted Solution

Touching any place in the Screen is calling the ListField TouchEvent.DOWN method

Hi,

 

I am suffering with little complex issue. In my app screen, I have Title bar with a labelfield and two customized buttons.

Below of the titlefield i am adding a list field. 

Now i have some complex functionality on the list field. When i will click each list row, it should move to another screen which i am doing using navigation click method. Now if i will touch the list row(for touch devices), it should move to another screen. And if i will long touch the list row it will display menu popup(customized, not default) with Delete and Details option.

 

        mListItem = new ListField(length, ButtonField.CONSUME_CLICK)
		{
			long touchedAt = -1;
		    long HOLD_TIME = 500;
		    
		 // The regular getFieldAtLocation returns wrong values in open
		 			// spaces in
		 			// complex managers, so we override it
//		 			public int getFieldAtLocation(int x, int y) {
//		 				XYRect rect = new XYRect();
//		 				int index = getFieldCount() - 1;
//		 				while (index >= 0) {
//		 					getField(index).getExtent(rect);
//		 					if (rect.contains(x, y))
//		 						break;
//		 					--index;
//		 				}
//		 				return index;
//		 			}
			protected boolean navigationClick(int status, int time)
			{
				// Click related functionality
				return true;
			}
			
			protected boolean touchEvent(TouchEvent message) 
			{
				if(message.getEvent() == TouchEvent.DOWN) 
				{
					 if (getFieldAtLocation(message.getX(1), message.getY(1)) == -1)
					 {
						 Logger.out("AccountList", "Touch down: getField location");
						 return true; // kill the event
					 }
					 else
					 {
						 touchedAt = System.currentTimeMillis();
						 touchedonList = true;
						 Logger.out("AccountList", "Touch down: touched down  "+touchedonList);
					 }
		        }
				else if(message.getEvent() == TouchEvent.UP ) 
				{
					   
		               if(System.currentTimeMillis() - touchedAt < HOLD_TIME && touchedonList == true)
		               {
		            	   touchedAt = -1; // reset
		            	   // Single Touch and Move to another screen
		               }
		                  
		               else if(touchedonList == true)
		               {
		            	 // Showing Menu Popup   
		               }
		        }
		        return true;
//		        return super.touchEvent(message);  // Not using this as it will show the default pop up
			}

 

  And here is my List Field Callback:

 

 

public class ListCallBack implements ListFieldCallback
	{
		public void drawListRow(ListField listField, Graphics graphics, int index,
		        int y, int width) {
			
			String strdomOrg = yyyyyyy;
		    String text = xxxxxx;
		    Font f = FONT_FAMILY_0_SF_AS_08;
		   
		    int h = f.getHeight();
//		    int height = (listField.getRowHeight() - h)/2 ;
		    int height = h/2 ;
		    y += height;
		    graphics.setFont(f);
		    graphics.setColor(Color.BLACK);
		    graphics.drawText(strdomOrg, 10, y, DrawStyle.ELLIPSIS, width);
		    y = y + h;
		    graphics.setFont(FONT_FAMILY_1_SF_AS_08);
		    graphics.setColor(Color.BLACK);
		    graphics.drawText(text, 10, y, DrawStyle.ELLIPSIS , width);
		    
		 // use the offset instead
		    int offset = (listField.getRowHeight() ) >> 1;
		    if (index != 0) {
		    	graphics.drawLine(0, y - offset , width, y - offset);
			}
		}

		public Object get(ListField listField, int index) {
		    // TODO Auto-generated method stub
		    return mAccounts[index];
		}

		public int getPreferredWidth(ListField listField) {
		    // TODO Auto-generated method stub
		    return screenWidth;
		}
		
		public int getPreferredHeight() {
			return getContentHeight();
		}

		public int indexOfList(ListField listField, String prefix, int start) {
		    // TODO Auto-generated method stub
		    return listField.getSelectedIndex();
		}

	}

  Now the issue is coming in a different ways:

 1. When ever i am touching the title field buttons, it is calling the listfield TouchEvent.DOWN . So the button click is not working. Instead, clicking the title bar, it is moving to another screen which is list filed click functionality.

2. So i have added the listfield to a VFM.:

 

m_vfmScreen = new VerticalFieldManager(VERTICAL_SCROLL | Manager.VERTICAL_SCROLLBAR) 
		{
		
		protected boolean touchEvent(TouchEvent message) {
				int event = message.getEvent();
				if (event == TouchEvent.CLICK) {
					if (getFieldAtLocation(message.getX(1), message.getY(1)) == -1)
						return true; // kill the event
					else {
					}
				}
				return super.touchEvent(message);
			}

			// The regular getFieldAtLocation returns wrong values in open
			// spaces in
			// complex managers, so we override it
			public int getFieldAtLocation(int x, int y) {
				XYRect rect = new XYRect();
				int index = getFieldCount() - 1;
				while (index >= 0) {
					getField(index).getExtent(rect);
					if (rect.contains(x, y))
						break;
					--index;
				}
				return index;
			}
		};

  So only in the list field touch is working. If i am touching the buttons in the Title bar, it is not functioning. But if the buttons are getting focus, that case they are working.

 

So i am in dilemma what exactly the issue. Can anyone please help.

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

Re: Touching any place in the Screen is calling the ListField TouchEvent.DOWN method

For me, there is some unepxected code in your touchEvent() - getFieldAtLocation().  If you want to supporess off Field presses, I suggest you use code like this:

 

        protected boolean touchEvent(TouchEvent message) {

                int x = message.getX( 1 );
                int y = message.getY( 1 );
                if( x < 0 || y < 0 || x > getExtent().width || y > getExtent().height ) {
                        // Outside the field
                        return false;
                }

 

Developer
Posts: 271
Registered: ‎12-24-2010
My Device: BlackBerry 10
My Carrier: Airtel

Re: Touching any place in the Screen is calling the ListField TouchEvent.DOWN method

I have used this for the vertical field manager in which i am adding the list. But if i am touching outside my list field, it is moving to another screen. 

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

Re: Touching any place in the Screen is calling the ListField TouchEvent.DOWN method

I can understand the code in the VFM, if you are doing funny things in sublayout, but you shoud still have this code to make sure that you ignore events outside the extent of the VFM.

 

But I am not sure why you are using the same check code inside the ListField.  And once again, you should ignore all events, not just Click. 

Developer
Posts: 271
Registered: ‎12-24-2010
My Device: BlackBerry 10
My Carrier: Airtel

Re: Touching any place in the Screen is calling the ListField TouchEvent.DOWN method

Finally it works. I have kept your coordinate checking for touch event in the list field, not in the VFM. And it works. Thanks Peter.

 

 

Developer
Posts: 45
Registered: ‎08-29-2013
My Device: Blackberry curve-9360 and Blackberry bold-9860
My Carrier: Company

Re: Touching any place in the Screen is calling the ListField TouchEvent.DOWN method

hi,

 

I have same problem but with editfield.

In my case when i move curson to editfield with tracball and then after touch anywhere in screen the key board will show.

I use the method

protected boolean touchEvent(TouchEvent message) {

                int x = message.getX( 1 );
                int y = message.getY( 1 );
                if( x < 0 || y < 0 || x > getExtent().width || y > getExtent().height ) {
                        // Outside the field
                        return false;
                }

as you mentioned but it always return false.

 

My code is as follow

 

public final class TextBoxField extends VerticalFieldManager {

      /** The manager width. */
      private int managerWidth;
      
      /** The manager height. */
      private int managerHeight;
      
      /** The edit field. */
      private EditField editField;
      
      /**
       * Instantiates a new text box field.
       *
       * @param width the width
       * @param height the height
       * @param maxNumChars the max num chars
       * @param style the style
       */
      public TextBoxField(int width, int height, int maxNumChars, long style) {
          this(width, height, maxNumChars, style, null);
      }

      /**
       * Instantiates a new text box field.
       *
       * @param width the width
       * @param height the height
       * @param maxNumChars the max num chars
       * @param style the style
       * @param font the font
       */
      public TextBoxField(int width, int height, int maxNumChars, long style, Font font){
          this(null, width, height, maxNumChars, style, font);
      }
      
      /**
       * Instantiates a new text box field.
       *
       * @param initialValue the initial value
       * @param width the width
       * @param height the height
       * @param maxNumChars the max num chars
       * @param style the style
       * @param font the font
       */
      public TextBoxField(String initialValue, int width, int height, int maxNumChars, long style, Font font) {
            super(Manager.NO_VERTICAL_SCROLL);
            managerWidth = width;
            managerHeight = height;

            VerticalFieldManager vfm = new VerticalFieldManager(Manager.VERTICAL_SCROLL);
            
            editField = new EditField("",initialValue,maxNumChars, style){
                public void paint(Graphics g) {
                    getManager().invalidate();
                    super.paint(g);
                }
                protected void displayFieldFullMessage()
                {                    
                }
                
                protected boolean touchEvent(TouchEvent message) {

                    int x = message.getX( -1 );
                    int y = message.getY( -1 );
                    Logger.debug("---x---"+x+"---y---"+y);
                    if( x < 0 || y < 0 || x > getExtent().width || y > getExtent().height ) {
                            // Outside the field
                        Logger.debug("---outside the field---");
                        return false;
                    }
                    return true;
                  }
            };
           
            if(font != null){
                editField.setFont(font);
            }
            vfm.add(editField);
            add(vfm);
     }

      /* (non-Javadoc)
       * @see net.rim.device.api.ui.Manager#paint(net.rim.device.api.ui.Graphics)
       */
      public void paint(Graphics g) {
          g.setBackgroundColor(Color.WHITESMOKE);
          g.clear();
          g.drawRect(0, 0, getWidth(), getHeight());
          g.setColor(Color.BLACK);
          super.paint(g);
    }

      /* (non-Javadoc)
       * @see net.rim.device.api.ui.container.VerticalFieldManager#sublayout(int, int)
       */
      public void sublayout(int width, int height) {
          if (managerWidth == 0) {
            managerWidth = width;
          }
          if (managerHeight == 0) {
            managerHeight = height;
          }
          super.sublayout(managerWidth, managerHeight);
          setExtent(managerWidth,managerHeight);
      }
      
      /**
       * Gets the text.
       *
       * @return the text
       */
      public String getText() {
        return editField.getText();
    }
 
}