Tutorial by Examples: e

RelativeLayout is a ViewGroup that displays child views in relative positions. By default, all child views are drawn at the top-left of the layout, so you must define the position of each view using the various layout properties available from RelativeLayout.LayoutParams. The value for each layout...
This is an example of a default build.gradle file in a module. apply plugin: 'com.android.application' android { compileSdkVersion 25 buildToolsVersion '25.0.3' signingConfigs { applicationName { keyAlias 'applicationName' keyPassword 'password...
public class SampleAdapter extends RecyclerView.Adapter<SampleAdapter.ViewHolder> { private String[] mDataSet; private OnRVItemClickListener mListener; /** * Provide a reference to the type of views that you are using (custom ViewHolder) */ public static cla...
To use a NavigationView just add the dependency in the build.gradle file as described in the remarks section Then add the NavigationView in the layout <?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.DrawerLayout xmlns:android="http://schema...
The following example shows the basics of throwing an exception: public void checkNumber(int number) throws IllegalArgumentException { if (number < 0) { throw new IllegalArgumentException("Number must be positive: " + number); } } The exception is thrown on the 3...
An abstract class is a class marked with the abstract keyword. It, contrary to non-abstract class, may contain abstract - implementation-less - methods. It is, however, valid to create an abstract class without abstract methods. An abstract class cannot be instantiated. It can be sub-classed (exten...
Product flavors are defined in the build.gradle file inside the android { ... } block as seen below. ... android { ... productFlavors { free { applicationId "com.example.app.free" versionName "1.0-free" } paid { ...
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...
A Java class can implement multiple interfaces. public interface NoiseMaker { String noise = "Making Noise"; // interface variables are public static final by default String makeNoise(); //interface methods are public abstract by default } public interface FoodEater { ...
This example illustrates sending a String with value as "Some data!" from OriginActivity to DestinationActivity. NOTE: This is the most straightforward way of sending data between two activities. See the example on using the starter pattern for a more robust implementation. OriginActivit...
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...
Addition Map<Integer, String> map = new HashMap<>(); map.put(1, "First element."); System.out.println(map.get(1)); Output: First element. Override Map<Integer, String> map = new HashMap<>(); map.put(1, "First element."); map.put(1, &quot...
We can use V put(K key,V value): Associates the specified value with the specified key in this map (optional operation). If the map previously contained a mapping for the key, the old value is replaced by the specified value. String currentVal; Map<Integer, String> map = new TreeMap&l...
Examples of using Default Methods introduced in Java 8 in Map interface Using getOrDefault Returns the value mapped to the key, or if the key is not present, returns the default value Map<Integer, String> map = new HashMap<>(); map.put(1, "First element"); map.get(1); ...
Under most circumstances, it is simpler from a code-design standpoint to use existing generic Exception classes when throwing exceptions. This is especially true if you only need the exception to carry a simple error message. In that case, RuntimeException is usually preferred, since it is not a che...
Map<Integer, String> map = new HashMap<>(); map.put(1, "First element."); map.put(2, "Second element."); map.put(3, "Third element."); map.clear(); System.out.println(map.size()); // => 0
Consider the following JSON string: { "title": "test", "content": "Hello World!!!", "year": 2016, "names" : [ "Hannah", "David", "Steve" ] } This JSON object can...
Create the JSONObject using the empty constructor and add fields using the put() method, which is overloaded so that it can be used with different types: try { // Create a new instance of a JSONObject final JSONObject object = new JSONObject(); // With put you can add a name/va...

Page 14 of 1191