Tutorial by Examples: di

We can create an ArrayList (following the List interface): List aListOfFruits = new ArrayList(); Java SE 5 List<String> aListOfFruits = new ArrayList<String>(); Java SE 7 List<String> aListOfFruits = new ArrayList<>(); Now, use the method add to add a String: ...
Dependencies can be added for a specific product flavor, similar to how they can be added for specific build configurations. For this example, assume that we have already defined two product flavors called free and paid (more on defining flavors here). We can then add the AdMob dependency for the ...
Resources can be added for a specific product flavor. For this example, assume that we have already defined two product flavors called free and paid. In order to add product flavor-specific resources, we create additional resource folders alongside the main/res folder, which we can then add resourc...
Basic cases int[] numbers1 = new int[3]; // Array for 3 int values, default value is 0 int[] numbers2 = { 1, 2, 3 }; // Array literal of 3 int values int[] numbers3 = new int[] { 1, 2, 3 }; // Array of 3 int values initialized int[][] numbers4 = { { 1, 2...
An interface can extend another interface via the extends keyword. public interface BasicResourceService { Resource getResource(); } public interface ExtendedResourceService extends BasicResourceService { void updateResource(Resource resource); } Now a class implementing ExtendedR...
Java 7 introduced the very useful Files class Java SE 7 import java.nio.file.Files; import java.nio.file.Paths; import java.nio.file.Path; Path path = Paths.get("path/to/file"); try { byte[] data = Files.readAllBytes(path); } catch(IOException e) { e.printStackTrace();...
Dimensions are typically stored in a resource file names dimens.xml. They are defined using a <dimen> element. res/values/dimens.xml <?xml version="1.0" encoding="utf-8"?> <resources> <dimen name="small_padding">5dp</dimen> &l...
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...
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>()...
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 ...
// 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...
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:...
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...
A Toolbar is a generalization of ActionBar for use within application layouts. While an ActionBar is traditionally part of an Activity's opaque window decor controlled by the framework, a Toolbar may be placed at any arbitrary level of nesting within a view hierarchy. It can be added by performing t...
Using BufferedReader: System.out.println("Please type your name and press Enter."); BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); try { String name = reader.readLine(); System.out.println("Hello, " + name + "!"); } catch(I...
It is possible to define an array with more than one dimension. Instead of being accessed by providing a single index, a multidimensional array is accessed by specifying an index for each dimension. The declaration of multidimensional array can be done by adding [] for each dimension to a regular a...
Updating the state of a Swing component must happen on the Event Dispatch Thread (the EDT). The javax.swing.Timer triggers its ActionListener on the EDT, making it a good choice to perform Swing operations. The following example updates the text of a JLabel each two seconds: //Use a timer to updat...
To check whether a particular String a is being contained in a String b or not, we can use the method String.contains() with the following syntax: b.contains(a); // Return true if a is contained in b, false otherwise The String.contains() method can be used to verify if a CharSequence can be fou...
As you use generic types with utility classes, you may often find that number types aren't very helpful when specified as the object types, as they aren't equal to their primitive counterparts. List<Integer> ints = new ArrayList<Integer>(); Java SE 7 List<Integer> ints = new A...

Page 3 of 164