I took one custom text box image, in that text box I want to show "Enter password" and when I focus on to that text box field and enter any charectors it converts to asterisk symbols like password edit field.
I want like this below Image:
I think it is possible by only overriding paint(..) of the Editfield. May be following steps can achieve the desired behaviour:
Store original text to a local variable.
Generate fake text depending on focus status, original text etc.
Call super.paint(..), it will paint the field with fake text.
Use setText(..) to set original text back.
Draft implementation:
class MyEditField extends EditField {
private final static String INIT_TEXT = "Enter Password";
protected void paint(Graphics graphics) {
String originalText = getText();
String fakeText = originalText;
if (originalText.equalsIgnoreCase("")) {
fakeText = isFocus() ? originalText : INIT_TEXT;
} else {
fakeText = "";
for (int i=0;i<originalText.length();i++) {
fakeText += '*';
}
}
setText(fakeText);
super.paint(graphics);
setText(originalText);
}
}
Above code doesn't paint a border.
We need to set a monochrome font for the problem you have mentioned in comment. Here is a sample code:
MyEditField me = new MyEditField();
try {
Font font = null;
FontFamily fontFamily[] = FontFamily.getFontFamilies();
for (int i=0;i<fontFamily.length; i++) {
if (fontFamily[i].getName().equalsIgnoreCase("Courier New")) {
font = fontFamily[i].getFont(FontFamily.SCALABLE_FONT,
Font.getDefault().getHeight());
me.setFont(font);
break;
}
}
} catch (Exception e) {
}
If I understand you correctly, you need to use a PasswordEditField.
Implement it just like any LabelField, and it will do what you need.
I see you wanted the background text too - I would use drawText() from within the paint() method to draw that. The other answer that shows a full implementation by using EditField is also good.
Related
Please Help!I have Edit Text field whose inputType is number. On the click of Submit button in the firstActivity I want to send the value via Bundle to next activity. I want to make the field compulsory. Before sending the value I am checking if the EditText is blank. if its blank I want to give a Toast message that "Please enter number". I am using below code line to get the numeric text
editTextValue= (EditText) findViewById(R.id.edit_attendance);
editTextValue.setText("0");
var1 = Integer.valueOf((editTextValue.getText().toString()));
For this code to work I have to first set the default value to 0
But due to this when user is entering value in this EditText, he has to first erase 0 and then enter number. I dont want to set it to 0 value. Is there any other way to make this work.
Below is my Code in XML:
<EditText
android:id="#+id/edit_1"
android:layout_marginLeft="5sp"
android:layout_width="100sp"
android:layout_height="wrap_content"
android:inputType="number"
android:hint="0"
android:textColor="#android:color/white" />
FirstActivity.java
onCreate(){
editTextValue = (EditText) findViewById(R.id.edit_1);
editTextValue.setText("0");
btnSubmit.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
var1 = Integer.valueOf((editTextValue.getText()
.toString()));
if (var1 == 0) // when no value is entered
Toast.makeText(getApplicationContext(),
"Please enter value",
Toast.LENGTH_SHORT).show();
else
{
Intent i = new Intent(getApplicationContext(),
SecondActivity.class);
dataBundle.putInt(SecondActivity.VAR1,var1);
i.putExtras(dataBundle);
startActivity(i);
}
}
The below code worked for me:
// editTextValue.setText("0");
editTextValue.setOnEditorActionListener(new OnEditorActionListener() {
public boolean onEditorAction(TextView v, int actionId,
KeyEvent event) {
// TODO Auto-generated method stub
if (actionId == EditorInfo.IME_ACTION_DONE) {
var1 = Integer.valueOf((editTextValue
.getText().toString()));
}
return false;
}
});
I'm trying to create a couple of BasicEditField objects after i get the number of fields that i want from an ObjectChoiceField.
Problem: the BasicEditField fields that i add to my screen don't refresh unless i do it in the listener from my ObjectChoiceField.
what i want to do :
select the number of BasicEditFields that i want.
refresh the screen so the fields added appear.
PD: if you need more info, just tell me, and sorry about my english. I'm new at developing for the BlackBerry plataform
public final class MyScreen extends MainScreen
{
private int fields_lenght;
public MyScreen()
{
// Set the displayed title of the screen
setTitle("Example");
fields_lenght =0;
final String shortcodes[] = {"1","2","3"};
final ObjectChoiceField dropdownlist=new ObjectChoiceField("Select a number of fields",shortcodes);
this.add(dropdownlist);
dropdownlist.setChangeListener( new FieldChangeListener() {
public void fieldChanged( Field arg0, int arg1 ) {
if(arg1 != PROGRAMMATIC){
fields_lenght= Integer.parseInt(shortcodes[dropdownlist.getSelectedIndex()]);
}
}
} );
// how to refresh the screen with the new fields ???
BasicEditField fields[]=new BasicEditField [fields_lenght] ;
for(int i = 0; i<fields.length;i++){
fields[i]=new BasicEditField("Campo "+i,"");
this.add(fields[i]);
}
}
}
You really should add or delete the fields from within your ObjectChoiceField listener. That's when you know what the proper number of fields is. (Certainly, if you just want to keep your code neat and clean, you could define a separate method, that is called from the choice field listener ... that's not much different).
Try something like this:
public final class MyScreen extends MainScreen {
/** A cached vector of the BasicEditFields, to make deleting easier */
private Vector fields;
public MyScreen() {
super(MainScreen.VERTICAL_SCROLL | MainScreen.VERTICAL_SCROLLBAR);
setTitle("Example");
final String shortcodes[] = {"1","2","3"};
final ObjectChoiceField dropdownlist = new ObjectChoiceField("Select a number of fields", shortcodes);
add(dropdownlist);
fields = new Vector();
final Screen screen = this;
dropdownlist.setChangeListener( new FieldChangeListener() {
public void fieldChanged( Field field, int context ) {
if (context != PROGRAMMATIC) {
// how many fields has the user chosen?
int fieldsLength = Integer.parseInt(shortcodes[dropdownlist.getSelectedIndex()]);
while (fieldsLength > fields.size()) {
// we need to ADD more fields
Field f = new BasicEditField("Campo " + fields.size(), "");
fields.addElement(f);
screen.add(f);
}
while (fieldsLength < fields.size()) {
// we need to DELETE some fields
Field f = (Field)fields.elementAt(fields.size() - 1);
fields.removeElement(f);
screen.delete(f);
}
}
}
});
}
I defined a new member named fields, which just makes it easier to keep track of the basic edit fields (in case this screen has many other fields, too).
When the choice field listener is called, I determine how many fields the user wants; if they need more, I add them to the screen, and to the fields Vector. If they want fewer, I delete some fields from the end of the Vector, and remove them from the Screen.
Note: there should be no need to call invalidate() here. Calling Screen#add() or Screen#delete() should add/delete the fields and cause repainting.
In my Application, i am adding a check box, a label field and a Edit Field in a Grid Field manager. Then this grid Field manager, i am adding multiple times in Vertical Field manager. So it is looking like List of items. Now when i checked five check box, i am trying to get the text of the correspondent edit field.
This is the code for Grid Field Manager:
int c[] = {screenWidth/6, (screenWidth)/3, (screenWidth)/2};
gm = new GridFieldManager(c, Manager.VERTICAL_SCROLL);
Logger.out("Grocery", "Here it is coming"+i);
cbfChecked = new CustomCheckBoxField();
cbfChecked.setChangeListener(new FieldChangeListener()
{
public void fieldChanged(Field field, int context)
{
if(checked[i] == false)
{
checked[i] = true;
}
else if(checked[i] == true)
{
checked[i] = false;
Logger.out("Grocery", "It is UnChecked" +checked[i]);
}
}
});
gm.add(cbfChecked);
Logger.out("Grocery", "Adding first Label Field");
LabelFieldCustom lfFrom = new LabelFieldCustom((String) m_vtrItems.elementAt(i),Color.BROWN,FONT_FAMILY_0_SF_AS_16,Field.FIELD_LEFT);
gm.add(lfFrom);
Logger.out("Grocery", "Adding second Label Field");
efcAmount = new EditFieldCustom(Bitmap.getBitmapResource("dob_text_box.png"), 25);
efcAmount.setMargin(new XYEdges(30, 0, 0, 0));
gm.add(efcAmount);
return gm;
Here i am adding the grid field manager multiple times:
for (int i = 0;i < m_vtrItems.size();i++)
{
vfm.add(getRow(i));
vfm.add(new SeparatorField(SeparatorField.NON_FOCUSABLE));
}
Please help me.
I solve the problem. Now i am taking the Edit Field array.
How do I create a RichTextEdit using RIM 4.5 API that contains text with multiple colors?
For example I want to create a RichTextEdit as follows:
The Text is "Hello BB world"
"Hello" should be blue
"BB world" should be red
"BB" should be ITALIC
"Hello" should be BOLD
The main problem is getting colors, not the bold and italic.
I have tried overriding RichTextEdit.paint function, but this formats the color of the whole string, not just a substring of it!
Here's the code I implemented to make the text bold and italic and overriding the paint to change the whole string color:
public final class RichTextFieldSample extends UiApplication
{
public static void main(String[] args)
{
RichTextFieldSample theApp = new RichTextFieldSample();
theApp.enterEventDispatcher();
}
public RichTextFieldSample()
{
String richText = "This is how you create text with formatting!!!";
Font fonts[] = new Font[3];
int[] offset = new int[4];
byte[] attribute = new byte[3];
fonts[0] = Font.getDefault();
fonts[1] = Font.getDefault().derive(Font.BOLD);
fonts[2] = Font.getDefault().derive(Font.BOLD | Font.ITALIC);
offset[0] = 0;
attribute[0] = 2;
offset[1] = 4;
attribute[1] = 0;
offset[2] = 33;
attribute[2] = 1;
offset[3] = richText.length();
RichTextField rtField = new RichTextField
(richText, offset, attribute, fonts,
RichTextField.USE_TEXT_WIDTH) {
protected void paint(Graphics graphics) {
graphics.clear();
graphics.setColor(0x0000FF);
super.paint(graphics);
}
};
MainScreen mainScreen = new MainScreen();
mainScreen.setTitle(new LabelField
("RichTextFieldSample Sample",
LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH));
mainScreen.add(rtField);
pushScreen(mainScreen);
}
}
Unfortunately no easy answer to this one. According to this post, RichTextField doesn't support multiple colors (despite the presence of a getForegroundColors method).
It may be possible to extend RichTextField to support multiple colors, but with the amount of work necessary, it would very likely be easier to implement your own Field from scratch.
I have created Table using vaadin.Now i want to set font size for particular column content in that table.Is it possible to set font size for particular column in that table?.
If so please give me idea to set the font size.If u can provide me some code snippet.
Yes, with CellStyleGenarators. Check 5.12.2 in the Book of Vaadin. You basically do a
if(propertyId.equals(yourColumnName)) {
return "someStyleName";
}
else {
return null;
}
inside your Table.CellStyleGenerator() and set the style for your text in css.
using CellStyleGenerator
simpleTable.setCellStyleGenerator(new Table.CellStyleGenerator() {
#Override
public String getStyle(Table components, Object itemId, Object columnId) {
int row = Integer.valueOf((String)itemId);
if (row%2 == 0)
return "grey";
else
return "white";
}
});
ColumnGenerator as it is described in How to get started with Vaadin: Table Styling
public class DescriptionColumnGenerator implements
Table.ColumnGenerator {
#Override
public Object generateCell(Table components, Object itemId, Object columnId) {
int row = Integer.valueOf((String)itemId);
Property prop = components.getItem(itemId).getItemProperty(columnId);
Label label = new Label("desc: " + prop.getValue());
if (row%2 != 0) {
label.addStyleName("column-description");
label.addStyleName("column-" + (String) columnId);
}
return label;
} }
You can add a style name for this column.