Tutorial by Examples

One use of SharedPreferences is to implement a "Settings" screen in your app, where the user can set their preferences / options. Like this: A PreferenceScreen saves user preferences in SharedPreferences. To create a PreferenceScreen, you need a few things: An XML file to define the av...
Scanner scanner = new Scanner(System.in); //Scanner obj to read System input String inputTaken = new String(); while (true) { String input = scanner.nextLine(); // reading one line of input if (input.matches("\\s+")) // if it matches spaces/tabs, stop reading b...
Scanner scanner = null; try { scanner = new Scanner(new File("Names.txt")); while (scanner.hasNext()) { System.out.println(scanner.nextLine()); } } catch (Exception e) { System.err.println("Exception occurred!"); } finally { if (scanner != nul...
Sometimes the same functionality has to be written for different kinds of inputs. At that time, one can use the same method name with a different set of parameters. Each different set of parameters is known as a method signature. As seen per the example, a single method can have multiple signatures....
Needed imports: import javax.sound.sampled.AudioSystem; import javax.sound.sampled.Clip; This code will create a clip and play it continuously once started: Clip clip = AudioSystem.getClip(); clip.open(AudioSystem.getAudioInputStream(new URL(filename))); clip.start(); clip.loop(Clip.LOOP_CO...
Here we have a class Counter with methods countNumbers() and hasNumbers(). public class Counter { /* To count the numbers in the input */ public static int countNumbers(String input) { int count = 0; for (char letter : input.toCharArray()) { if (Character....
The switch statement is Java's multi-way branch statement. It is used to take the place of long if-else if-else chains, and make them more readable. However, unlike if statements, one may not use inequalities; each value must be concretely defined. There are three critical components to the switch...
Paginations in JavaFX use a callback to get the pages used in the animation. Pagination p = new Pagination(); p.setPageFactory(param -> new Button(param.toString())); This creates an infinite list of buttons numbed 0.. since the zero arg constructor creates an infinite pagination. setPageFac...
Pagination p = new Pagination(10); Timeline fiveSecondsWonder = new Timeline(new KeyFrame(Duration.seconds(5), event -> { int pos = (p.getCurrentPageIndex()+1) % p.getPageCount(); p.setCurrentPageIndex(pos); })); fiveSecondsWonder.setCycleCount(Timeline.INDEFINITE); fiveSecondsWon...
ArrayList<String> images = new ArrayList<>(); images.add("some\\cool\\image"); images.add("some\\other\\cool\\image"); images.add("some\\cooler\\image"); Pagination p = new Pagination(3); p.setPageFactory(n -> new ImageView(images.get(n))); Note...
wait() and notify() work in tandem – when one thread calls wait() on an object, that thread will block until another thread calls notify() or notifyAll() on that same object. (See Also: wait()/notify() ) package com.example.examples.object; import java.util.concurrent.atomic.AtomicBoolean; p...
The Collections class offers two standard static methods to sort a list: sort(List<T> list) applicable to lists where T extends Comparable<? super T>, and sort(List<T> list, Comparator<? super T> c) applicable to lists of any type. Applying the former requires amending...
MIDI files can be played by using several classes from the javax.sound.midi package. A Sequencer performs playback of the MIDI file, and many of its methods can be used to set playback controls such as loop count, tempo, track muting, and others. General playback of MIDI data can be done in this wa...
In your build.gradle file of main module(app), define your minimum and target version number. android { //the version of sdk source used to compile your project compileSdkVersion 23 defaultConfig { //the minimum sdk version required by device to run your app minSd...
Colors are usually stored in a resource file named colors.xml in the /res/values/ folder. They are defined by <color> elements: <?xml version="1.0" encoding="utf-8"?> <resources> <color name="colorPrimary">#3F51B5</color> <c...
A simple example of producer-consumer problem solution. Notice that JDK classes (AtomicBoolean and BlockingQueue) are used for synchronization, which reduces the chance of creating an invalid solution. Consult Javadoc for various types of BlockingQueue; choosing different implementation may drastica...
Java's Reflection API allows the programmer to perform various checks and operations on class fields, methods and annotations during runtime. However, in order for an annotation to be at all visible at runtime, the RetentionPolicy must be changed to RUNTIME, as demonstrated in the example below: @i...
First of all for a JAX-RS application must be set a base URI from which all the resources will be available. For that purpose the javax.ws.rs.core.Application class must be extended and annotated with the javax.ws.rs.ApplicationPath annotation. The annotation accepts a string argument which defines...
Date date = new Date(); System.out.println(date); // Thu Feb 25 05:03:59 IST 2016 Here this Date object contains the current date and time when this object was created. Calendar calendar = Calendar.getInstance(); calendar.set(90, Calendar.DECEMBER, 11); Date myBirthDate = calendar.getTime(); ...
Calendar, Date, and LocalDate Java SE 8 before, after, compareTo and equals methods //Use of Calendar and Date objects final Date today = new Date(); final Calendar calendar = Calendar.getInstance(); calendar.set(1990, Calendar.NOVEMBER, 1, 0, 0, 0); Date birthdate = calendar.getTime(); ...

Page 23 of 1336