Android IP Subnet Practice App – Part 1 – External Libraries

I started app development on iOS, Swift and Xcode, so naturally I was used to Cocoa Pods to get external libraries. Android Studio (with Kotlin) has a similar method, but of course it’s a bit different. I’m no Android expert but I’ll share my learnings so far trying to put together an app that allows the user to practice calculating IPv4 subnets. If you’re not familiar with those, they are a crucial skill to understanding TCP/IP networking.

Concept

Just to get a very basic app working, we’ll build an experimental layout in order to test and see if the subnetting code is working. The basic idea is to get something that looks like this:

Subnet Practice Concept App

The top element is a text field that captures the user’s input, while the bottom element is a button that runs the code to find the “network address”, or the first IP address in a subnet, given an IP address and subnet mask. Above, we can see that from “192.168.0.5/24”, the network address “192.168.0.0/24” has been calculated after “TEST” was pressed. That’s what we’re shooting for.

Layout

We’ll create these three simple elements on a Relative Layout in activity_main.xml like this:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <EditText
        android:id="@+id/edittext_entry"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="100dp"
        android:text="192.168.0.5/24" />

    <TextView
        android:id="@+id/textview_result"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/edittext_entry"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="60dp" />

    <Button
        android:id="@+id/button_test"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/textview_result"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="60dp"
        android:text="Test" />

</RelativeLayout>

Hopefully this is pretty simple – it just creates the three elements and lays them out vertically in the center of the screen.

Adding an external library

Googling around, I found an IP address processing library on Github here:

https://github.com/seancfoley/IPAddress

On the Github page, it explicitly states the Maven Central (a large repository of libraries, connectivity to it is built-in to Android Studio) group and artifact ID’s, as well as the versions:

  • Maven group id: com.github.seancfoley
  • Maven artifact id: ipaddress
  • Maven versions: 2.0.2, 3.0.0, 4.3.3, 5.3.3

We can take this information and put a line in the app-level build.gradle file to download and install the library. Find the dependencies section (usually at the bottom) and add this line:

dependencies {
    //Others here removed for brevity
    implementation 'com.github.seancfoley:ipaddress:5.3.3'
}

Sync the gradle file and the library should be installed!

Kotlin code

In MainActivity.kt, we’ll connect to the layout elements and add logic. Make sure to enable view binding. Then we’ll write an onCreate function that looks like this:

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val binding = ActivityMainBinding.inflate(layoutInflater) #view binding
        setContentView(binding.root) #view binding

        val et = binding.edittextEntry #grab the EditText
        val r = binding.textviewResult #grab the TextView (result)
        val b = binding.buttonTest     #grab the Button

        b.setOnClickListener() {
            val str = et.text.toString() #get text from EditText
            val netAddr = IPAddressString(str).toAddress().toZeroHost().toString() #Magic to calculate network address
            r.text = netAddr #set TextView (result) to network address
        }
    }
}

This line:

val netAddr = IPAddressString(str).toAddress().toZeroHost().toString()

…takes whatever string is in the EditText and does these steps:

  • IPAddressString(str) turns the string into an IP address string, which is necessary for parsing the original string to a properly formatted IP address.
  • .toAddress() converts the string to an IP address object
  • .toZeroHost() finds the lowest IP address in the subnet, usually called the network address or subnet address. This library calls it a Zero Host.
  • .toString() converts the network address back to a string so it can be put in the TextView

This is definitely where the magic is happening in this code.

Hope this was all clear enough. Leave a comment or send me a message if it’s not!