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
ankur_siwach
Posts: 145
Registered: ‎09-28-2012
My Device: Blackberry Developer
Accepted Solution

i want to change deafault blackberry focus color of listfield

Hello,

 

i want to change deafault(blue) blackberry focus color of listfield,below are the sample code ,code is working fine ,i just want to customize focus color of the list..please suggest me...

 

package Meidcare;

import java.util.Enumeration;
import java.util.Vector;

import net.rim.device.api.collection.util.SortedReadableList;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.Display;
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.component.KeywordFilterField;
import net.rim.device.api.ui.component.KeywordProvider;
import net.rim.device.api.ui.component.ListField;
import net.rim.device.api.ui.component.ListFieldCallback;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.decor.BackgroundFactory;
import net.rim.device.api.util.Comparator;
import net.rim.device.api.util.StringUtilities;

class SearchFieldDemoScreen extends MainScreen
{
Vector drugs_list = new Vector();
Bitmap buttonleft,buttonright;
CountryList _countryList;
KeywordFilterField _keywordFilterField;
int mHColor = -1;

public SearchFieldDemoScreen(Vector drugs_list) {
this.drugs_list=drugs_list;
this.getMainManager().setBackground(BackgroundFactory.createSolidBackground(0xC2C2C2));

buttonleft=Bitmap.getBitmapResource("medical_pills.png");
buttonright=Bitmap.getBitmapResource("white_aero.png");

_countryList = new CountryList();
_keywordFilterField = new KeywordFilterField();


Enumeration enumumeration = drugs_list.elements();
while (enumumeration.hasMoreElements()) {
_countryList.addElement(new Country(enumumeration.nextElement().toString()));

}


try{
setTitle(_keywordFilterField.getKeywordField());

_keywordFilterField.setLabel("Find:");
_keywordFilterField.setSourceList(_countryList, new Country.MyProvider());
_keywordFilterField.setCallback(new MyListFieldCallback());

_keywordFilterField.setFocus();

add(_keywordFilterField);

}
catch (Exception e) {
System.out.println("Exception in keyword filter field="+e);
}
}

class MyListFieldCallback implements ListFieldCallback {

public void drawListRow(ListField listField, Graphics g, int index, int y, int width) {
Object obj = ((KeywordFilterField)listField).getElementAt(index);
g.setColor(Color.BLACK);

if(obj != null && obj instanceof Country) {
Country item = (Country) obj;
g.drawText(item.toString(), 20 , y ,DrawStyle.ELLIPSIS|DrawStyle.HCENTER);
g.drawLine(0, y-9, width, y-9 );
g.setColor(Color.WHITE);
g.fillRect(0, y-9, width, 3); // for 3 pixel thick seprater "line"
g.setColor(Color.BLACK);

} else if(index == 0) {
g.drawText("* Empty *", 0, y);
}

}

protected void drawFocus(Graphics graphics, boolean on){
//graphics.setColor(Color.RED);
paint(graphics);
}

public Object get(ListField list, int index) {
return drugs_list.elementAt(index);
}

public int getPreferredWidth(ListField list) {
return Display.getWidth();
}

public int indexOfList(ListField list, String prefix, int start) {
return 0;
}
}

class CountryList extends SortedReadableList
{
//Vector drugs_list=new Vector();

public CountryList()
{
super(new Country.CountryListComparator());
}

void addElement(Object element)
{
try{
//System.out.println("ankur");
//SearchFieldDemoScreen.ls2=new ListStyleButtonSet();
//SearchFieldDemoScreen.ls2.add((Field) element);
doAdd(element);
}
catch (Exception e) {
System.out.println("Exception in add element="+e);
}
}

}
}

class Country
{
private String _countryName;

public Country(String countryName)
{
_countryName = countryName;
}

public String toString()
{
return _countryName;
}

static class MyProvider implements KeywordProvider {
public String[] getKeywords(Object obj) {
//Country item = (Country) obj;
//return new String[]{ item._countryName };
//System.out.println("MyProvider called");
return StringUtilities.stringToWords(obj.toString());
}
}

static class CountryListComparator implements Comparator
{
public int compare(Object o1, Object o2)
{
//System.out.println("CountryListComparator called in compare method");
if (o1 == null || o2 == null)
throw new IllegalArgumentException("Cannot compare null countries");
return o1.toString().compareTo(o2.toString());
}
}
}

 

thanx in advance... :smileyhappy:

 

   


----------------------------------------------------------
Feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
Please use plain text.
Developer
peter_strange
Posts: 19,607
Registered: ‎07-14-2008
My Device: Not Specified

Re: i want to change deafault blackberry focus color of listfield

This question has been asked before.  As a general rule I recommend that you search the forum before asking a quesiton - you are likely to find a solution for most common problems and I generally find some really interesting Threads when I search too!

 

Anyway, I searched for you and found these which I think will help:

 

http://supportforums.blackberry.com/t5/Java-Development/ListField-Background-Color-Highlighted/m-p/7...

 

http://supportforums.blackberry.com/t5/Java-Development/ListField-Focus-and-Background-Color/m-p/185...

 

Please use plain text.
Developer
ankur_siwach
Posts: 145
Registered: ‎09-28-2012
My Device: Blackberry Developer

Re: i want to change deafault blackberry focus color of listfield

Thank you dear for your support ,

i have search before posting and see all the problems and answer..but its not working please have a look on my sample code..as i am not extending ListField class...in my sample code


----------------------------------------------------------
Feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
Please use plain text.
Developer
simon_hain
Posts: 16,190
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport

Re: i want to change deafault blackberry focus color of listfield

yes, you are using ListField.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Please use plain text.
Developer
peter_strange
Posts: 19,607
Registered: ‎07-14-2008
My Device: Not Specified

Re: i want to change deafault blackberry focus color of listfield

Like Simon, I see ListField in your sample.

 

I suggest you create a really simple sample, that just demonstrates the problem and has nothing extra in it (like Backgrounds).  Then we can try ourselves if you don't get it working the way you want. 

Please use plain text.
Developer
ankur_siwach
Posts: 145
Registered: ‎09-28-2012
My Device: Blackberry Developer

Re: i want to change deafault blackberry focus color of listfield

oky i have solved the issue.. 

using ...

 

if(g.isDrawingStyleSet(Graphics.DRAWSTYLE_FOCUS)) {

//change focus color
g.setBackgroundColor(0xDCDCDC);
g.clear();
//draw text
g.setFont(Resize.getSecondFont());
g.setColor(Color.BLACK);
g.drawText(item.toString(), 20 , y ,DrawStyle.ELLIPSIS|DrawStyle.HCENTER);
}

 

inside the drawListRow...

 

thanx to all...

 

----------------------------------------------

Press like if  you like it..


----------------------------------------------------------
Feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
Please use plain text.
Developer
peter_strange
Posts: 19,607
Registered: ‎07-14-2008
My Device: Not Specified

Re: i want to change deafault blackberry focus color of listfield

Good stuff, can you mark this as solved then?

Please use plain text.
Developer
peter_strange
Posts: 19,607
Registered: ‎07-14-2008
My Device: Not Specified

Re: i want to change deafault blackberry focus color of listfield

Actually looking at your code, I can see a potential problem.  I think this code:

 

g.setBackgroundColor(0xDCDCDC);
g.clear();

 

will change the background color for the entire Field, not just the row you are painting.  So I think you need to use fillRect and restrict it to the height given by the y value that is passed in, and the height of each of your rows - getRowHeight().  The width should also be restricted - from 0 to the width that is passed in.

 

Hope this helps. 

Please use plain text.