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
shahumang8
Posts: 576
Registered: ‎08-09-2010
My Device: Not Specified

Problem in the CustomAutoCompleteField

Hello,

 

     Today i build the CustomAutoCompleteField.Means i extends AutoCompleteField.So my question is when the height and width are set into sublyout into Custom class and then set the height and width into drawListRaw and drawExpendListRow but there are no any effected.So now how to increse size of AutoCompletedField.

 

       Note :- The TextField are fix size show but when user enter any text into textField at that time the AutoCompletedField open and his width are incresed .So it's possible.

 

Thanks.

 

 

 

Please use plain text.
BlackBerry Development Advisor
oros
Posts: 1,523
Registered: ‎04-12-2010
My Device: BlackBerry Z10
My Carrier: Bell

Re: Problem in the CustomAutoCompleteField

Hi there,

 

Can you provide a copy of the source for the CustomAutoCompleteField you've developed? It may be something as simple as throwing in an invalidate() to force re-painting; however by taking a look at your code we should be able to get a better idea of what has been done and what can still be tried.

 

Erik Oros

BlackBerry Development Advisor


Erik Oros | @WaterlooErik | eoros@blackberry.com | Developer Issue Tracker

If a reply has answered your question, please click Accept as Solution to help other visitors in the future.
Please use plain text.
Developer
shahumang8
Posts: 576
Registered: ‎08-09-2010
My Device: Not Specified

Re: Problem in the CustomAutoCompleteField

Hello Oros,

 

        Thanks for reply me.I attached my test Source code so check it out.

 

 

I need result like as a images  or if possible then user enter any text at that time AutoCompletedField size incresed as a Disply width.If possible then reply me asap i am waiting you ur postive response.

 

Untitled.jpg

Please use plain text.
Developer
shahumang8
Posts: 576
Registered: ‎08-09-2010
My Device: Not Specified

Re: Problem in the CustomAutoCompleteField

Hi  oros,

 

      If any update ?

 

Thanks.

Please use plain text.
BlackBerry Development Advisor
oros
Posts: 1,523
Registered: ‎04-12-2010
My Device: BlackBerry Z10
My Carrier: Bell

Re: Problem in the CustomAutoCompleteField

Hi there,

 

When we're talking about the AutoCompleteField, there are two components we can be talking of and I just want to ensure that I'm understanding correctly.

 

In your first post the question was:

So now how to increse size of AutoCompletedField.

 

If we are talking about the EditField component (i.e. the portion where you type text), the easiest way to set the horizontal size of this is by overriding the sublayout method:

 

protected void sublayout(int maxWidth, int maxHeight) {
	super.sublayout(maxWidth / 2, maxHeight);
	super.setExtent(maxWidth / 2, maxHeight);
}

In the sample above, this would resize the EditField component to half the standard width.

 

To set the vertical size, the easiest way would be to alter the font. Example:

 

this.getEditField().setFont(Font.getDefault().derive(Font.PLAIN, 50));

Where this is the CustomAutoCompleteField.

 

However, it sounds like you may also be trying to increase the size allocated to each row of the ListField component. Here, the easiest approach would be to leverate the setRowHeight method:

 

this.getListField().setRowHeight(50);

Again, this refers to the CustomAutoCompleteField.

 

If you are in need of wrapping text like in the image you shared, you would need to override drawListRow and draw the text yourself.

 

Putting this all together, here is a sample of an AutoCompleteField which only uses half the screen width, but displays three lines of text in the ListField (with ... if there is more text.) The sample is two files.

 

/* MyApp.java */

import net.rim.device.api.collection.util.BasicFilteredList;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.container.MainScreen;

public class MyApp extends UiApplication {
	
	private static MyApp _app = null;
	private static MainScreen _screen = null;
	
	public static void main(String[] args) {
		/* Create a new instance of our application. */
		_app = new MyApp();
		
		/* Create an empty list. */
		BasicFilteredList items = new BasicFilteredList();
		
		/* Add 5 default items to our list and add the custom field to the screen. */
		items.addDataSet(0, new Object[] {
			"This is my first search result.",
			"This is my second search result. It is a little longer.",
			"This is third.",
			"This is the fourth search result that keeps going and going and going and going and going and going.",
			"This is the sixth search result."
		},"Sample data.");
		_screen.add(new CustomAutoCompleteField(items));
		
		/* Enter our event dispatcher. */
		_app.enterEventDispatcher();
	}

	public MyApp() {
		/* Push a new blank screen. */
		pushScreen(_screen = new MainScreen());
	}
}

 

Now, the main work is done within our CustomAutoCompleteField implementation.


/* CustomAutoCompleteField.java */

import java.util.Vector;
import net.rim.device.api.collection.util.BasicFilteredList;
import net.rim.device.api.collection.util.BasicFilteredListResult;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.component.AutoCompleteField;
import net.rim.device.api.ui.component.ListField;

public class CustomAutoCompleteField extends AutoCompleteField {

	public CustomAutoCompleteField(BasicFilteredList dataSet) {
		/* Invoke the default constructor. */
		super(dataSet);
		
		/* Set aside enough space for four rows of text per list item. */
		this.getListField().setRowHeight(this.getListField().getFont().getHeight() * 4);
	}

	protected void sublayout(int maxWidth, int maxHeight) {
		/* Assign half the screen's width to our field. */
		super.sublayout(maxWidth / 2, maxHeight);
		super.setExtent(maxWidth / 2, maxHeight);
	}

	/* This will be invoked for each row that needs to be drawn. */
	public void drawListRow(ListField listField, Graphics g, int index, int y, int width) {
		
		/* Get the current result we are drawing. */
		BasicFilteredListResult result = (BasicFilteredListResult)(this.get(listField, index));
		if(result != null) {
			/* Get the text belonging to this result. */
			String text = result._object.toString();
			
			/* Invoke wrapText; a utility method to split a String into pieces. */
			String[] lines = wrapText(text, listField.getFont(), listField.getWidth());
			
			/* We will loop through the split String's lines; up to a maximum of 3 lines. */
			for(int i = 0; i < lines.length && i < 3; ++i) {
				/* Draw this line of text; offset from y based on the line number. */
				g.drawText(lines[i], 0, y + i * listField.getFont().getHeight());
			}
			if(lines.length > 3) {
				/* If we still had more text, reserve the 4th line for ... */
				g.drawText("...", 0, y + (3 * listField.getFont().getHeight()));
			}
		}
	}
}

 The end result is something like this:

 

Flow.PNG

 

Now, this comes with a few caveats:

 

  • This is not a thoroughly tested sample. It was based on the discussion here. Some scenarios/use-cases may cause problems though I tried to weed them out where possible.
  • The wrapText method is based on the code here. Two approaches are discussed there, and those are not the only two approaches available. Depending on your needs, you can implement your own approach.
  • 4 rows of text are designated for each result. To dynamically size each row would require further customization and possibly your own custom field altogether (i.e. as opposed to modifying the AutoCompleteField.)

What the above is, is a basic sample of sizing the results beyond one line of text, then rendering that text across multiple lines.

 

I sure hope I haven't misunderstood the initial question :smileyhappy: But if so, by all means feel free to clarify. If you have any questions about the above, please let me know.

 

Erik Oros

BlackBerry Development Advisor

 


Erik Oros | @WaterlooErik | eoros@blackberry.com | Developer Issue Tracker

If a reply has answered your question, please click Accept as Solution to help other visitors in the future.
Please use plain text.