Android get public ip address programmatically

Android get public ip address programmatically

Hey, In this tutorial we are getting a mobile phone device IP address while the phone is connected to a mobile data connection or WiFi connection. This example demonstrates android get public ip address programmatically

Whenever you enable WiFi on your device and have an active connection to a WiFi network, your mobile data is temporarily disabled, Android Apps/Applications Mobile development this example demonstrates how do I get the IP address of an android device programmatically.

How to use TabLayout in ViewPager with RecyclerView

In this example, we will use Network Interface to get the IP address. So, before starting the code we need to know about NetworkInterface.


This class represents a Network Interface made up of a name, and a list of IP addresses assigned to this interface. It is used to identify the local interface on which a multicast group is joined. Interfaces are normally known by names such as "le0".


Public methods

Resource : Network Interface

booleanequals(Object obj)Compares this object against the specified object.
static NetworkInterfacegetByIndex(int index)Get a network interface given its index.
static NetworkInterfacegetByInetAddress(InetAddress addr)Convenience method to search for a network interface that has the specified Internet Protocol (IP) address bound to it.
static NetworkInterfacegetByName(String name)Searches for the network interface with the specified name.
StringgetDisplayName()Get the display name of this network interface.
byte[]getHardwareAddress()Returns the hardware address (usually MAC) of the interface if it has one and if it can be accessed given the current privileges.
intgetIndex()Returns the index of this network interface.
Enumeration<InetAddress>getInetAddresses()Convenience method to return an Enumeration with all or a subset of the InetAddresses bound to this network interface.
List<InterfaceAddress>getInterfaceAddresses()Get a List of all or a subset of the InterfaceAddresses of this network interface.
intgetMTU()Returns the Maximum Transmission Unit (MTU) of this interface.
StringgetName()Get the name of this network interface.
static Enumeration<NetworkInterface>getNetworkInterfaces()Returns all the interfaces on this machine.
NetworkInterfacegetParent()Returns the parent NetworkInterface of this interface if this is a subinterface, or null if it is a physical (non-virtual) interface or has no parent.
Enumeration<NetworkInterface>getSubInterfaces()Get an Enumeration with all the subinterfaces (also known as virtual interfaces) attached to this network interface.
booleanisLoopback()Returns whether a network interface is a loopback interface.
booleanisPointToPoint()Returns whether a network interface is a point to point interface.
booleanisUp()Returns whether a network interface is up and running.
booleanisVirtual()Returns whether this interface is a virtual interface (also called subinterface).
booleansupportsMulticast()Returns whether a network interface supports multicasting or not.

How to get ip address of android device programmatically

There are few steps to get an IP address of device.

Step 1

Create a new project in Android Studio, go to File -> New Project and fill all required details to create a new project.

Step 2

Add the following code to res/layout/activity_main.xml.

Android Collapsing Toolbar With Image

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android=""

      android:text="IP address of your Device"
      android:textSize="20sp" />


Step 3

Add the following code to androidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="" package="">
   <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>

      <activity android:name=".MainActivity">
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />

Step 4

Add the following code to src/

import android.os.Bundle;
import android.text.format.Formatter;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
   TextView textView;

   protected void onCreate(Bundle savedInstanceState) {
      textView = findViewById(;
      textView.setText("Your Device IP Address: " + getIPAddress(true));
   public static String getIPAddress(boolean useIPv4) {
		try {
			List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
			for (NetworkInterface intf : interfaces) {
				List<InetAddress> addrs = Collections.list(intf.getInetAddresses());
				for (InetAddress addr : addrs) {
					if (!addr.isLoopbackAddress()) {
						String sAddr = addr.getHostAddress().toUpperCase();
						boolean isIPv4 = InetAddressUtils.isIPv4Address(sAddr);
						if (useIPv4) {
							if (isIPv4)
								return sAddr;
						} else {
							if (!isIPv4) {
								int delim = sAddr.indexOf('%'); // drop ip6 port suffix
								return delim<0 ? sAddr : sAddr.substring(0, delim);
		} catch (Exception e) {
		return "";

Related posts

Write a comment