How to insert text between characters in a EditText on Android? - android

Im getting an Hour value from a database which I am working and I can't change it.
The value can be for example "1800" which means "18:00" or "930" which means "9:30". This value appears on a EditText.
So how can I insert the ":" in that value before it shows in the EditText.
Thanks!

According to the format you provided, the last two characters must be minute, so you can substring it and add the ":" in the middle.
String str = "1800";
final int len = str.length();
String formattedStr = str.substring(0, len - 2) + ":" + str.substring(len - 2, len); // 18:00

You can also use setText[] with the character[] attribute
char[] your_addition = {':'};
//use setText(char[], int start, int length)
your_editext.setText(your_addition, 2, 1);
You can also check the length of your string to make sure that the value for int start is correct using 2 when the length is 4 and 1 when the length is 3.
Hope this helps

try to split your string which is getting set to edit text. use yourString.split(":");

Related

Converting int 0 to binary in android studio

I am converting 4 integers into binary and everything works fine until it has to convert a zero.
for example:
int subnet1 = 255;
int subnet2 = 255;
int subnet3 = 255;
int subnet4 = 0;
binarystring = Integer.toBinaryString(subnet1)
+ Integer.toBinaryString(subnet2)
+ Integer.toBinaryString(subnet3)
+ Integer.toBinaryString(subnet4);
BinaryView.setText(binarystring);
text would look like this: 11111111 11111111 11111111 0 (without the space inbetween)
why wont it convert the 0 to 00000000 ??
Because value for "0000000" is same for "0" that's why it set as "0".
If you want to print that result. Just make a string of "0000000" for printing. Because int value always convert it.
Why? By design. From the documentation:
This value is converted to a string of ASCII digits in binary (base 2) with no extra leading 0s.
(Emphasis added.)
If you want 8 characters, append the result to a string of 7 zeros, and take the last 8 characters. In pseudo code:
intermediateString = "0000000" + Integer.toBinaryString( 0 ); // obviously don't hard code zero; just for example
finalString = intermediateString.substring( intermediateString.length() - 8);
If you read the documentation here: https://developer.android.com/reference/java/lang/Integer.html#toBinaryString(int)
It tells you:
The unsigned integer value is the argument plus 232 if the argument is negative; otherwise it is equal to the argument. This value is converted to a string of ASCII digits in binary (base 2) with no extra leading 0s.

how to remove special characters when i paste +1-xxx-xxx-xxxx from another source in android edit text with 10 digits length in android

copy from another source is : +1-541-xxx-3010
when i paste in my edit text i need to get the result as follow :
541xxx3010
need to remove special characters and also need to remove +1 (country code)
i need to display only 10 digits actual number after removing special chars
#Override
public void onTextChanged(CharSequence s, int start, int before, int count){
String edit = s.toString();
System.out.println("##"+edit);
edit = edit.replaceAll("[^0-9]", "");
String result = edit.replaceAll("[|?*<\":>+\\[\\]/'-]","");
System.out.println(result);
}
You can try SubString to get only 10 digit from your string.
For E.g. Your String is +1-541-xxx-3010. Now as you want you need to remove the + and country code.
String phone = "+1-541-xxx-3010"
result = phone.substring(Math.max(phone.length() - 12, 0)).replaceAll("-", ""));
From the above code it returns the result like this.
Result: 541xxx3010
NOTE : Before apply this code you need to check if your string is not
empty or its size is greater than or equal 12.
OR
If you don't want to use + and - then why don't you restrict the character using android:digits just apply below property in edittext.So it will not allow to enter the character apart from 0-9.
android:digits="0123456789"
First you need to remove country code from the string using substring() method,
For remove special character in your case it is "-" you can refer below code snippet
String result = inputString.replaceAll("[-]","");

How to replace character at particular index in android?

I am working on android. I have a string containing huge data. In that string I want to replace a particular character to another character. I got the index of the character which I want to replace. But I am unable to replace that character.
How can I do that?
String str = "data1data2mdata2test1test2test3dd"
int ind = str.indexOf("m");
System.out.println("the index of m" + ind);
Now in the above string I want to replace the character "m"(after data2) to "#".
Now how can I replace the m to #. Please help me in this reagard.
You can use substring:
String newStr = str.substring(0, ind) + '#' + str.substring(ind + 1);
Try this:
str = str.replaceFirst("m", "#");
It will replace the first m to #
String str1 = "data1data2mdata2test1test2test3dd"
String str = str1.replace("m", "#");
System.out.println(str);
So you are getting 10 as system out,
so this way you can replace it like,
Str.replace('m', '#')--->when you want all occurrences of it to replace it,
Or if you want only first occurrence to be replaced by # then you can do following trick,
StringBuffer buff=new StringBuffer();
buff.append(Str.substring(0,ind)).append("#").append(Str.substring(ind+1));
i hope it would help

Add a number and a Text Input value with adobe flex

I am trying to add a number and a text input value to display in a label. here is my code thus far.
'lblAnswer.text = bloodglucose + 100;'
Please tell me what I am doing wrong.
Please try following answer -
bloodglucose += 100;
lblAnswer.text = String(bloodglucose);
Hope this will work :)
Sunil is correct - when doing mixed type addition, the UI input first needs to be coerced to either int or Number. IE: Number(bloodglucose) + 100; This assumes bloodglucose is actually a getter to the input text reference. If it's not, then you need to coerce the property and not the id of the component.
Getter: public function get bloodglucose():Number { return Number(myInput.text); }
In method: lblAnswer.text = bloodglucose + 100;
or (bloodglucose is a UIComponent):
In method: lblAnswer.text = Number(bloodglucose.text) + 100;
You should use String(int i)
lblAnswer.text = String(bloodglucose + 100);
Update: What about something like this:
var i:int = bloodglucose + 100;
var s:String = String(i);
lblAnswer.text = s;
** Update ,
I am changing the code from the update that was previously posted. I initially found that because I was including the string value inside of the equation this is what was prompting an error. You have to wrap the converted components to Number inside of the string all together. Basically convert the components to a number, then convert the answer received into a string.
Below is an example of the wrong code.
txtAnswer = (String(Number(bloodglucose)+100)) / 36)).toFixed(2)
Below this line is the fixed code.
txtAnswer.text = String( (Number(bloodglucose.text) + (Number(100))/ (Number(36))).toFixed(2) ;
The .toFixed Property signifies how many decimal places I want the returned value to display.

Reformatting a String in Java

For my app I have created a QR Code, then took that bitmap and added text to the bitmap, however I need the text not to extend longer then the bitmap is. So what I want to do is create an Array of the text by taking 25 characters then find the last index of (" ") in that 25 character section. at that space I want to be able to replace that space that was located with \n to start a new line.
So the plan is if I have a String that looks like "Hello this is my name and I am longer than 25 charters and I have lots of spaces so that this example will work well."
I want it to out up this
Hello this is my name and
I am longer than 25
charters and I have lots
of spaces so that this
example will work well.
To make this I counted 25 characters then went back to the most resent space, at that point I hit enter, I want my app to do this for me.
I am not very good at English so if something doesn't make sense tell me and I will try to explain it. Thanks
I haven't tested this but you can try it and tweak as necessary
String fullText = "your text here";
String withBreaks = "";
while( fullText.length() > 25 ){
String line = fullText.substring(0,24);
int breakPoint = line.lastIndexOf( " ");
withBreaks += fullText.substring(0,breakPoint ) + "\n";
fullText = fullText.substring( breakPoint );
withBreaks += fullText;
char [] way (more C like):
public static String reduceLength(String s, int len){
char [] c = s.toCharArray();
int i=len, j=0, k;
while(true){
for(k=j; k<=i; k++){
if (k >= s.length()) return new String(c);
if (c[k] == ' ') j=k;
}
c[j] = '\n';
i= j+ len;
}
}
This isn't safe, just something i threw together.

Categories

Resources