Thursday, 28 May 2015

How to get IMEI number in android

Here I am going to explain how to IMEI  number programmatically in android

package com.example.get_mobilename;
import android.app.Activity;
import android.telephony.TelephonyManager;
import android.content.Context;
import android.os.Bundle;
import android.widget.Toast;

public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TelephonyManager tel= (TelephonyManager)this.getSystemService(Context.
TELEPHONY_SERVICE); String imei= tel.getDeviceId();
Toast.makeText(getApplicationContext(), imei,Toast.LENGTH_LONG).show();
}

}







Add the below permission in your Manifestfile

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

Happy coding:)

Get SIM Serial number programmatically in android

Here I am going to explain how to get SIM Serial number programmatically in android

package com.example.get_mobilename;
import android.app.Activity;
import android.telephony.TelephonyManager;
import android.content.Context;
import android.os.Bundle;
import android.widget.Toast;

public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TelephonyManager tel= (TelephonyManager)this.getSystemService(Context.
TELEPHONY_SERVICE); String SimSerialNumber = tel.getSimSerialNumber();
Toast.makeText(getApplicationContext(), SimSerialNumber,
Toast.LENGTH_LONG).show();
}

}







Add the below permission in your Manifestfile

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

Happy coding:)

Get phone number programmatically in android

Here I am going to explain how to get mobile number programmatically in android

package com.example.get_mobilename;
import android.app.Activity;
import android.telephony.TelephonyManager;
import android.content.Context;
import android.os.Bundle;
import android.widget.Toast;

public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TelephonyManager tel= (TelephonyManager)this.getSystemService(Context.
TELEPHONY_SERVICE); String PhoneNumber = tel.getLine1Number();
Toast.makeText(getApplicationContext(), PhoneNumber ,
Toast.LENGTH_LONG).show();
}

}





Add the below permission in your Manifestfile

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

Happy coding:)

Get Android Phone Model Programmatically


Here I am going to explain how to get mobile model and brand name programmatically in android

package com.example.get_mobilename;
import android.app.Activity;


import android.content.Context;
import android.os.Bundle;
import android.widget.Toast;

public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String model= android.os.Build.MODEL;
String device= android.os.Build.MANUFACTURER;
Toast.makeText(getApplicationContext(), modeldevice,
Toast.LENGTH_LONG).show();
}

}
Happy coding

Wednesday, 27 May 2015

How to check server connection is available or not in android

Add the below function to your Activity Class to check server connection

public boolean isConnectedToServer(String url, int timeout) {
 try{
    URL myUrl = new URL(url);
    URLConnection connection myUrl .openConnection();
    connection .setConnectTimeout(timeout);
    connection .connect();
    return true;
} catch (Exception e) {
    // Handle your exceptions
    return false;
}
}


and also add intent permission in your manifest file

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

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

Happy Coding:)

How to create SQLite table in android


   Here I am going to explain how to create SQLite table in android


Step 1).DataBaseHelper Class
 
package com.createtable;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class DataBaseHelper extends SQLiteOpenHelper 
{
static final String DATABASE_NAME = "groupchat.db";
static final int DATABASE_VERSION = 1;
private final Context context;
public static final String TABLE_MESSAGES = "messages";

public DataBaseHelper(Context _context) {
super(_contextDATABASE_NAME , null, DATABASE_VERSION );

this.context_context;
}

@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table messages (_id integer primary key autoincrement,                                                         text,userid integer, fromUsermsg text);");
}

@Override
public void onUpgrade(SQLiteDatabase db int oldVersion, int newVersion)
         {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_MESSAGES);

onCreate(db);
}

public void insertvalues() {
SQLiteDatabase db = this.getWritableDatabase();

ContentValues Values = callForValues();
db.insert("messages", null, Values );

}

private ContentValues callForValues() {
ContentValues values = new ContentValues();
values .put("msg", "hi");
values .put("userid", 1);
values .put("fromUser", "Dhina");

return values ;

}

}

Step 2).Activity Class

package com.createtable;
import android.app.Activity;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;

public class MainActivity extends Activity {

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

DataBaseHelper db  = new DataBaseHelper(getApplicationContext());
db .insertvalues();
}

}

Happy Coding:)

Generate Notification in Android

   Here I am going to explain how to generate notification in android


  public static void generateNotification(Context context, String message)
       {
int icon = R.drawable.image;
long when = System.currentTimeMillis();
NotificationManager notificationManager =(NotificationManager)                               context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, messagewhen );

String title = context.getString(R.string.app_name);

Intent notificationIntent= new Intent(context, Activity.class);
// set intent so it does not start a new activity
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);

PendingIntent intent = PendingIntent.getActivity(context, 0,
notificationIntent, 0);
notification .setLatestEventInfo(context, title, messageintent);
notification .flags |= Notification.FLAG_AUTO_CANCEL;

// Play default notification sound
notification .defaults |= Notification.DEFAULT_SOUND;

// Vibrate if vibrate is enabled
notification .defaults |= Notification.DEFAULT_VIBRATE;
notificationManager .notify(0, notification );
}

Monday, 25 May 2015

Android Load Webview with Progressbar

Here I am going to explain how to display a progress bar with webview
                                  
Step 1) Open res ⇒ layout⇒ activity_main.xml and add below items.
                                    <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical"

    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin" >
    <WebView
        android:id="@+id/web"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
    </LinearLayout>


Step 2) Activity Class
package com.example.androidprogressbar;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends Activity {

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

              final Activity WebActivity = this;
             getWindow().setFeatureInt(Window.FEATURE_PROGRESS,            
                            Window.PROGRESS_VISIBILITY_ON);
             webview= (WebView) findViewById(R.id.web);
             webview.setWebChromeClient(new WebChromeClient() 
             {
              public void onProgressChanged(WebView view, int progress)   
                 {
                     WebActivity .setTitle("Loading...");
                    WebActivity .setProgress(progress * 100); 
                     if(progress == 100)
                    WebActivity .setTitle(R.string.app_name);
           }
         });
     webview.setWebViewClient(new WebViewClient());
     webview.getSettings().setJavaScriptEnabled(true);
     webview.loadUrl("http://google.com");
}
}


Step 3) Add android internet permission to your Manifestfile.xml
 <uses-permission android:name="android.permission.INTERNET"/>


Happy Coding :)

Android Splash Screen Example


Here I am going to explain the implementation of android splash screen


package com.Splash;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;

public class SplashScreen extends Activity 

{
private static int SPLASH_TIME_OUT = 2000;

@Override

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);

new Handler().postDelayed(new Runnable() {


@Override

public void run() {

Intent i = newIntent(SplashScreen .thisFirstActivity.class);

                       
startActivity(i);

overridePendingTransition(R.anim.abc_fade_in, R.anim.abc_fade_in);

                                                                    
finish();
}
}, SPLASH_TIME_OUT);
}


}


Happy Coding :)


Friday, 22 May 2015

Android Clear all EditText Fields


 Here I am going to explain how to clear all the EditText Fields in a single Button  click

Step 1)Open res->layout->activity_main.xml;

   <RelativeLayout    
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/allClear"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.clear_edittext.MainActivity" >

    <TextView
        android:id="@+id/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Android Dhina Textwatcher Example" />

    <EditText
        android:id="@+id/editext1"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/android"
        android:layout_marginTop="50dp"
        android:textColor="#000000" />
   <EditText
        android:id="@+id/editext2"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editext1"
        android:layout_marginTop="50dp"
        android:textColor="#000000" />


   <EditText
        android:id="@+id/editext3"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editext2"
        android:layout_marginTop="50dp"
        android:textColor="#000000" />


  <Button
        android:id="@+id/clear"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp"
        android:layout_below="@+id/editext3"
        android:text="Clear" >

    </Button>

</RelativeLayout >



Step 2)Activity Class
  
package com.example.clear_edittext;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;


public class MainActivity extends Activity 
{

EditText editext1,editext2,editext3;
@Override
protected void onCreate(Bundle savedInstanceState) 
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editext1=(EditText)findViewById(R.id.editext1);
editext2=(EditText)findViewById(R.id.editext2);
  editext3=(EditText)findViewById(R.id.editext3);

  Button Clear=(Button)findViewById(R.id.Clear);
  Clear.setOnClickListener(new OnClickListener() {
            @Override
    public void onClick(View v) 
     {
ViewGroup group = (ViewGroup) findViewById(R.id.allClear);
clearEditext(group );
}

});


}     

   private void clearEditext(ViewGroup group
{
for (int i = 0, count = group.getChildCount(); i < count; ++i) {
        View view = group.getChildAt(i);
        if (view instanceof EditText) {
        ((EditText)view).setText("");
        }
        if(view instanceof ViewGroup&& (((ViewGroup)view).getChildCount() > 0))
        clearEditext((ViewGroup)view);

}
}
}
Happy Coding :)