Friday, 17 October 2014

how to uninstall shortcut from home screen in andriod



/**
     * add permission for Android manifest xml
     * **/

<uses-permission
        android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />
/**
     *call method for onCreate method
     * **/
 removeShortcutIcon();

/**
     * Adding shortcut for MainActivity
     * Home screen
     * **/
private void removeShortcutIcon() {
     
    //Deleting shortcut for MainActivity
    //on Home screen
    Intent shortcutIntent = new Intent(getApplicationContext(),
            MainActivity.class);
    shortcutIntent.setAction(Intent.ACTION_MAIN);
     
    Intent addIntent = new Intent();
    addIntent
            .putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "your app name");
 
    addIntent
            .setAction("com.android.launcher.action.UNINSTALL_SHORTCUT");
    getApplicationContext().sendBroadcast(addIntent);
}
 
 

 

how to install shortcut on home screen in andriod



/**
     * add permission for Android manifest xml
     * **/
  <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
/**
     *call method for onCreate method
     * **/
 addShortcutIcon();

/**
     * Adding shortcut for MainActivity
     * Home screen
     * **/
     private void addShortcutIcon() {
   
            Intent shortcutIntent = new Intent(getApplicationContext(),MainActivity.class);       
            shortcutIntent.setAction(Intent.ACTION_MAIN);
            Intent addIntent = new Intent();
            addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
            addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "your app name");
            addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,Intent.ShortcutIconResource.fromContext(getApplicationContext(),R.drawable.ic_launcher));
            addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
            getApplicationContext().sendBroadcast(addIntent);
        }


Tuesday, 7 October 2014

how to Integrating Google Maps API v2 in Android


activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.MapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
</fragment>



MainActivity.java

package com.googlemap;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class MainActivity extends Activity {
    private GoogleMap googleMap;

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

        try {
           
            initilizeMap();
           
            googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
            googleMap.setMyLocationEnabled(true);
            googleMap.getUiSettings().setZoomControlsEnabled(false);
            googleMap.getUiSettings().setMyLocationButtonEnabled(true);
            googleMap.getUiSettings().setCompassEnabled(true);
            googleMap.getUiSettings().setRotateGesturesEnabled(true);
            googleMap.getUiSettings().setZoomGesturesEnabled(true);

            double latitude = 23.0600290;
            double longitude = 72.6772020;

            for (int i = 0; i < 5; i++) {
           
                double[] randomLocation = createRandLocation(latitude,longitude);

                MarkerOptions marker = new MarkerOptions().position(new LatLng(randomLocation[0], randomLocation[1])).title("Hello Maps" + i);
                if (i == 0)
                    marker.icon(BitmapDescriptorFactory
                            .defaultMarker(BitmapDescriptorFactory.HUE_AZURE));
                if (i == 1)
                    marker.icon(BitmapDescriptorFactory
                            .defaultMarker(BitmapDescriptorFactory.HUE_BLUE));
                if (i == 2)
                    marker.icon(BitmapDescriptorFactory
                            .defaultMarker(BitmapDescriptorFactory.HUE_CYAN));
                if (i == 3)
                    marker.icon(BitmapDescriptorFactory
                            .defaultMarker(BitmapDescriptorFactory.HUE_GREEN));
                if (i == 4)
                    marker.icon(BitmapDescriptorFactory
                            .defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA));
                if (i == 5)
                    marker.icon(BitmapDescriptorFactory
                            .defaultMarker(BitmapDescriptorFactory.HUE_YELLOW));

                googleMap.addMarker(marker);
                if (i == 5) {
                    CameraPosition cameraPosition = new CameraPosition.Builder()
                            .target(new LatLng(randomLocation[0],
                                    randomLocation[1])).zoom(15).build();

                    googleMap.animateCamera(CameraUpdateFactory
                            .newCameraPosition(cameraPosition));
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    protected void onResume() {
        super.onResume();
        initilizeMap();
    }

    private void initilizeMap() {
        if (googleMap == null) {
            googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();

            if (googleMap == null) {
                Toast.makeText(getApplicationContext(),"Sorry! unable ", Toast.LENGTH_SHORT).show();
            }
        }
    }
    private double[] createRandLocation(double latitude, double longitude) {

        return new double[] { latitude + ((Math.random() - 0.5) / 500),longitude + ((Math.random() - 0.5) / 500),150 + ((Math.random() - 0.5) * 10) };
    }
}


AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.googlemap"
    android:versionCode="1"
    android:versionName="1.0" >

    <permission
        android:name="com.googlemap.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />

    <uses-permission android:name="com.googlemap.permission.MAPS_RECEIVE" />

    <uses-sdk
        android:minSdkVersion="12"
        android:targetSdkVersion="17" />

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <!-- Required to show current location -->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name="com.googlemap.MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppBaseTheme" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="your API Key" />
    </application>

</manifest>


 Getting Started - Google Maps Android API v2 — Google Developers




Monday, 6 October 2014

how to set edittext error message in android


activity_main.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="match_parent"
    android:paddingTop="20dp"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Username" >
    </EditText>

    <EditText
        android:id="@+id/editText2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Password" />

    <Button
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Loging" />

</LinearLayout>


MainActivity.java

package com.test;

import android.app.Activity;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {

   
    private EditText edit_username,edit_password;
    private Button btn_login;
   
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       
        edit_username=(EditText)findViewById(R.id.editText1);
        edit_password=(EditText)findViewById(R.id.editText2);
       
        btn_login=(Button)findViewById(R.id.button1);
        btn_login.setOnClickListener(new OnClickListener() {
           
            @Override
            public void onClick(View v) {
               
                if(Validation()){
                   
                    Toast.makeText(getApplicationContext(), "login Success", Toast.LENGTH_LONG).show();
                }
               
            }
        });
    }
   
    private boolean Validation(){
       
        // Checking If Selected Values is Default
        if (edit_username.getText().toString().equals("")) {
            edit_username.setError(Html.fromHtml("<font color='red'>Enter the Username</font>"));
            return false;
        }
       
        if (edit_password.getText().toString().equals("")) {
            edit_password.setError(Html.fromHtml("<font color='red'>Enter the Password</font>"));
            return false;
        }
        return true;
       
    }

}