Create Android Device Network Listener for Network On/Off Events

Azhar
Updated on 07-Jul-2020 12:43:03

460 Views

This example demonstrates how do I create Android Device Network Listener based on on/off network.Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.Step 2 − Add the following code to res/layout/activity_main.xml

Get OnClick Event on WebView in Android

Azhar
Updated on 07-Jul-2020 12:41:36

5K+ Views

This example demonstrates how do I get onClick event on webView in android.Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.Step 2 − Add the following code to res/layout/activity_main.xml. Step 3 − Add the following code to src/MainActivity.javaimport androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.MotionEvent; import android.view.View; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.Toast; public class MainActivity extends AppCompatActivity implements View.OnTouchListener, Handler.Callback {    private static final int CLICK_ON_WEBVIEW = 1;    private static final int CLICK_ON_URL = 2;   ... Read More

Play Videos in Android from Assets or Raw Folder

Azhar
Updated on 07-Jul-2020 12:41:01

5K+ Views

This example demonstrates how do I play videos in Android from the assets folder or raw folder in android.Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.Step 2 − Add the following code to res/layout/activity_main.xml. Step 3 − Create a new android resource folder(raw) and copy-paste your video file in that folder.Step 4 − Add the following code to src/MainActivity.javaimport androidx.appcompat.app.AppCompatActivity; import android.net.Uri; import android.os.Bundle; import android.widget.MediaController; import android.widget.VideoView; public class MainActivity extends AppCompatActivity {    @Override    protected void ... Read More

Implement Custom FieldNamingStrategy Using Gson in Java

raja
Updated on 07-Jul-2020 12:39:14

1K+ Views

The FieldNamingStrategy is a mechanism for providing custom field naming in Gson. This allows the client code to translate field names into a particular convention that is not supported as a normal Java field declaration rules. The translateName() method will prefix every field name with the string “pre_”.In the below example, we can implement the Custom FieldNamingStrategy.Exampleimport java.lang.reflect.Field; import com.google.gson.*; public class GsonFieldNamingStrategyTest {    public static void main(String[] args) {       Employee emp = new Employee();       emp.setEmpId(115);       emp.setFirstName("Adithya");       emp.setLastName("Jai");       CustomFieldNamingStrategy customFieldNamingStrategy = new CustomFieldNamingStrategy();     ... Read More

Get JSONParser Default Settings Using Jackson in Java

raja
Updated on 07-Jul-2020 12:18:20

484 Views

All the default settings of JSON Parser can be represented using the JsonParser.Feature enumeration. The JsonParser.Feature.values() will return all the features that are available for JSONParser but whether a feature is enabled or disabled for a particular parser can be determined using the isEnabled() method of JsonParser. Syntaxpublic static enum JsonParser.Feature extends EnumExampleimport com.fasterxml.jackson.core.*; import java.io.*; public class JsonParserSettingsTest {    public static void main(String[] args) throws IOException {       String json = "[{\"name\":\"Adithya\", \"age\":\"30\"}, " + "{\"name\":\"Ravi\", \"age\":\"35\"}]";       JsonFactory jsonFactory = new JsonFactory();       JsonParser jsonParser = jsonFactory.createParser(json);       for(JsonParser.Feature feature : JsonParser.Feature.values()) {       ... Read More

Get JSONFactory Settings Using Jackson in Java

raja
Updated on 07-Jul-2020 12:12:33

1K+ Views

The JsonFactory class is a thread-safe and responsible for creating instances of writer and reader. The list of settings that can be turned on/off is present in an enumeration JsonFactory.Feature, it contains static method values() that return the enum constant of this type with the specified name.Syntaxpublic static enum JsonFactory.Feature extends EnumExampleimport com.fasterxml.jackson.core.JsonFactory; public class JsonFactorySettingsTest {    public static void main(String[] args) {       JsonFactory jsonFactory = new JsonFactory();       for(JsonFactory.Feature feature : JsonFactory.Feature.values()) {          boolean result = jsonFactory.isEnabled(feature);          System.out.println(feature.name() + ":" + result);       }    } }OutputINTERN_FIELD_NAMES:true ... Read More

Get JSONGenerator Settings Using Jackson in Java

raja
Updated on 07-Jul-2020 12:05:39

401 Views

The JsonGenerator class can be responsible for writing JSON data as a stream instead of constructing an Object Model in memory. The list of settings that can be turned on/off is present in an enum JsonGenerator.Feature, it contains static method values() that returns an array containing the constants of this enum type.Syntaxpublic static enum JsonGenerator.Feature extends EnumExampleimport java.io.*; import com.fasterxml.jackson.core.*; public class JsonGeneratorSettingsTest {    public static void main(String[] args) throws IOException {       StringWriter writer = new StringWriter();       JsonFactory jsonFactory = new JsonFactory();       JsonGenerator jsonGenerator = jsonFactory.createGenerator(writer);       for(JsonGenerator.Feature feature : JsonGenerator.Feature.values()) {     ... Read More

FieldNamingPolicy Enum Using Gson in Java

raja
Updated on 07-Jul-2020 12:04:50

1K+ Views

Gson library provides the naming conventions as part of enum FieldNamingPolicy. We can set the field naming policy using the setFieldNamingPolicy() method of the GsonBuilder class.FieldNamingPolicy enum ConstantsIDENTITY − Using this naming policy, the field name is unchanged.LOWER_CASE_WITH_DASHES − Using this naming policy, modify the Java Field name from its camel-cased form to a lower case field name where each word is separated by a dash (-).LOWER_CASE_WITH_UNDERSCORES − Using this naming policy, modify the Java Field name from its camel-cased form to a lower case field name where each word is separated by an underscore (_).UPPER_CAMEL_CASE − Using this naming policy, ... Read More

Get Values of a Key Using JSONPointer Interface in Java

raja
Updated on 07-Jul-2020 11:58:26

791 Views

The JSONPointer is a standard that defines a string syntax that can be used to access a particular key value in the JSON document. An instance of JSONPointer can be created by calling the static factory method createPointer() on the Json class. In the JSONPointer,  every string syntax is prefixed with “/”. We can get the value of a key by calling the getValue() method on the JsonPointer object.JSON fileExampleimport javax.json.*; import java.io.*; public class JsonPointerTest {    public static void main(String[] args) throws Exception {       JsonReader jsonReader = Json.createReader(new FileReader("simple.json"));       JsonStructure jsonStructure = jsonReader.read();       JsonPointer jsonPointer1 ... Read More

Importance of the JsonPatch Interface in Java

raja
Updated on 07-Jul-2020 11:55:50

641 Views

The JsonPatch interface is a format for storing a sequence of operations that can be applied to the target JSON structure. There are few operations like add, remove, replace, copy, move and test can be stored in JsonPath and operated on JSON structure. The JsonPatchBuilder interface can be used for constructing a JSON patch using the Json.createPatchBuilder().JSON fileExampleimport java.io.*; import javax.json.Json; import javax.json.JsonPatch; import javax.json.JsonPatchBuilder; import javax.json.JsonReader; import javax.json.JsonStructure; public class JsonPatchTest {    public static void main(String[] args) throws Exception {       JsonPatchBuilder jsonPatchBuilder = Json.createPatchBuilder();       JsonPatch jsonPatch = jsonPatchBuilder.add("/postalCode", "500072").remove("/age").build();       JsonReader reader = Json.createReader(new FileReader("simple.json"));     ... Read More

Advertisements