Friday, March 29, 2013

Friday, March 22, 2013

When Landscape view is used then how can change the layout in landscape view?


1. Create new folder in res/layout-land.
2. Make two layout file with same name main.xml
3. Now place one layout file which is for landscape view in res/layout-land/main.xml


Now when you change view of device that can be take layout automatically from the layout.


Tuesday, March 12, 2013

How to detact Internet connection availibility in android ?

1. Create Layout 

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Detect Internet Status" />

    <Button android:id="@+id/btn_check"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="Check Internet Status"
        android:layout_centerInParent="true"/>

</RelativeLayout>

2.Create ConnectionDetector class

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

public class ConnectionDetector {
     private Context _context;
   
        public ConnectionDetector(Context context){
            this._context = context;
        }
   
        public boolean isConnectingToInternet(){
            ConnectivityManager connectivity = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);
              if (connectivity != null)
              {
                  NetworkInfo[] info = connectivity.getAllNetworkInfo();
                  if (info != null)
                      for (int i = 0; i < info.length; i++)
                          if (info[i].getState() == NetworkInfo.State.CONNECTED)
                          {
                              return true;
                          }
   
              }
              return false;
        }

}

3. now in main activity class write following code.

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.Menu;
import android.view.View;
import android.widget.Button;


public class MainActivity extends Activity {

    // flag for Internet connection status
    Boolean isInternetPresent = false;

    // Connection detector class
    ConnectionDetector cd;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       
       
        Button btn_check=(Button) findViewById(R.id.btn_check);
       
        // creating connection detector class instance
        cd = new ConnectionDetector(getApplicationContext());
       
        btn_check.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View v)
            {

                // get Internet status
                isInternetPresent = cd.isConnectingToInternet();

                // check for Internet status
                if (isInternetPresent)
                {
                    // Internet Connection is Present
                    // make HTTP requests
                    showAlertDialog(MainActivity.this, "Internet Connection","You have internet connection", true);
                }
                else
                {
                    // Internet connection is not present
                    // Ask user to connect to Internet
                    showAlertDialog(MainActivity.this, "No Internet Connection","You don't have internet connection.", false);
                }
            }

        });
    }
  
    public void showAlertDialog(Context context, String title, String message, Boolean status) {
        AlertDialog alertDialog = new AlertDialog.Builder(context).create();

        // Setting Dialog Title
        alertDialog.setTitle(title);

        // Setting Dialog Message
        alertDialog.setMessage(message);

        // Setting alert dialog icon
        //alertDialog.setIcon((status) ? R.drawable.success : R.drawable.fail);

        // Setting OK Button
        alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
            }
        });

        // Showing Alert Message
        alertDialog.show();
    }
   
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

Friday, March 8, 2013

How to use SQLiteAssetHelper Library with SQLite Database

Jar file and documentation are available at  https://github.com/jgilfelt/android-sqlite-asset-helper.

It is used to add your database folder in asset folder in your folder hierarchy and use it in your Application. 


  1. Add android-sqlite-asset-helper.jar to lib directory.
  2. Copy database in a zip file in assets/databases/database_name.zip.
  3. Create class:
    public class MyDatabase extends SQLiteAssetHelper {
    
        private static final String DATABASE_NAME = "database_name";
        private static final int DATABASE_VERSION = 1;
    
        public MyDatabase(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);  
        }
     }
    
    

Friday, February 22, 2013

AVD Configuration

Device
Version
Resolution
RAM
LCD Density
Hardware Back/Home
Nexus
Galaxy Nexus
17 (4.2.1)
720x1280
1024
316
no
Nexus S
16 (4.1.2)
480x800
512
233
yes
Nexus 7
17 (Google APIs)
800x1280
1024
216
no
Nexus 4
17 (Google APIs)
768x1280
2048
320
no
Nexus 10
17 (Google APIs)
2560x1600
2048
300
no
Galaxy
Galaxy S2
15 (4.0.4)
480x800
1024
218
yes
Galaxy S3
16 (4.1.2)
720x1280
1024
306
yes

Saturday, December 29, 2012

Disable Android Browser Zoom Function

<meta content='True' name='HandheldFriendly' />
<meta content='width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;' name='viewport' />
<meta name="viewport" content="width=device-width" />


OR


<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />

Wednesday, November 14, 2012

Run .APK File On Android Emulator


1.     Go to sdk/platform-tools with the command cd /home/android-sdk/platform-tools you should copy the .apk you want to install in this directory and add the path of it to environment variable in variable PATH "you might need restart"
2.     List the devices with the command ./adb devices
3.     You can install your file with ./adb install .filename.apk

Example for step 2 :
./adb devices
     List of devices attached
     emulator-5554    device
Example for step 3 :
./adb install AndWebServiceTest.apk