Wednesday, 29 January 2014

Android Add and Remove Application Shortcut in Home Screen Example

One of my friend is asking
When i am installing any application from the android market .I seen shortcut of the application in my mobile home screen.

how to create programmatically can u know ?

Based on that request Here i was creating one example how to create shortcut and remove shortcut on home screen.

Android provide intent class com.android.launcher.action.INSTALL_SHORTCUT which can be used to add shortcuts to home screen.

intent class com.android.launcher.permission.UNINSTALL_SHORTCUT which can be used to remove shortcut from home screen.

Here i created Activity with two buttons one for Add shortcut and other one for remove shortcut.And i was using preferences to store state of shortcut (already created or not)

Android Activity(MainActivity.java)

package com.androidsurya.shortcuts;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {
Context mContext = MainActivity.this;
SharedPreferences appPreferences;

@Override
public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Preferences to store all ready created or not finding purpose.
appPreferences = PreferenceManager.getDefaultSharedPreferences(this);
// Add listener to add shortcut button
Button add = (Button) findViewById(R.id.AddShortcut);
add.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
if (!(appPreferences.getBoolean("isShortcutCreated", false))) {
addShortcut(); // Add shortcut on Home screen

}

}
});

// Add listener to remove shortcut button
Button remove = (Button) findViewById(R.id.RemoveShortcut);
remove.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
removeShortcut(); // Remove shortcut from Home screen
}
});
}

private void addShortcut() {
// Adding 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, "Shortcut Example");
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);
SharedPreferences.Editor editor = appPreferences.edit();
editor.putBoolean("isShortcutCreated", true);
editor.commit();
}

private void removeShortcut() {
// 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, "Shortcut Example");
addIntent.setAction("com.android.launcher.action.UNINSTALL_SHORTCUT");
getApplicationContext().sendBroadcast(addIntent);
SharedPreferences.Editor editor = appPreferences.edit();
editor.putBoolean("isShortcutCreated", false);
editor.commit();
}

}

Android UI layout (activity_main.xml)

<LinearLayout xmlns: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" >

    <Button
        android:id="@+id/AddShortcut"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Add Shortcut" />

    <Button
        android:id="@+id/RemoveShortcut"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Remove Shortcut" />

</LinearLayout>

Note: 
 Don't Forget Register this Below permissions  In Manifest File 

<uses-permission
         android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
    <uses-permission
         android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />

Output Screenshot:






   































Tags : Android application,app shortcut in homescreen,android homescreen with application shortcut,application shortcut,remove shortcut from home screen,how to add shortcut of application in home screen.

Tuesday, 28 January 2014

How to avoid duplicate's insertion into android SQLite data base?


The UNIQUE constraint uniquely identifies each record in a database table.

The UNIQUE and PRIMARY KEY constraints both provide a guarantee for uniqueness for a column or set of columns.

A PRIMARY KEY constraint automatically has a UNIQUE constraint defined on it.

Note that you can have many UNIQUE constraints per table, but only one PRIMARY KEY constraint per table.

SQL UNIQUE Constraint on CREATE TABLE
The following SQL creates a UNIQUE constraint on the "P_Id" column when the "Persons" table is created:

Query

CREATE TABLE Persons
(
P_Id int NOT NULL UNIQUE,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255)
)

Friday, 24 January 2014

Multiple images Insert and Retrieve from SQLite Database and assign to Grid View in Android

I was done single image insertion and retrive from the android sqlite in preview post(http://androidsurya.blogspot.in/2012/11/insert-and-retrieve-image-from-sqlite.html).So many people asking how to insert multiple images and retrieve multiple images from the android sqlite database.

Now i cleared and i created one example to store multiple images and retrieve multiple images from the android sqlite database.retrieved images assigned to android gridview.

Here the below example insert multiple images in to sqlite and retrive images from the  android sqlite and assigning to android gridview.(I was written this code just modify the previews post which i was posted in past -http://androidsurya.blogspot.in/2012/11/insert-and-retrieve-image-from-sqlite.html)

Output Screenshot:
























Tags: Android,grid-view,images insert,images retrieve,no of images in sq-lite,how to show multiple images in grid view from the sq lite database,

Android SQLite Database Viewer or Debuging with Stetho

Every Android Developer uses SQLite Database to store data into the Android Application data. But to view the data in SQLite have a lot of...