mkyong

mkyong

Software Engineer • Writer • Dad

20+ years hands-on, writing about Java, Spring, and AI. Every code example here is tested.

1,960 posts

Posts by mkyong

This article shows you how to use try-with-resources in Java. Table of contents: 1 Java try-with-resources before and after 2. try-with-resources with single resource 3. try-with-resources with multiple resources 3.1 JDBC example. 4. Custom resource with AutoCloseable interface 5. Resource open and closing order 6. Java 9 – final or effectively final variables Download Source […]

Read more Java try-with-resources example

In Java, we use FileInputStream to read bytes from a file, such as an image file or binary file. Topics FileInputStream – Read a file FileInputStream – Remaining bytes FileInputStream – Better performance FileInputStream vs BufferedInputStream InputStreamReader – Convert FileInputStream to Reader FileInputStream – Read a Unicode file Note However, all the below examples use […]

Read more How to read file in Java – FileInputStream

In Android, ListView let you arranges components in a vertical scrollable list. In this tutorial, we will show you 2 ListView examples : Normal way to display components in ListView. Custom array adapter to customize the item display in ListView. P.S This project is developed in Eclipse 3.7, and tested with Android 2.3.3. 1. Normal […]

Read more Android ListView example

In Android, you can configure the starting activity (default activity) of your application via following “intent-filter” in “AndroidManifest.xml“. See following code snippet to configure a activity class “logoActivity” as the default activity. File : AndroidManifest.xml <activity android:label="Logo" android:name=".logoActivity" > <intent-filter > <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> For example, let said you have […]

Read more How to set default activity for Android application

In Android, an activity is represent a single screen. Most applications have multiple activities to represent different screens, for example, one activity to display a list of the application settings, another activity to display the application status. Note Refer to this official Android activity article to understand more about Android activity. In this tutorial, we […]

Read more Android activity – from one screen to another screen

In Android, you always put either “wrap_content” or “fill_parent” on component’s attribute “layout_width” and “layout_height“, did you wonder what’s the different? See following definition : wrap_content – The component just want to display big enough to enclose its content only. fill_parent – The component want to display as big as its parent, and fill in […]

Read more Android wrap_content and fill_parent example

In Android, LinearLayout is a common layout that arranges “component” in vertical or horizontal order, via orientation attribute. In additional, the highest “weight” component will fill up the remaining space in LinearLayout. In this tutorial, we show you how to use LinearLayout to display 3 buttons in vertical and horizontal order, and also how “weight” […]

Read more Android LinearLayout example

Here’s a code snippet to show you how to use “android.content.Intent” to open an specify URL in Android’s web browser. button.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://mkyong.com")); startActivity(intent); } }); Note For full example, please refer to this – Android button example. References Android Button JavaDoc Android […]

Read more How to open an URL in Android’s web browser

In Android, just use “android.widget.Button” class to display a normal button. In this tutorial, we show you how to display a normal button, add a click listener, when user click on the button, open an URL in your Android’s internet browser. P.S This project is developed in Eclipse 3.7, and tested with Android 2.3.3. Note […]

Read more Android button example

In Android, you can use “android.widget.ImageButton” to display a normal “Button“, with a customized background image. In this tutorial, we show you how to display a button with a background image named “android_button.png“, when user click on it, display a short message. As simple as that. Note You may also like this Android ImageButton selector […]

Read more Android ImageButton example

In Android, you can use “android.widget.ImageView” class to display an image file. Image file is easy to use but hard to master, because of the various screen and dpi in Android devices. Note Please refer to this official Android’s “Drawable Resource” and “Screen Support” article for better understand of how image works in Android. In […]

Read more Android ImageView example

In Android, you can use “android.widget.TimePicker” class to render a time picker component to select hour and minute in a pre-defined user interface. In this tutorial, we show you how to render time picker component via android.widget.TimePicker in current page, and also in dialog box via android.app.TimePickerDialog. In addition, we also show you how to […]

Read more Android time picker example

In Android, you can use “android.widget.DatePicker” class to render a date picker component to select day, month and year in a pre-defined user interface. In this tutorial, we show you how to render date picker component in current page via android.widget.DatePicker, and also in dialog box via android.app.DatePickerDialog. In addition, we also show you how […]

Read more Android date picker example

In Android, you can use “android.widget.RadioButton” class to render radio button, and those radio buttons are usually grouped by android.widget.RadioGroup. If RadioButtons are in group, when one RadioButton within a group is selected, all others are automatically deselected. In this tutorial, we show you how to use XML to create two radio buttons, and grouped […]

Read more Android radio buttons example

In Android, you can use “android.widget.CheckBox” class to render a checkbox. In this tutorial, we show you how to create 3 checkboxes in XML file, and demonstrates the use of listener to check the checkbox state – checked or unchecked. P.S This project is developed in Eclipse 3.7, and tested with Android 2.3.3. 1. Custom […]

Read more Android checkbox example

Problem Android development with Eclipse IDE, sometime it hit following “adb” error message and caused the application fail to start? [2011-11-22 15:17:07 – HelloWorld] —————————— [2011-11-22 15:17:07 – HelloWorld] Android Launch! [2011-11-22 15:17:07 – HelloWorld] The connection to adb is down, and a severe error has occured. [2011-11-22 15:17:07 – HelloWorld] You must restart adb […]

Read more Android : The connection to adb is down, and a severe error has occurred.

For security concern, many users like to disable JavaScript in their web browser, and it raise a big problem in many website which depends on JavaScript to function well. The easy and simple solution is using the HTML “noscript” tag to display different content if JavaScript is disabled. See following full “noscript” example : If […]

Read more Display different content if JavaScript is disabled

See following example to get the real server file path via servletRequest.getRealPath(“/”). However, warning is prompt and complained that this method is deprecated. import javax.servlet.http.HttpServletRequest; public class DisplayAction { private HttpServletRequest servletRequest; public String execute() { //The method getRealPath(String) from the type ServletRequest is deprecated String filePath = servletRequest.getRealPath("/"); } @Override public void setServletRequest(HttpServletRequest arg0) […]

Read more The method getRealPath(String) from the type ServletRequest is deprecated

Google Maps API support four map types : ROADMAP – Displays normal street/road map (default map type). TERRAIN – Display normal street/road map based on terrain information. SATELLITE – Display satellite images only. HYBRID – Mixed normal and satellite views, display street/road views on top of the satellite images. Note Normally, you just use either […]

Read more Google Maps API – Map Types example