I have used refactor for copying, moving and renaming but I want to expand my horizon for feature use so I wanted to know what Extract feature does and how it helps to increase productivity and code cleanliness.
"Extract Method" can help you reduce the size of your methods. For example,
public static void main(String[] args) {
System.out.println("Hello!");
System.out.println("My name is Sweeper!");
System.out.println("I write code in Java.");
}
If I select the three lines of System.out.println, and do "Extract Method", and give it a name of selfIntro, I get the following:
public static void main(String[] args) {
selfIntro();
}
private static void selfIntro() {
System.out.println("Hello!");
System.out.println("My name is Sweeper!");
System.out.println("I write code in Java.");
}
Related
This question already has answers here:
How to remove all debug logging calls before building the release version of an Android app?
(31 answers)
Closed 7 years ago.
so.. I'm using Log.d("", ""); in many places in my app for debuging.
but I don't want those logs in the store.
right now in order to hide them in the store version, I created a Constant.class and put a boolean there called debugMode, and wrapped every log i have in an if statement like this :
if (Constant.debugMode) {
Log.d(TAG, "check123");
}
and then when I build a google store apk I change that boolean to true instead of false.
that's kind of clumsy in my opinion, is there any more efficient way to do that?
Make a simple logger class that has public static methods and a swich to enable logs only for debug versions of your app. Here is a sample code.
public class Logger {
public static final boolean D = BuildConfig.DEBUG;
public static void LogInfo(String TAG, String msg) {
if (D) {
Log.i(TAG, msg);
}
}
public static void LogWarrning(String TAG, String msg) {
if (D) {
Log.w(TAG, msg);
}
}
public static void LogDebug(String TAG, String msg) {
if (D) {
Log.d(TAG, msg);
}
}
public static void LogError(String TAG, String msg) {
if (D) {
Log.e(TAG, msg);
}
}
}
Usage
Logger.LogDebug("Test", "Example");
This way you can keep the if clauses in one place and don't have to worry about them. Also I don't think it clumsy this way.
I find a far easier solution is to forget all the if checks all over
the place and just use ProGuard to strip out any Log.d() or Log.v()
method calls when we call our Ant release target.
That way, we always have the debug info being output for regular
builds and don't have to make any code changes for release builds.
ProGuard can also do multiple passes over the bytecode to remove other
undesired statements, empty blocks and can automatically inline short
methods where appropriate.
For example, here's a very basic ProGuard config for Android:
-dontskipnonpubliclibraryclasses
-dontobfuscate
-forceprocessing
-optimizationpasses 5
-keep class * extends android.app.Activity
-assumenosideeffects class android.util.Log {
public static * d(...);
public static * v(...); } So you would save that to a file, then call ProGuard from Ant, passing in your just-compiled JAR and the
Android platform JAR you're using.
#REF:Remove all debug logging calls before publishing: are there tools to do this?
I want to show a simple java program in my app
when i put the below code in my textview it simply shows it like a paragraph.
How to show a program in my textview like the format given below
public class Main {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Help me :-) please
I dont have any idea how you are trying but refer my answer i think you will get some idea
String firststring = "public class Main {";
String secondstring = "public static void main(String[] args) {";
String thirdstring = "System.out.println("Hello, World!");";
textview.setText(Html.fromHtml(firststring + "<br>" + secondstring+"<br>"+thirdstring));//Your textview
I finally got the answer.
Place texts which you want to look like a program in the Syntax highlighter save it as html and put it on assets folder load the url in webview!
That's what I found on matter:
Syntax Highlighter Link
This question already has answers here:
How to filter Android logcat by application? [duplicate]
(27 answers)
Closed 9 years ago.
On IntelliJ, i'm trying to read the logcat for seeking errors.
The problem is that all the applications' log are present in the 'Android' Window.
How to only display the log that is relevant ?
I'm not looking for tags since i want to view Exception throws, Segfaults from JNI, etc.
Thanks !
Filtering based on an application (package) is not available in IntelliJ IDEA (current version: 12).
EDIT: Everything above the horizontal rule is my new answer (updated 5/17/13):
I'd suggest moving over to the new Android Studio IDE (which is based of IntelliJ). This allows filtering by package name.
You can vote for the feature to be added here: http://youtrack.jetbrains.com/issue/IDEA-95780
In the meantime, you may think about wrapping the Android Log class to add a suffix/prefix that you could then filter by. Something like the following:
/**
* Wrapper for the Android {#link Log} class.
* <br><br>
* The primary reason for doing this is to add a unique prefix to all tags for easier filtering
* in IntelliJ IDEA (since in v12 there is no way to filter by application).
*
* #author gloesch
*/
public class Logger {
private static final String FILTER = "(MY APP) ";
public static void d(String tag, String message) {
Log.d(tag.concat(FILTER), message);
}
public static void i(String tag, String message) {
Log.i(tag.concat(FILTER), message);
}
public static void v(String tag, String message) {
Log.v(tag.concat(FILTER), message);
}
public static void e(String tag, String message) {
Log.e(tag.concat(FILTER), message);
}
public static void w(String tag, String message) {
Log.w(tag.concat(FILTER), message);
}
public static void wtf(String tag, String message) {
Log.wtf(tag.concat(FILTER), message);
}
}
You can filter by the process ID (PID):
The only drawback is that PID changes and you will have to adjust the filter after every app restart.
Depends on what you're trying to do. In the logcat window, you should have an [non]empty window named "Filters" with a + or - next to it (as it appears in IntelliJ IDEA 12) and it'll pop up a window like this:
If the Log Tag isn't what you need, you can filter based on a regexp in the log message itself (e.g. if the message always starts with seek error, then input seek and it should find it. If you set Log Level to Verbose it should match anything that gets written to logcat.
I am not sure I did the right thing. The main reason for my doubts is that I cannot find, in this or other forums, someone who has done a similar thing.
I created an abstract java class in my project. Named it lib. I put there several structures and methods used by all other classes in the project.
It works for me, but I want to know if there is a more accepted method of gathering all common methods and structures.
Note: All methods of course are declared as public static.
Note II: I did not know how to get the context within the abstract class, so if needed I had to pass it as argument to the method.
Is this wat you are looking for?
public abstract class AbstractActivity extends Activity{
public static synchronized boolean showAlertBox(Context ctx,final String title,final String message,final String okBtnTxt,final OnClickListener clickListener){
AlertDialog.Builder alertbox; alertbox = new AlertDialog.Builder(ctx);
this.runOnUiThread(new Runnable() {
#Override
public void run() {
alertbox.setTitle(title);
alertbox.setMessage(message);
if(okBtnTxt!=null || clickListener!=null)
alertbox.setNeutralButton(okBtnTxt,clickListener);
alertbox.show();
.....
}
});
return true;
}
}
In the class extending this abstract class you can just call it by using showAlertBox(this);
Other wise use AbstractActivity.showAlertBox(Context);
Well, thanks to #Matt Wolfe's comment I came to know that what I did is called "Utility class" and it is widely used to share common code in a project.
The general template is:
public abstract class lib {
public static final int ZERO = 0;
public static final int ONE = 1;
public static final int TWO = 2;
public static void func1(int i) {
}
public static void func2(int i, String s) {
}
}
and you can use it like this from any other code:
...;
lib.func1( lib.ZERO );
lib func2( lib.TWO, "sandwich" );
...;
Knowing that makes me confident that what I did is OK.
It would be perfect to find a way to avoid the prefix lib. and just have ECLIPSE, and the compiler, find the right import and recognize the function with just its name, like they do for global libraries.
How can we create a verbose/logging mode in the app which when switched on will print logging statements from the app and on switching it off no log statements will be printed on the console? One way is to create a preferences option and do a ton of if and else, which does not sound very good. Is there a standard way to do it in Android?
I've never done this myself but check here : http://developer.android.com/reference/android/util/Log.html
Under the isLoggable function it mentions how to enable/disable logging for different types (ERROR, INFO, VERBOSE, etc).
That is, if you're okay having this setting in one file and changing that when you need to.
I created a simple Class to do my logs, so I can easily switch it off. I'm sure there's plenty of rooms for improvements, but it works :)
So use this Log Class instead of the android one:
public class Log {
private static String tag = "YOUR_LOG_TAG";
public static void d(String... params){
for(String m : params){
android.util.Log.d(tag, m);
}
}
public static void d(int m){
android.util.Log.d(tag, String.valueOf(m));
}
public static void d(String iTag, String m){
android.util.Log.d(iTag, m);
}
public static void e(String iTag, String m, Throwable t){
android.util.Log.e(iTag, m, t);
}
public static void e(String m, Throwable t){
android.util.Log.e(tag, m, t);
}
public static void e(String iTag, String m){
android.util.Log.e(iTag, m);
}
}
I agree with #nmjohn that using the android logger is the easiest way to do logging. Together with the Eclipse-adt-plugin-view "LogCat" you can decide at runtime, what you want to see from the logging when your device is connected to development-pc via usb.
If you want to write portable busineslogic you can use the Simple Logging Facade for Java(SLF4J). There is already a "do nothig" implementatin and a implementation that uses the android Log mechanism.
If you need a special kind of logging you can easily create your own SLF4J-implementation that can write to a disk file or do something else.
As far as i know the java standard logger log4j does not work for android because of missing dependencies and a memory footprint that is to big for android.