miércoles, 13 de abril de 2011

Android: Writting long text on Canvas

Some time ago i researched how to write a long text on canvas on my Android apps. I finally just used this code:


TextPaint texto = new TextPaint();
StaticLayout layout = new StaticLayout("My long long long long text! My long long long long text! .", texto, 140,android.text.Layout.Alignment.ALIGN_CENTER,(float)1.0, (float)0.0, true);
layout.draw(canvas);


lunes, 11 de abril de 2011

BlackBerry: My Custom ListField

One of the problems we can find when we create our first BlackBerry app is to create a Custom ListField, where we can decide which colors to use for selecter or unselected elements in our list.



Here i will show you how to create a simple ListField, you can change it's color as you like.

I'm creating a new class on my project which i extend from MainScreen, so i can push it, and it will implement the ListFieldCallback.

MyCustomListField
public class MyCustomListField extends MainScreen implements ListFieldCallback{
private int selectedColor = 0x8f52a8;
private int defaultColor =  0xf2f2f2;
private int rowHeight = 50;
private Vector elements;
private ListField customList;
public MyCustomListField(){
setTitle("My Custom List Field!");
elements = new Vector();
for(int i=0; i<30; i++){
elements.addElement("My #"+i+" element");
}
customList = new ListField(elements.size());
customList.setCallback(this);
customList.setRowHeight(70);
add(customList);
}
public void drawListRow(ListField list, Graphics g, int index, int y, int w) {
String text = (String)elements.elementAt(index);
if(g.isDrawingStyleSet(Graphics.DRAWSTYLE_FOCUS)){
g.setColor(selectedColor);
g.fillRect(0,y,Display.getWidth(),list.getRowHeight());
}else{
g.setColor(defaultColor);
g.fillRect(0,y,Display.getWidth(),list.getRowHeight());
}
g.setColor(0x000000);
g.drawLine(0, y, Display.getWidth(), y);
g.drawText(text, 0, y, 0, w);
}

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

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

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



Here you can download the full project example: MyCustomListField Example

viernes, 8 de abril de 2011

BlackBerry: Streaming Audio

WoW! I've been many days trying to stream audio on my BlackBerry app, using the BufferedPlayBackDemo from the BlackBerry JDE examples with no success. I was so annoyed because it worked on my emulator, but never on my devices, so i decided to keep on researching and i finally used another code.

The code i'm actually using for my streaming is very much lite and easier than the BlackBerry's example, you only have to use this function, and voila! your app will stream audio from the internet!


public void Player(String url){
    Player p;
    
    try {
p = Manager.createPlayer(url);
p.start();
} catch (IOException e) {
e.printStackTrace();
System.out.println("I/O Exception: "+e);
    } catch (MediaException e) {
e.printStackTrace();
System.out.println("Media Exception: "+e);
    }
}


You can download my entire project example from this link STREAMING AUDIO EXAMPLE

BlackBerry: Custom Label Field

Some times when we're developing UI screens for a BlackBerry app we find that there are not too many fancy UI elements.

Here i let you a code i wrote for my app. It can not be as optimized as it could, but i'm just starting BlackBerry developing and learning every day something new! I just wanted to share it with all the people who could need it.

Si if you want to create what i show you in this image, this is your code!!



I created a CustomLabel class where i create my title and my text fields, i set their colors and the background style.

CustomLabel
package com.ui;

import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.XYEdges;
import net.rim.device.api.ui.component.EditField;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.VerticalFieldManager;
import net.rim.device.api.ui.decor.BackgroundFactory;
import net.rim.device.api.ui.decor.Border;
import net.rim.device.api.ui.decor.BorderFactory;

public class CustomLabel{
//You can change this colors as you like!
private int titleFontColor = 0x2bb7de;
private int textFontColor=0x000000;
private int bgColor = 0xe8e8e8;
private VerticalFieldManager vm;
private LabelField titleLabel;
private EditField textField;
private Font _font;
private int _labelHeight;
private int _labelWidth;

public CustomLabel(String title, String text){
//We create a Vertical Field Manager that will wrap all our content
vm = new VerticalFieldManager(Manager.USE_ALL_WIDTH);
//Now we set our border style to simulate a cool efect
XYEdges padding = new XYEdges(15, 15, 15, 15);
vm.setBorder(BorderFactory.createRoundedBorder(padding, bgColor, Border.STYLE_FILLED));
//We set the same Background color to the border and to our
//Manager Background so it looks all the same field
vm.setBackground(BackgroundFactory.createSolidBackground(bgColor));
//We create our title LabelField and set the same background color
titleLabel = new LabelField(""){
public void paint(Graphics g){
g.setColor(titleFontColor);
super.paint(g);
}
};
titleLabel.setText(title);
//Our text LabelField again with the same background color
textField = new EditField("","", bgColor, EditField.READONLY){
public void paint(Graphics g){
g.setColor(textFontColor);
super.paint(g);
}
};
textField.setText(text);
//Now we set some padding and add our elements to our CustomField! 
vm.setPadding(5, 5, 5, 5);
vm.add(titleLabel);
vm.add(textField);
_labelHeight = textField.getHeight()+titleLabel.getHeight();
_labelWidth = textField.getWidth()+titleLabel.getWidth();
}
public VerticalFieldManager getField(){
return vm;
}
}

You can download the full project example from here CustomLabel Project