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
babakar
Posts: 714
Registered: ‎05-29-2012
My Device: see later
My Carrier: see later
Accepted Solution

Listfield color

package mypackage;
import java.util.Vector;



import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.Display;
import net.rim.device.api.system.EncodedImage;
import net.rim.device.api.ui.DrawStyle;

import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.FontFamily;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.XYRect;
import net.rim.device.api.ui.component.ListField;
import net.rim.device.api.ui.component.ListFieldCallback;

public class SingersListField extends ListField {
 
	  private Vector content = null;
	  private ListCallback callback = null;
	  private int currentPosition = 0;

	  /**
	    * Instantiates a new title list and populates it with the appropriate items.
	    *
	    * @param content The content to be loaded in the list.
	    */
	  public SingersListField (Vector content) {
	    this.content = content;
	    initCallbackListening();
	   // font = this.getFont().derive(Font.BOLD, 16);
	   // this.setFont(font);
	    this.setRowHeight(100);    // I think this is an important point for it to work: set the height of the row outside the draw method.
	    // Other possibly useful info is that the negative signal means I will multiply the font size by the number provided.
	  }

	  private void initCallbackListening() {
	    callback = new ListCallback();
	    this.setCallback(callback);
	  }
		
	  /**
	    * Callback implementation to this custom list field. 
	    * 
	    * @author Alaa El-Khatib
	    */
	  private class ListCallback implements ListFieldCallback {
	  
	    private Bitmap image = null;
	    Bitmap Arrow = Bitmap.getBitmapResource("arrow.png");
	    Bitmap Arrow2 = Bitmap.getBitmapResource("arrow2.png");
	    public ListCallback() {
	    }
	    public void drawListRow(ListField listField, Graphics graphics, int index, int y, int width) {
	    	  if(index!=-1){
	      currentPosition = listField.getSelectedIndex();
	      int yPos = 5 + y; // initial position will consider an offset.				
	      int xPos = 15; // left offset, fixed for all rows in the list.
	     Song temp =  (Song)content.elementAt(index);
	      image=MAMClient.sizeImage(EncodedImage.createEncodedImage(temp.getAlbumCover(), 0, temp.getAlbumCover().length),110,110).getBitmap();
	      graphics.drawBitmap(5, yPos, image.getWidth(), image.getHeight(), image, 0, 0);
	      xPos = xPos + image.getWidth();  
	      if (graphics.isDrawingStyleSet(Graphics.DRAWSTYLE_FOCUS))
	        {
	    	 // graphics.setColor(0x858686);
	    	  Font font = getFont().derive(Font.BOLD, 32); 
	    	  graphics.setFont(font);
			  graphics.drawText(temp.albumName,  xPos , yPos,DrawStyle.ELLIPSIS,width-(image.getWidth()+Arrow2.getWidth()+10));
			  font = getFont().derive(Font.PLAIN, 32); 
		    	 graphics.setFont(font);
			  graphics.drawText(temp.singerName,  xPos , yPos+25,DrawStyle.ELLIPSIS,width-(image.getWidth()+Arrow2.getWidth()+10));
			  font = getFont().derive(Font.ITALIC, 32); 
		    	 graphics.setFont(font);
		    	 graphics.drawText("ALBUM - "+temp.getPrice()+" KWD",  xPos , yPos+50,DrawStyle.ELLIPSIS,width-(image.getWidth()+Arrow2.getWidth()+10));
		  	  graphics.drawBitmap(width-Arrow2.getWidth()-5, yPos, Arrow2.getWidth(), Arrow2.getHeight(), Arrow2, 0, 0);}
	        else
	        { 
	          //graphics.setColor(0x60347A);
	        	Font font = getFont().derive(Font.BOLD, 32); 
		    	 graphics.setFont(font);
	          graphics.drawText(temp.albumName,  xPos , yPos,DrawStyle.ELLIPSIS,width-(image.getWidth()+Arrow.getWidth()+10));
	          font = getFont().derive(Font.PLAIN, 32); 
		    	 graphics.setFont(font);
	          graphics.drawText(temp.singerName,  xPos , yPos+25,DrawStyle.ELLIPSIS,width-(image.getWidth()+Arrow2.getWidth()+10));
	          font = getFont().derive(Font.ITALIC, 32); 
		    	 graphics.setFont(font);
		    	 graphics.setColor(Color.GRAY);
		    	 graphics.drawText("ALBUM - "+temp.getPrice()+" KWD",  xPos , yPos+50,DrawStyle.ELLIPSIS,width-(image.getWidth()+Arrow2.getWidth()+10));
	  		 graphics.drawBitmap(width-Arrow.getWidth()-5, yPos, Arrow.getWidth(), Arrow.getHeight(), Arrow, 0, 0); }
	       graphics.setColor(0x60347A);
	      yPos = y + listField.getRowHeight() - 1;
	      graphics.drawLine(0, yPos,Display.getWidth(), yPos);
	    	  }}

		public Object get(ListField listField, int index) {
	      return content.elementAt(index);
	    }

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

	    public int indexOfList(ListField listField, String prefix, int start) {
	      return content.indexOf(prefix, start);
	    } }

	  // This method returns the index of the highlighted item in the list, which will be useful somewhere in my code.
	  public int getCurrentPosition() {
	    return currentPosition;
	  }	
	  
	  protected boolean navigationClick(int status, int time) {
			fieldChangeNotify(0);  
		    //Song temp =  (Song)content.elementAt(currentPosition);
			//UiApplication.getUiApplication().pushScreen(new SingersCategory(temp.getSingerId(),temp.albumName,temp.getAlbumCover()));
		    return true;
		}
	  
	  protected void drawFocus(Graphics graphics, boolean on) {
		  XYRect rect = new XYRect();
		  getFocusRect(rect);
		  if (on) {
		  graphics.setColor(Color.PURPLE);
		  graphics.fillRect(rect.x, rect.y, rect.width, rect.height);
		  getCallback().drawListRow(this, graphics, getSelectedIndex(), rect.y, rect.width);
		  }
	  } }

 

 

As you see I have a list, on selection of the row I highlight it purple.But since letters are black, they disappear.What advice can u offer?

Can I on selection change black text colors to white? if yes can u give how?

or i make adjustments to suitable colors?and can u advice what colors?

Please use plain text.
Developer
simon_hain
Posts: 16,023
Registered: ‎07-29-2008
My Device: Z10 LE
My Carrier: O2 Germany

Re: Listfield color

set a different text color when the row is highlighted
----------------------------------------------------------
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
babakar
Posts: 714
Registered: ‎05-29-2012
My Device: see later
My Carrier: see later

Re: Listfield color

but how do u that? how can i on highlight change the text color?
Please use plain text.
Developer
simon_hain
Posts: 16,023
Registered: ‎07-29-2008
My Device: Z10 LE
My Carrier: O2 Germany

Re: Listfield color

graphics.setColor
----------------------------------------------------------
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
babakar
Posts: 714
Registered: ‎05-29-2012
My Device: see later
My Carrier: see later

Re: Listfield color

[ Edited ]

argh, graphics.setcolor set the color of the highlighted row.

 

protected void drawFocus(Graphics graphics, boolean on) {
		  XYRect rect = new XYRect();
		  getFocusRect(rect);
		  if (on) {
		  graphics.setColor(Color.PURPLE);
		  graphics.fillRect(rect.x, rect.y, rect.width, rect.height);
		  getCallback().drawListRow(this, graphics, getSelectedIndex(), rect.y, rect.width);
		  }
	  }


but i want to change the color of the texts inside the highlighted row to make them visible.

i want to change them from black to white on selection

Please use plain text.
Developer
simon_hain
Posts: 16,023
Registered: ‎07-29-2008
My Device: Z10 LE
My Carrier: O2 Germany

Re: Listfield color

i would not use drawFocus, just check the DRAW_FOCUS style (as you already do it).

whenever you use setColor, remember the current color and set it back to it after you used 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.
@SimonHain on twitter
Please use plain text.
Developer
babakar
Posts: 714
Registered: ‎05-29-2012
My Device: see later
My Carrier: see later

Re: Listfield color

I have solved the issue with this

drawHighlightRegion(graphics, HIGHLIGHT_FOCUS, on, rect.x, rect.y, rect.width, rect.height);

Please use plain text.