Android Get IP Address

Android application for fetching network IP address of the android device

Create new android project and add below code changes on MainActivity.java 

Source SVN:Android IP Address Zipped Source :IPAddressDisplay.zip
package com.javaorigin.ipdisplay;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class MainActivity extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  try {
   for (Enumeration en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
              NetworkInterface intf = en.nextElement();
              for (Enumeration enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
                  InetAddress inetAddress = enumIpAddr.nextElement();
                  if (!inetAddress.isLoopbackAddress()) {
                  TextView ipView= (TextView) findViewById(R.string.ip);
                  ipView.setText(inetAddress.getHostAddress());
                  }
              }
          }
  } catch (Exception e) {
   Log.e("------------", e.toString());
  }
   
 }


}

Add network permission to AndroidManifest.xml
       
          <uses-permission android:name="android.permission.INTERNET" />

7 comments :

  1. hi i got an error at TextView ipView= (TextView) findViewById(R.string.ip);

    ReplyDelete
  2. i always read blog but this is one of the best for the viewers thanks for the real and helpful blog. software company in delhi

    ReplyDelete
  3. Awesome. I am a beginner and this article helped me to resolve the problem .Thank you author and the whole expect team.

    http://www.wikitechy.com/view-article/how-to-find-ip-address-in-java


    Both are really good,
    Cheers,
    Venkat

    ReplyDelete
  4. It's interesting that many of the bloggers your tips helped to clarify a few things for me as well as giving... very specific nice content.Android training in chennai with placement | Android Training in chennai |Android Training in Velachery

    ReplyDelete
  5. While developing application sometimes you may need the IP address of android device on which an application is installed. So in this tutorial, I am going to show how to get the IP address of an android device and display in TextView programmatically.

    Android Training in chennai | Android Training|Android Training in chennai with placement | Android Training in velachery

    ReplyDelete
  6. Your website is cool and fun, and your blog is wonderful. I am very happy that I came across your write-up. Now, I am all set to take your advice. I am sure to visit free source code download

    ReplyDelete