TextView live text changed as format dd/mm/yyyy using c# - android

In UWP c# in textbox.textchange event I'm using code like that
public static void wpisywanie_daty(TextBox sender)
{
var nowyWpis = "";
var plainText = sender.Text.Replace("/", "");
if (plainText.Length <= 5)
{
for (int i = 1; i <= plainText.Length; i++)
{
nowyWpis += plainText[i - 1];
if (i % 2 == 0) nowyWpis += "/";
}
sender.Text = nowyWpis;
sender.Select(sender.Text.Length, 0);
}
if (sender.Text.Length >= 10)
{
sender.IsEnabled = false;
sender.IsEnabled = true;
}
}
changing dinamicly text to format dd/mm/yyyy when you typing.
How to implement it in android c# xamarin?
When the code hit line sender.Text = nowyWpis; it rise textchanged eventhandler and start code from beginning.

Related

Finding App Architecture using android API

Suppose we can use getPackageInfo in Package Manager in android and get any installed app's versionCode and all.
So can we find the architecture or the app? Like it's arm-v7a or arm64
I'll be very helpful to you.
Thank you
I found your question for "Java custom string encryption from a paragraph or set of strings", and would like to help because I've done something similar in c#.
I noticed that one of the largest reasons for that question being closed is because you were lacking in certain details that would help us determine what you needed.
This is an example of mine:
public static string Trans(string data) {
data = ToBinary(ConvertToByteArray(data));
string[] Adata = data.Split();
string output = "";
foreach(string word in Adata) {
int count = 0;
bool counting1s = false;
count = 0;
output = "";
foreach(var ch in word) {
if (ch == '0' && counting1s) {
counting1s = false;
if (count > 0) {
output += bit1[count - 1];
count = 0;
}
}
if (ch == '1' && !counting1s) {
counting1s = true;
if (count > 0) {
output += bit0[count - 1];
count = 0;
}
}
count++;
}
if (count > 0) {
output += (counting1s ? bit1 : bit0)[count - 1];
}
}
return output;
}
I could help you with making something in Java if youd like.

Realm - Do not update if property equals

I am using realm for Android. I have the following code and it works but I was wondering if it is the best way to go about updating objects and if it would cause any performance issues.
Currently, I do not want to update an existing object if the status is set to processing.
List<WorkOrderObject> woList = new ArrayList<>();
for (int i = 0; i < openWorkOrders.size(); i++) {
if (!visnetawrap.isUserLoggedIn) {
return;
}
WorkOrderObject wo = visnetawrap.gsonClient.fromJson(openWorkOrders.get(i).toString(), WorkOrderObject.class);
WorkOrderObject currWO = realmThread.where(WorkOrderObject.class).equalTo("id", wo.getOrderRawId()).findFirst();
if (currWO != null) {
if (currWO.getOrderStatus().equals("Processing")) {
continue;
}
}
issueDateTime = AppUtils.formatTimestampToDateTime(wo.getOrderIssueDate());
issueDateString = issueDateTime.toLocalDateTime().toString("MM/dd/yyyy", Locale.US);
dueDateTime = AppUtils.formatTimestampToDateTime(wo.getOrderDueDate());
dueDateString = dueDateTime.toLocalDateTime().toString("MM/dd/yyyy", Locale.US);
if (!issueDateString.equals("") && !issueDateString.equals("00/00/0000") && issueDateTime.getYear() >= now.getYear() && !dueDateString.equals("") && !dueDateString.equals("00/00/0000") && dueDateTime.getYear() >= now.getYear()) {
//Log.d("dueDate", dueDateString);
woList.add(wo);
}
}
realmThread.beginTransaction();
realmThread.copyToRealmOrUpdate(woList);
realmThread.commitTransaction();
I think basically it is the same.
Since you are worried about performance here are ways you can improve.
private static String PROCESSING = "Processing";
private static String DATE_FORMAT = "MM/dd/yyyy";
private static String EMPTY_DATE = "00/00/0000";
public void betterMethod() {
List<WorkOrderObject> woList = new ArrayList<>(openWorkOrders.size());
//I think this code doesnot need to be inside loop.
if (!visnetawrap.isUserLoggedIn) {
return;
}
for (int i = 0, j = openWorkOrders.size(); i < j; i++) {
//Since you are using gson there are ways to convert JsonArray to list directly which is a better way than this
WorkOrderObject wo = visnetawrap.gsonClient.fromJson(openWorkOrders.get(i).toString(), WorkOrderObject.class);
WorkOrderObject currWO = realmThread.where(WorkOrderObject.class).equalTo("id", wo.getOrderRawId()).findFirst();
if (currWO != null && currWO.getOrderStatus().equals(PROCESSING)) { //Its cleanar way
continue;
}
issueDateTime = AppUtils.formatTimestampToDateTime(wo.getOrderIssueDate());
issueDateString = issueDateTime.toLocalDateTime().toString(DATE_FORMAT, Locale.US);
dueDateTime = AppUtils.formatTimestampToDateTime(wo.getOrderDueDate());
dueDateString = dueDateTime.toLocalDateTime().toString(DATE_FORMAT, Locale.US);
//I assume you have stripped out code where it needs string
//You can use TextUtils.isEmpty() or issueDateString.isEmpty() ,
// issueDateString.equals("") does is creates new String which is empty and compares issueDateString with it while above methods just check the
//length of string
if (!TextUtils.isEmpty(issueDateString) && !issueDateString.equals(EMPTY_DATE) && issueDateTime.getYear() >= now.getYear() && !TextUtils.isEmpty(dueDateString) && !dueDateString.equals(EMPTY_DATE) && dueDateTime.getYear() >= now.getYear()) {
//Log.d("dueDate", dueDateString);
woList.add(wo);
}
}
if (!woList.isEmpty()) {
realmThread.beginTransaction();
realmThread.copyToRealmOrUpdate(woList);
realmThread.commitTransaction();
}
}
For loop can be very large so conditional statement like currWO.getOrderStatus().equals("Processing") will create an new string and compares. It's better to initialize the string before and pass as above.
Converting JsonArray to List
Why instantiating arrays like new ArrayList<>(openWorkOrders.size()) and using for loop with list like for (int i = 0, j = openWorkOrders.size(); i < j; i++) {}
Streamlining Android Apps: Eliminating Code Overhead by Jake Wharton

Android : Remove last paragraph tag generated by Html.toHtml

I'm editing content that may be html in an EditText. Just before saving the result, I use Html.toHtml to convert the input into a string to be sent to the server. However this method call seems to be generating paragraph tags which I dont need. Eg -
Test edited
seems to get converted to
<p dir="ltr">Test edited</p>
I would like to strip out the last paragraph tag before saving the content. If there are other paragraph tags, I would like to keep those. I have this regex that matches all p tags
"(<p.*>)(.*)(</p>)";
but I'm not sure how to match just the last paragraph and remove just the tags for it.
public static void handleOneParagraph(SpannableStringBuilder text) {
int start = 0;
int end = text.length();
String chars1 = "<p";
if (end < 2)
return;
while (start < end ) {
String seq = text.toString().substring(start, start + 2);
if (seq.equalsIgnoreCase(chars1))
break;
start++;
}
if (text.toString().substring(start, start + 2).equalsIgnoreCase(chars1) ) {
int start2 = start + 1;
String chars2 = ">";
while (start2 < end && !text.subSequence(start2, start2+1).toString().equalsIgnoreCase(chars2) ) {
start2++;
}
if (start2 >= end)
return;
text.replace(start, start2+1, "");
end = text.length();
start = end;
String chars3 = "</p>";
while (start > start2 + 4) {
String last_p = text.subSequence(start - 4, start).toString();
if (last_p.equalsIgnoreCase(chars3) ) {
text.replace(start - 4, start, "");
break;
}
start--;
}
}
}
And now, you can use it like this...
SpannableStringBuilder cleaned_text = new SpannableStringBuilder(Html.toHtml(your_text));
handleOneParagraph(cleaned_text);

String Multiline - Android

i got this issue and i don't know how to solve it. Here is the problem:
1 - i have a data in my database who i split into a strings[] and then i split this strings[] into another 2 strings[] (even and odd lines). Everything works fine but when i want to join all the lines into a single String i got a multi line string intead of a single line. Someone can help me?
data
abcdef//
123456//
ghijkl//
789012
code:
String text = "";
vec1 = data.split("//"); //split the data
int LE = 0;
for (int a = 0; a < vec1.length; a++) { //verify how many even and odds line the data have
if (a % 2 == 0) { //if 0, LE++
LE++;
}
}
resul1 = new String[LE];
int contA = 0, contB = 0;
for (int c = 0; c < resul1.length; c++) {
if (c % 2 != 0) {
text += " " + resul1[c].toLowerCase().replace("Á","a").replace("Ã","a").replace("ã","a").replace("â","a").replace("á","a").replace("é","e").replace("É","e")
.replace("ê","e").replace("í","i").replace("Í","i").replace("ó","o").replace("Ó","o").replace("õ","o").replace("Õ","o").replace("ô","o").replace("Ô", "o")
.replace("Ú","u").replace("ú","u").replace("ç","c").replace("_","").replace("<","").replace(">","");
contA++;
}
}
And the String looks like
abcdef
ghijkl
instead of
abcdefghijkl
You should use replaceAll() method.
text.replaceAll("\\r\\n|\\r|\\n", ""); // the method removes all newline characters

How to add Comma between numbers

I have this code for my calculator:
public void onClick(View v) {
try {
double price = Double.parseDouble(InputPrice.getText()
.toString());
double percent = Double.parseDouble(InputPercent.getText()
.toString());
double priceValue = price * percent / 100.0f;
double percentValue = price - priceValue;
PriceToGet.setText(String.valueOf(priceValue));
PriceToPay.setText(String.valueOf(percentValue));
PriceToGet.setText(String.format("%.02f", priceValue));
PriceToPay.setText(String.format("%.02f", percentValue));
The Input and the Output are coming without commas like this:
Input: 333333333
Output: 134555.44
Output: 17475.66
This was just an example for Output and Input.
How do I like the user see them is:
Input: 333,333,333
Output: 134,555.44
Output: 17,475.66
Thanks
Update:
I added decimal in my onclick code:
DecimalFormat formatter = new DecimalFormat("#,###,###");
I used this code but its closing the App after I press the button:
String PriceToGet = formatter.format(String.format("%.02f", priceValue));
And when I am using this method:
String PriceToGet = formatter.format("%.02f", priceValue);
Its force me to change it to:
String PriceToGet = formatter.format(priceValue);
What to do?
You need to use DecimalFormat
You will find the complete answer here
This is how you can convert one of your integers to strings.
int x = 1000000;
DecimalFormat formatter = new DecimalFormat("#,###,###");
String number_string = formatter.format(x);
System.out.println(number_string);
// Outputs 1,000,000
This JS function from Css Tricks - http://css-tricks.com/snippets/javascript/comma-values-in-numbers/
function CommaFormatted(amount) {
var delimiter = ","; // replace comma if desired
var a = amount.split('.',2)
var d = a[1];
var i = parseInt(a[0]);
if(isNaN(i)) { return ''; }
var minus = '';
if(i < 0) { minus = '-'; }
i = Math.abs(i);
var n = new String(i);
var a = [];
while(n.length > 3) {
var nn = n.substr(n.length-3);
a.unshift(nn);
n = n.substr(0,n.length-3);
}
if(n.length > 0) { a.unshift(n); }
n = a.join(delimiter);
if(d.length < 1) { amount = n; }
else { amount = n + '.' + d; }
amount = minus + amount;
return amount;
}

Categories

Resources