Tutorial by Examples: d

android:layout_gravity android:layout_gravity is used to set the position of an element in its parent (e.g. a child View inside a Layout). Supported by LinearLayout and FrameLayout android:gravity android:gravity is used to set the position of content inside an element (e.g. a text insid...
GridLayout, as the name suggests is a layout used to arrange Views in a grid. A GridLayout divides itself into columns and rows. As you can see in the example below, the amount of columns and/or rows is specified by the properties columnCount and rowCount. Adding Views to this layout will add the fi...
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...
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); ...
// Create a new instance of a JSONArray JSONArray array = new JSONArray(); // With put() you can add a value to the array. array.put("ASDF"); array.put("QWERTY"); // Create a new instance of a JSONObject JSONObject obj = new JSONObject(); try { // Add the JSONAr...
First example reimplemented in Kotlin and using RxJava for cleaner interaction. import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.support.v7.widget.RecyclerView import rx.subjects.PublishSubject public class SampleAdapter(private val ite...
Methods can also have generic type parameters. public class Example { // The type parameter T is scoped to the method // and is independent of type parameters of other methods. public <T> List<T> makeList(T t1, T t2) { List<T> result = new ArrayList<T&...
When we create a button in layout, we can use the android:onClick attribute to reference a method in code to handle clicks. Button <Button android:width="120dp" android:height="wrap_content" android:text="Click me" android:onClick="handleCl...
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();...
Strings are typically stored in the resource file strings.xml. They are defined using a <string> XML element. The purpose of strings.xml is to allow internationalisation. You can define a strings.xml for each language iso code. Thus when the system looks for the string 'app_name' it first che...
In order to define a string array write in a resources file res/values/filename.xml <string-array name="string_array_name"> <item>text_string</item> <item>@string/string_id</item> </string-array> for example res/values/arrays.xml <?x...
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...
Integers are typically stored in a resource file named integers.xml, but the file name can be chosen arbitrarily. Each integer is defined by using an <integer> element, as shown in the following file: res/values/integers.xml <?xml version="1.0" encoding="utf-8"?> &...

Page 8 of 691