Friday, 27 November 2015

Example to get Network status in Android


// Check network connection
private boolean isNetworkConnected(){
    ConnectivityManager connectivityManager 
            = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
    return activeNetworkInfo != null && activeNetworkInfo.isConnected();    
}

Example for getting BlueTooth status in Android

public static boolean getBlueToothStatus()
{
boolean status=false;

BluetoothAdapter adapter=BluetoothAdapter.getDefaultAdapter();
if(adapter == null )
{
status = false;
}
else if(adapter.isEnabled())
{
status = true;
}
else
{
status = false;
}

return status;
}

Example for ProgressDialog in Android

public static ProgressDialog getDialog(Activity activity)
{
ProgressDialog pDialog=new ProgressDialog(activity,ProgressDialog.THEME_DEVICE_DEFAULT_LIGHT);
pDialog.setIndeterminate(true);
pDialog.setIndeterminateDrawable(activity.getResources().getDrawable(R.anim.progress_dialog_icon_drawable_animation));
pDialog.setCancelable(false);
pDialog.setCanceledOnTouchOutside(false);
pDialog.setMessage("    Please wait.....");
return pDialog;
}  

Alert dialog and CustomAlert Dailog with Bullets in android

////////////////    Normal Alert Dialog ///////////////////////

public static AlertDialog.Builder getAlertDialog(Activity activity,String msg)
{
AlertDialog.Builder builder=new AlertDialog.Builder(activity, AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);
builder.setTitle("Message");
builder.setMessage(msg);
builder.setIcon(R.drawable.logo_small);
    builder.setCancelable(false);
DialogInterface.OnClickListener listener=new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
dialog.dismiss();
}
};

builder.setPositiveButton("OK", listener);

return builder;
}







///////////////    Custome Alert Dialog Which contains Bullets ///////////////



Step 1> Create a xml file which contains ListView as below with the name of dialog_list_bullets.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >
    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:divider="#5B5B5B"
        android:dividerHeight="1dp"
         >
    </ListView>
</LinearLayout>

Step 2> Call that Alert Dialo from Any location By passing input parameter as a Activity name and String message which is seperated with ",".



public static AlertDialog.Builder  getAlertDialogWithBullets(Activity activity,String msg)
{

ArrayList<String> list=new ArrayList<String>();

StringTokenizer tokenizer=new StringTokenizer(msg, ",");

while(tokenizer.hasMoreElements())
{
list.add(tokenizer.nextToken()) ;
}
AlertDialog.Builder alertdialog = new AlertDialog.Builder(activity, AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);
       LayoutInflater inflater = activity.getLayoutInflater();
       View convertView = (View) inflater.inflate(R.layout.dialog_list_bullets, null);
       alertdialog.setView(convertView);
       alertdialog.setTitle("Message");
       alertdialog.setCancelable(false);
       alertdialog.setIcon(R.drawable.logo_small);
       ListView lv = (ListView) convertView.findViewById(R.id.listView1);
       lv.setAdapter(new BulletsAdapter(activity, list));
   
       DialogInterface.OnClickListener listener=new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
dialog.dismiss();
}
};

alertdialog.setPositiveButton("OK", listener);

return alertdialog;
}

Create a xml file and store in internal Memory in android

 public static void insertXMLFile(String cur_activity,  String fr_Id)
 {
try
{
      DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
      DocumentBuilder builder=factory.newDocumentBuilder();
      String path="/storage/sdcard0/HealthKos.xml";
     
      File f=new File(path);
    Document d = null;
    Element e1;
      if(f.exists())
   {
      f.delete();
      }
      d=builder.newDocument();
     
    e1=d.createElement("Main");
      Element e2=d.createElement("Location");
      Element e3=d.createElement("LocationId");
     // Element e4=d.createElement("LocationId");
     
      //For login Activity 1
     //For login Activity 2
     //For login Activity 3
     
      Node n1=d.createTextNode(cur_activity);
      Node n2=d.createTextNode(fr_Id);
     
     
     // Node n3=d.createTextNode("3");
     
      e2.appendChild(n1);
      e3.appendChild(n2);
     // e4.appendChild(n3);
     
      e1.appendChild(e2);
      e1.appendChild(e3);
      //e1.appendChild(e4);
   
      if(!f.exists())
      {
      d.appendChild(e1);
      }

      TransformerFactory tfactory=TransformerFactory.newInstance();
      Transformer t=tfactory.newTransformer();
       
      DOMSource source=new DOMSource(d);
     
      FileWriter writer=new FileWriter("/storage/sdcard0/HealthKos.xml");
     
      StreamResult result=new StreamResult(writer);
     
      t.transform(source, result);
      //Toast.makeText(activity, "complete", 10000).show();
   }
catch (Exception e)
{
    e.printStackTrace();
   }
}