Terser Coloring of a LogCat Message? - java

To speed up my debugging, I color certain messages for instant spotting, like this:
if (isOK)
Log.i(TAG, stringVarContentOfMessage);
else
Log.v(TAG, stringVarContentOfMessage);
It works, but viewing this source code over and over again, where the only justification for occupying 4 precious lines is one different character only (Log.i vs. Log.v) is an eyesore for me.
Any suggestions for avoiding this eyesore without resorting to the following?
isOK ? Log.i(TAG, stringVarContentOfMessage) : Log.v(TAG, stringVarContentOfMessage);

You can use Log.println():
Log.println(isOK ? Log.INFO : Log.VERBOSE, TAG, stringVarContentOfMessage);

Create a helper method:
private void conditionalLog(boolean flag, String tag, String message);

A better way is to color from the other end. Just color selected logcat messages. See: Modifying the Android logcat stream for full-color debugging That is a Python script that you can easily mod to your own pleasure.

Related

Convert Android view to PDF of A4 size

I have an android resume building application. I want to generate a PDF of size A4 from my view. Here's how my layout looks like - At the top I have a Top App Bar, and the whole view in encapsulated in drawer. The main part which contains user's details is encapsulated in nestedScrollView, which contains multiple LinearLayout and TextView. In this screenshot below, I have populated it with mock data, but in actuality, I am fetching data from the Firebase Realtime Database and displaying it on the UI.
I tried to understand iTextPdf solution and multiple question of similar type that has been asked here, but I couldn't find something solid. Please help me out, it would be of great help.
Also, please don't close this question by giving a reason that the question doesn't contain any code. It doesn't because I don't have any. I am trying to solve this problem from scratch. I have tried to describe my problem as much as I could.
try this:
create a WebView and copy the text of your edittext in it:
webview.loadData(youredittext.gettext().tostring, "text/html", "UTF-8");
and convert webview to pdf by below function:
private void createWebPrintJob(WebView webView) {
PrintManager printManager = (PrintManager) this
.getSystemService(Context.PRINT_SERVICE);
PrintDocumentAdapter printAdapter =
webView.createPrintDocumentAdapter();
String jobName = getString(R.string.app_name) + " Print Test";
if (printManager != null) {
printManager.print(jobName, printAdapter,
new PrintAttributes.Builder().build());
}
}
after that user can select page size for example A4
There are a lot of libraries that convert layouts to PDF, but let's opt for popular one so we could find answers if we're stuck.
The libraries I listed works like so : They take screenshot of your layout as bitmap image and convert it to pdf.
- 1st solution: iTextPDF https://github.com/itext/itext7 (New Version).
check this detailed tutorial which treates also the case of taking screenshot of a scrollview https://www.codeproject.com/Articles/989236/How-to-Convert-Android-View-to-PDF-2
and this stackoverflow answer https://stackoverflow.com/a/29731275/12802591
- 2nd solution: PdfMyXML library https://github.com/HendrixString/Android-PdfMyXml just follow the steps in the documentation.
They may be other solutions, but these are the popular ones.
Let Me know if it works for you and also if you're stuck. Thank you!

What command do I use to fix this problem?

My main goal is to create a program that can automate an android app to do everything you would with your fingers.
I wrote this line of code so that if that switch is turned ON, the code will switch it OFF.
driver.findElement(By.id("com.offerup:id/shipping_checkbox")).click();
But when I run my code again, that switch is now OFF by default.
I need a command that leaves the switch alone if it is already OFF.
Does anyone know what command to use?
You need to check whether the switch is ON or not. You could achieve this using the following code:
if(driver.findElement(By.id("com.offerup:id/shipping_checkbox")).isSelected()){
driver.findElement(By.id("com.offerup:id/shipping_checkbox")).click();
}
Updated:
As mentioned by Muzammil, isSelected() is not working as expected. Alternatively, you could use the checked attribute:
MobileElement shippingCheckbox= driver.findElement(By.id("com.offerup:id/shipping_checkbox"));
if(shippingCheckbox.getAttribute("checked").equalsIgnoreCase("true")){
shippingCheckbox.click();
}
There are 2 ways to check slider is on or off.
This is Node details of Slider.
Option-1: By using text
Here we will click only if slider is On.
MobileElement sliderElement=driver.findElement(By.id("com.offerup:id/shipping_checkbox"));
String sliderStatus = sliderElement.getText();
if (sliderStatus.equalsIgnoreCase("On")) {
sliderElement.click();
}
Option-2: By using Attribute
Here we will click only if slider is On.
MobileElement sliderElement=driver.findElement(By.id("com.offerup:id/shipping_checkbox"));
String sliderStatus=sliderElement.getAttribute("checked");
if (sliderStatus.equalsIgnoreCase("true")) {
sliderElement.click();
}
}
Edit: I made a mistake in reading this, and missed that it was an Android app, so Javascript is likely unavailable. This comment will only help someone with the same issue on web browser testing, or any situation where Javascript can be executed on the application.
Additional possibility for checking. Not that it is superior to other options.
using OpenQA.Selenium.Support.Extensions;
string selector = "com.offerup:id/shipping_checkbox";
if(driver.ExecuteJavascript<bool>($"return document.getElementById('{selector}').checked")) {
driver.findElement(By.id($"{selector}")).click();
}

Printing in android studio - folding sections

When I was printing java code in Eclipse, the way of printing was WYSIWYG. So if I folded some selected sections (for example import section, but also any other foldable section), it was printed as folded.
In Android Studio code is printed allways fully unfolded. Does anybody know some way (plugin or some preferences setting etc.), how to set it to print also in the folding WYSIWYG style?
My problem is that I have done the printing part in android device when the printer is already configured on the device.
If the printer is not configured on the device, the exception occurs can you tell that how u know that printer is configured on device
e1=(EditText) findViewById(R.id.editText);
String name=e1.getText().toString();
PrintManager printManager=(PrintManager) this.getSystemService(Context.PRINT_SERVICE);
//PrintDocumentAdapter printAdapter = view.createPrintDocumentAdapter();
//Toast.makeText(getApplicationContext(),"hi nikhil"+name+""+printManager.getPrintJobs(),Toast.LENGTH_LONG).show();
String text=this.getString(R.string.app_name)+"Document";
// printManager.getPrintJobs();
printManager.print(text, new MyPrintDocumentAdapter(this), null);
Log.d("print", "print" + printManager);
on Android-Studio 4.0, I noticed that you can select the parts of a file you wish to print, and in the print dialog box, it will allow you to just print the selection. This isn't exactly what you're looking for, but it is a way to bypass the long lines of import statements and comments at the head of a file.

How do you replace htmlspecialchars simply?

input.replace("&", "&").replace("<", "<").replace(">", ">").replace(""", "\"");
im using this line of code as a sudo htmlspecialchars converter. with my application, it doesnt need to be too elaborate.
for some reason the above code does not replace("&", "&"), it doesnt seem to do anything.
any advice?
input = input.replaceAll("[^\\x20-\\x7e]", "");
i also tried this.
In android there is one class Html inside android.text.Html which you can use like this as below:
Html.fromHtml(any_html_text);
But this function will return Spanned object so use
Spanned spn=Html.fromHtml(any_html_text);
You can set this spanned text to any button or textview text Hope it will help you.
See this link also for your reference.
Instead of doing this manually, check out the Html class. Calling Html.fromHtml() should do the trick.
Trying to escape/unescape html by replacing strings yourself you might introduce security issues in your application (depending on what it does with the output). Either use Html.FromHtml or from Apache libs org.apache.commons.lang.StringEscapeUtils.unescapeHtml
This does the work -
public String cleanString(String dirty) {
return Html.fromHtml(dirty).toString();
}

Android: IF statement not working when set by preferences

I am having trouble using and IF statement based on a string from my preferences.
Here is the code:
preferences = PreferenceManager.getDefaultSharedPreferences(this);
String themePref = preferences.getString("theme", "null");
Log.i("Theme", "Current theme is " + themePref );
if (themePref == "dark"){
setTheme(android.R.style.Theme_Black);
}else{
setTheme(android.R.style.Theme_Light);
}
I have two options at this point; light and dark. The code successfully sets themePref as light or dark as needed, and I've confirmed via debugging, but for some reason the if statement fails. I have manually set a string to "dark" and it then works correctly.
Am I missing something here? Why would a string coming from the preferences process any differently?
Any help would be greatly appreciated.
Thanks,
Josh
You should use themePref.equals("dark") (or even better "dark".equals(themePref)) to compare strings. == tells you if the string instances are the same, but two instances can have the same value and not be the same instance.
Is PreferenceManager.getDefaultSharedPreferences(this).getString(...) returning a java.lang.String instance or a subclass? I would replace themePref == "dark" with themePref.equals("dark") instead.

Categories