Tutorial by Examples: a

Gradle (Module:app) Configuration android { .... dataBinding { enabled = true } } Data model public class Item { public String name; public String description; public Item(String name, String description) { this.name = name; this.descr...
If your model has private methods, the databinding library still allows you to access them in your view without using the full name of the method. Data model public class Item { private String name; public String getName() { return name; } } Layout XML <?xml versi...
Data model public class Item { private String name; public String getName() { return name; } } Layout XML You must import referenced classes, just as you would in Java. <?xml version="1.0" encoding="utf-8"?> <layout xmlns:android="h...
From the documentation: The exception that is thrown when an application attempts to perform a networking operation on its main thread. This is only thrown for applications targeting the Honeycomb SDK or higher. Applications targeting earlier SDK versions are allowed to do networking on their ma...
In order to compare Strings for equality, you should use the String object's equals or equalsIgnoreCase methods. For example, the following snippet will determine if the two instances of String are equal on all characters: String firstString = "Test123"; String secondString = "Test...
/** * Interface with default method */ public interface Printable { default void printString() { System.out.println( "default implementation" ); } } /** * Class which falls back to default implementation of {@link #printString()} */ public class WithDefault...
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);
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"...
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...

Page 15 of 1099