Open In App

How to Build an IP Address Finder Android App?

Improve
Improve
Like Article
Like
Save
Share
Report

IP Address stands for Internet Protocol. It plays an important role while using the internet. It is one type of our device identity. It shows our location also. In this article, we will see How to make an android app to display the IP Address of a device using java?  We will use the WifiManager and WifiInfo utils classes to get an IP Address of the device, Both classes are present in the wifi package in java. Let’s see the step by step to implement it in the android app.

Step by Step Implementation

Step 1: Open an Android Studio software and click on New Project to create an android application.

 

Step 2: Select an Empty Activity from the listed activities under the Phone and Tablet section and click on Next.

 

Step 3: Select Java as the language for your Android app and give it any name that you want. (Eg. I am giving it “Get My IP“) and click on Finish.

 

Step 4: Add the following Permissions in the AndroidManifest.xml file

<uses-permission android:name = "android.permission.ACCESS_WIFI_STATE" />

AndroidManifest.xml file will look as shown below.

XML




<?xml version="1.0" encoding="utf-8"?>
    xmlns:tools="http://schemas.android.com/tools"
    package="com.app.getmyip">
  
    <uses-permission android:name = "android.permission.ACCESS_WIFI_STATE" />
  
    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.GetMyIP"
        tools:targetApi="31">
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
  
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
  
</manifest>


Step 5: Working with the activity_main.xml file

Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. Make a simple design to show/display the IP Address. You can use below simple layout code below contains two TextViews.

XML




<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
  
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@id/ip_address"
        android:layout_centerHorizontal="true"
        android:text="You Device's IP Address "
        android:textColor="@color/black"
        android:textSize="20sp" />
  
    <TextView
        android:id="@+id/ip_address"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="IP Address"
        android:textSize="22sp" />
  
</RelativeLayout>


Step 6: Working with the MainActivity.java file

Now we will  Import WifiInfo and WifiManager classes using the below lines of code in MainActivity.java.

import android.net.wifi.WifiManager;
import android.net.wifi.WifiInfo;

We have to make an instance of WifiManager class using that instance we will get wifiInfo, These wifiInfo class will have a method names getConnectionInfo() that provides all information regarding wifi, and using the getIpAddress() method we get an IP Address and put that address into Formatter to get dot(.) separated IP Address in a readable format. 

Java




package com.app.getmyip;
  
import android.annotation.SuppressLint;
  
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.text.format.Formatter;
import android.view.View;
import android.widget.TextView;
  
import androidx.appcompat.app.AppCompatActivity;
  
public class MainActivity extends AppCompatActivity {
  
    TextView ipAddressTv;
  
    @SuppressLint("MissingPermission")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
  
        ipAddressTv = findViewById(R.id.ip_address);
  
        WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE);
        WifiInfo wifiInfo = wifiManager.getConnectionInfo();
        final String IP_ADDRESS = Formatter.formatIpAddress(wifiInfo.getIpAddress());
        ipAddressTv.setText(IP_ADDRESS);
        ipAddressTv.setVisibility(View.VISIBLE);
  
    }
}


Step 8: Now the time is to run the app and get an IP Address. Click on the run app icon, make sure your android device has connected with your laptop and debugging mode is ON, Or you have installed Virtual Android Device.

Output:

 



Last Updated : 15 Nov, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads