Tutorial by Examples: e

You can as well access other interface methods from within your default method. public interface Summable { int getA(); int getB(); default int calculateSum() { return getA() + getB(); } } public class Sum implements Summable { @Override public int get...
Java SE 7 Java 7 introduced the Diamond1 to remove some boiler-plate around generic class instantiation. With Java 7+ you can write: List<String> list = new LinkedList<>(); Where you had to write in previous versions, this: List<String> list = new LinkedList<String>()...
class TrivialClass {} A class consists at a minimum of the class keyword, a name, and a body, which might be empty. You instantiate a class with the new operator. TrivialClass tc = new TrivialClass();
package com.example; import android.os.Bundle; import android.support.annotation.Nullable; import android.util.Log; import android.view.View; import android.view.ViewTreeObserver; public class ExampleActivity extends Activity { @Override protected void onCreate(@Nullable final ...
In Android Activities and Services, most callbacks are run on the main thread. This makes it simple to update the UI, but running processor- or I/O-heavy tasks on the main thread can cause your UI to pause and become unresponsive (official documentation on what then happens). You can remedy this by...
// Compile a Uri with the 'mailto' schema Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts( "mailto","[email protected]", null)); // Subject emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Hello World!"); // Body of email emailIntent.putEx...
If you need to produce a JSON string with a value of null like this: { "name":null } Then you have to use the special constant JSONObject.NULL. Functioning example: jsonObject.put("name", JSONObject.NULL);
if (i < 2) { System.out.println("i is less than 2"); } else if (i > 2) { System.out.println("i is more than 2"); } else { System.out.println("i is not less than 2, and not more than 2"); } The if block will only run when i is 1 or less. The else if...
int i = 0; while (i < 100) { // condition gets checked BEFORE the loop body executes System.out.println(i); i++; } A while loop runs as long as the condition inside the parentheses is true. This is also called the "pre-test loop" structure because the conditional stateme...
The do...while loop differs from other loops in that it is guaranteed to execute at least once. It is also called the "post-test loop" structure because the conditional statement is performed after the main loop body. int i = 0; do { i++; System.out.println(i); } while (i &l...
Java SE 5 With Java 5 and up, one can use for-each loops, also known as enhanced for-loops: List strings = new ArrayList(); strings.add("This"); strings.add("is"); strings.add("a for-each loop"); for (String string : strings) { System.out.println(string); } For each ...
public class MyActivity extends Activity { private static final String PREFS_FILE = "NameOfYourPrefrenceFile"; // PREFS_MODE defines which apps can access the file private static final int PREFS_MODE = Context.MODE_PRIVATE; // you can use live template "key"...
int i = 2; if (i < 2) { System.out.println("i is less than 2"); } else { System.out.println("i is greater than 2"); } An if statement executes code conditionally depending on the result of the condition in parentheses. When condition in parentheses is true it will ...
With this class: class ObjectMemberVsStaticMember { static int staticCounter = 0; int memberCounter = 0; void increment() { staticCounter ++; memberCounter++; } } the following code snippet: final ObjectMemberVsStaticMember o1 = new ObjectMemberVsStati...
Android Studio is the Android development IDE that is officially supported and recommended by Google. Android Studio comes bundled with the Android SDK Manager, which is a tool to download the Android SDK components required to start developing apps. Installing Android Studio and Android SDK tools:...
It is helpful to filter the logcat output because there are many messages which are not of interest. To filter the output, open the "Android Monitor" and click on the drop down on the top-right and select Edit Filter Configuration Now you can add custom filters to show messages which ar...
If you want to take a screenshot from the Android Emulator (2.0), then you just need to press Ctrl + S or you click on the camera icon on the side bar: If you use an older version of the Android Emulator or you want to take a screenshot from a real device, then you need to click on the camera ico...
If you have many tasks to execute, and all these tasks are not dependent of the result of the precedent ones, you can use Multithreading for your computer to do all this tasks at the same time using more processors if your computer can. This can make your program execution faster if you have some bi...
Once the SDK installed, you can open the AVD Manager from the command line using android avd. You can also access AVD Manager from Android studio using Tools > Android > AVD Manager or by clicking on the AVD Manager icon in the toolbar which is the second in the screenshot below.
private static final String MY_PREF = "MyPref"; // ... SharedPreferences prefs = ...; // ... SharedPreferences.Editor editor = prefs.edit(); editor.putString(MY_PREF, "value"); editor.remove(MY_PREF); editor.apply(); After the apply(), prefs contains "key&q...

Page 16 of 1191