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.

<PreferenceScreen
    xmlns:android="http://schemas.android.com/apk/res/android">

    <PreferenceCategory
        android:title="@string/preference_category1">

        <CheckBoxPreference
            android:key="example_key"
            android:title="@string/example_key"
            android:defaultValue="true"/>

        <CheckBoxPreference
            android:key="transparent_nav"
            android:title="@string/transparent_nav"
            android:summary="@string/transparent_nav_description"
            android:defaultValue="false"/>

    </PreferenceCategory>

    <PreferenceCategory
        android:title="@string/preference_category2">

        <Preference
            android:key="call_method"
            android:title="@string/call_method"
            android:summary="@string/call_method_description"/>

    </PreferenceCategory>

</PreferenceScreen>

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.

apply plugin: 'com.android.application'

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"
    defaultConfig {
        applicationId "org.indywidualni.preferences"
        minSdkVersion 14
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    productFlavors {
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:appcompat-v7:21.0.3'
}

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.

<uses-permission android:name="android.permission.INTERNET" />
    <WebView
        android:id="@+id/webView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

Now short piece of code to your Activity.

private WebView webView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        webView = (WebView) findViewById(R.id.webView1);
        webView.loadUrl("http://m.facebook.com");
        webView.setWebViewClient(new MyAppWebViewClient());
    }

    // handling back button
    @Override
    public void onBackPressed() {
        if(webView.canGoBack()) {
            webView.goBack();
        } else {
            super.onBackPressed();
        }
    }

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

package org.indywidualni.myapplication;

import android.content.Intent;
import android.net.Uri;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MyAppWebViewClient extends WebViewClient {

    // handling external links as intents
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {

        if( Uri.parse(url).getHost().endsWith("facebook.com") ) {
            return false;
        }

        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        view.getContext().startActivity(intent);
        return true;
    }

}

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.

yaourt -S jdk
sudo archlinux-java set java-8-jdk
yaourt -S 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.