Sunday, November 23, 2014

Getting Package names of all installed apps in Android Device


This code snippet will give the package names of all installed apps in device… Along with package name, it also gives label, description, version name, version code, icon…

PackageManager packageManager = getPackageManager();
List<PackageInfo> packages = packageManager.getInstalledPackages(0);
for(int i=0;i<packages.size();i++)
{
PackageInfo pInfo = packages.get(i);
String description = (String).applicationInfo.loadDescription(packageManager);
String appName   = pInfo .applicationInfo.loadLabel(packageManager).toString();
String packageName = pInfo .packageName;
String versionName = pInfo .versionName;
int versionCode = pInfo .versionCode;
Drawable icon = pInfo .applicationInfo.loadIcon(packageManager);
System.out.println(appName+" - "+packageName+" - "+versionName+" - "+versionCode+" - "+description);
}

Reference Link :- http://developer.android.com/reference/android/content/pm/PackageManager.html

Customizing or Replacing Progressbar circle in Android.

 Customizing or Replacing Progressbar circle in Android.


This code snippet will helps to replace android default progress bar circle with image.

Initially create an xml file with the rotation animation like this
custom_rotating_image.xml
<?xml version="1.0" encoding="utf-8"?>
<animated-rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@drawable/ic_launcher"
    android:pivotX="50%"
    android:pivotY="50%" />

Now assign this custom animation to progress bar by using android:indeterminateDrawable ="@drawable/custom_rotating_image" 
Layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <ProgressBar
        android:id="@+id/customdialog_progressbar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:indeterminateDrawable="@drawable/custom_rotating_image"
        android:progressDrawable="@drawable/ic_launcher" />

</LinearLayout>

Implementing “Share” functionality in Android

Implementing “Share” functionality in Android.

The common button will come across in many apps is “Share Button”, which helps to share data with available social network apps.


Here is the code snippet for implementing share functionality.

Button shareButton = (Button) findViewById(R.id.share_button);
shareButton.setOnClickListener(new OnClickListener() {
       @Override
       public void onClick(View v) {
              Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
              sharingIntent.setType("text/plain");
              String shareBody = "Here is the share content body";
              sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject Here");
              sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
              startActivity(Intent.createChooser(sharingIntent, "Share via"));
       }
});

Checking Connectivity & Network Information in Android

 Checking Connectivity & Network Information in Android


Network & Dataflow is the key thing in mobile devices…

Here  is code snippet for checking network information…

The manifest permissions required for network connectivity are…


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

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


The classes used for checking network connectivity are ConnectivityManager & NetworkInfo

Checking Network Information

public void onClick(View v)
{                                                                                                                             
ConnectivityManager connMgr = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
                                      
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
              
if (networkInfo != null && networkInfo.isConnected())
     {
  Toast.makeText(this, "Network connection available.", Toast.LENGTH_SHORT).show();
      }
else
     {
  Toast.makeText(this, "No network connection available.", Toast.LENGTH_SHORT).show();
     }
}
});


Checking wi-fi Information

public void onClick(View v)
{      
 ConnectivityManager connMgr = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
                                      
NetworkInfo networkInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
                                     
 boolean isWifiConn = networkInfo.isConnected();
     if (isWifiConn )
    {
     Toast.makeText(this, "Network connection available.", Toast.LENGTH_SHORT).show();
     }
     else
      {
      Toast.makeText(this, "No network connection available.", Toast.LENGTH_SHORT).show();
      }
  }
  });


Checking mobie Connection through sim Information

public void onClick(View v) {
                                                                                                                              
ConnectivityManager connMgr = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);


networkInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

boolean isMobileConn = networkInfo.isConnected();
  if (isMobileConn)
      {
       Toast.makeText(this, "Network connection available.", Toast.LENGTH_SHORT).show();
       }
 else
        {
      Toast.makeText(this, "No network connection available.", Toast.LENGTH_SHORT).show();
        }
 }
  });

Monday, October 20, 2014

ANDROID SUPPORTING MULTIPLE SCREEN

For supporting multiple screen,you need to create different layout for diff screen size. Support all screen you need to create following layout and put them each folder. Here is the different folder structure that you need to create.
Low density Small screens QVGA 240×320 (120dpi):
1
2
layout-small-ldpi (240x320) 
layout-small-land-ldpi (320x240)
Low density Normal screens WVGA400 240×400 (x432) (120dpi):
1
2
layout-ldpi  (240 x 400 )
layout-land-ldpi  (400 x 240 )
Medium density Normal screens HVGA 320×480 (160dpi):
1
2
layout-mdpi (320 x 480 )
layout-land-mdpi (480 x 320 )
Medium density Large screens HVGA 320×480 (160dpi):
1
2
layout-large-mdpi (320 x 480 )
layout-large-land-mdpi (480 x 320)
Tablet ( 240 dpi ):
1
2
3
4
5
6
7
8
9
10
11
12
13
layout-sw600dp for tablet 7inch
layout-sw720dp for tablet 10inch
or
layout-large  (600 x 1024)
layout-large-land  (1024 x 600)
High density Normal screens WVGA800 480x800 (x854) (240 dpi):
 
layout-hdpi (480 x 800)
layout-land-hdpi (800 x 480)
Xoom (medium density large but 1280x800 res) (160 dpi):
 
layout-xlarge (800 x 1280)
layout-xlarge-land (1280 x 800)
Also you should add following lines in .manifest file:
1
2
3
4
5
6
<supports-screens                                
    android:smallScreens="true"                   
    android:normalScreens="true"        
    android:largeScreens="true"           
    android:xlargeScreens="true"            
    android:anyDensity="true" />
Its all that you need do. Now your app will run smoothly in all devices.