Introduction to Java: input, output, file operations

I wrote a simple console application to show you how to read from and write to text files in Java. Basically it generates two HTML files (exam results based on a data file) in two formats. There is a version for students (results sorted by student id) and a version for a teacher (sorted by result). For Java programming I recommend IntelliJ IDEA.

A data file looks like this. First name then surname, student system id and number of points achieved. An example data file:

We want to store these lines in an array of objects so there is a class with variables needed, getters and setters and some more methods I will describe later. What’s important I use comparators (needed for sorting). I need data sorted by two keys so I use two comparators.

As you can see there is a few lines commented. If you need only one way of sorting you can uncomment them and comment those two comparators instead and use the following code somewhere in your StudentApp to do the sorting.

Continue reading Introduction to Java: input, output, file operations

Shared Preferences, Preference Fragment & transparent Navbar

PreferenceActivity is deprecated now but it’s a really nice new solution to manage your preferences. I’m gonna show you a simple implementation of PreferenceFragment, how to use it in Activity which you’d like to make a Preference Page and how to get values in your app. Example is about saving booleans and making preference fields clickable to call a method. Obviously you can put a wide variety of preference fields and it will work out of the box. PreferenceFragment makes everything easier, automated and it’s great.

When I started to learn about creating Android Apps, PreferenceActivity was already deprecated so I had an opportunity to learn something up to date. I really recommend that way of making a preference page. It’s more adjustable and complex solution. First thing to do is to create a preference layout. You can notice it’s located in xml directory while all normal layouts are in layout directory. Well, it’s somehow special and deserves its own location. You don’t have one? Just create the directory in your resources.

I assume you are creating the app using Android Studio. Edit your build.gradle configuration file. While creating this example I used Minimum SDK 14 (Ice Cream Sandwich). Make sure you are using the same value as well to avoid possible errors. PreferenceFragment may not be present in older versions of Android SDK. I’m not sure when it was actually introduced but it’s not so important.

Now a bit of real coding. Create a new class and put the code in there. It will create SettingsFragment which you can use later to display a preference page in an Activity of your choice.

Continue reading Shared Preferences, Preference Fragment & transparent Navbar

An advanced WebView with some cool features

I was angry with the official Facebook app for Android. It’s really heavy and I don’t mean the size of the package which is not actually a problem. The problem is that Facebook Mobile uses a lot of RAM and battery drain is not acceptable for me. That’s why I always prefered to use a mobile website rather than the official app.

When I started getting to know Android programming I decided to create an app which could help people using Facebook website. I wanted to have some cool Android features like pull to refresh and drawer layout. I did some research and found something needed to create a Web App. WebView allows you to use Android browser (Chromium) inside your app. It can support Java Script and it has actually all the features present in Google Chrome. So how to implement a simple WebView in you app? How to build something more advanced? I’m gonna show you right now.

First add INTERNET permission to your Manifest and add WebView to your layout xml file.

Now short piece of code to your Activity.

As you noticed I used a new WebView Client to have a possibility to customize the WebView.

As a result I achieved a simple implementation of a WebView. But it’s not enough to have for example file upload support (including a possibility to take pictures with your camera) while using online and local forms. And what about Java Script support and error handling? A Progress Bar would be also nice. I spent some time looking for all the solutions implemented below but it is worth the result.

Continue reading An advanced WebView with some cool features

Introduction: Installing Java (JDK) and Android Studio on Arch Linux

Welcome, I’m Krzysztof, I live in Krak√≥w (Poland) and a few weeks ago I decided to learn Java and Android. My programming experience is limited to C++ but it’s never too late to start. I’m gonna document problems I managed to resolve and show you some examples, especially code snippets appropriate for beginners. My main goal is to learn Android development, I’m also interested in Linux and Java. The operating system of my choice is Arch Linux.

At the links section you can find my Github account where I’m currently working on Facebook Lite, Web App for Android. I learnt a lot creating the app so it’s a great place to show you how it’s made and share my experience. But what is actually needed to start building Android apps? I personally recommend Android Studio by Google. It’s a great app for developers and the only recommended way of creating Android apps for now. But before you start you need Java Development Kit. I’m working on Arch Linux so now I’m gonna show you how to install Oracle Java (JDK) which is not present in Arch Linux (the system uses OpenJDK by default) and how to install Android Studio.

Now you are ready to go so it’s a perfect time to create your first Android app. Just create a new project with blank activity and test it on your device or start a new emulator. I’m not gonna write about the basic usage of Android Studio. It’s rather intuitive and well documented software. Instead of that I’ll share some links good to begin your programming journey.

  1. developer.android.com/training/index.html – you should begin here
  2. stackoverflow.com – it’s where you look for all the answers
  3. romannurik.github.io/AndroidAssetStudio/icons-launcher.html – Android Asset Studio: Launcher Icon Generator

Next time I will show you how to create an advanced WebView.