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,

Wednesday, 11 December 2013

Sunday, 17 November 2013

Difference between Android AsyncTask,Thread,IntentService and Service


Service
Thread
IntentService
AsyncTask
When to use ?
Task with no UI, but shouldn't be too long. Use threads within service for long tasks.
- Long task in general.

- For tasks in parallel use Multiple threads (traditional mechanisms)
- Long task usually with no communication to main thread.
(Update)- If communication is required, can use main thread handler or broadcast intents

- When callbacks are needed (Intent triggered tasks). 
- Small task having to communicate with main thread.

- For tasks in parallel use multiple instances OR Executor
 
Trigger
Call to method
onStartService()
Thread start() method
Intent
Call to method execute()
Triggered From (thread)
Any thread
Any Thread
Main Thread (Intent is received on main thread and then worker thread is spawed)
Main Thread
Runs On (thread)
Main Thread
Its own thread
Separate worker thread
Worker thread. However, Main thread methods may be invoked in between to publish progress.
Limitations /
Drawbacks
May block main thread
- Manual thread management

- Code may become difficult to read
- Cannot run tasks in parallel.

- Multiple intents are queued on the same worker thread.
- one instance can only be executed once (hence cannot run in a loop) 

- Must be created and executed from the Main thread

Content from : techtej

Thursday, 17 October 2013

List of open source Game Engines for Android(Android Game Development)

Here below is list of Game Engine used in Android Game Development

  • Open Source Android Apps for Developers: jMonkeyEngine (Java Based 3D Game Engine)
  • Open Source Android Apps for Developers: Android-2D-Engine (Android Game)
  • Open Source Android Apps for Developers: YoghurtGum (Android Game Engine)
  • Open Source Android Apps for Developers: Catcake (Android Game Engine)
  • Open Source Android Apps for Developers: jPCT-AE (Android Game 3D Engine)
  • Open Source Android Apps for Developers: Dwarf-fw (Android 3D Framework)
  • Open Source Android Apps for Developers: Mages (Android Game Engine)
  • Open Source Android Apps for Developers: AndEngine (Android Game Engine)
  • Open Source Android Apps for Developers: Angle (Android Game Engine)

Thursday, 10 October 2013

Tuesday, 1 October 2013

Android GridLayout Example (New Feature in 4.0)

Here The Below Example shows How to create GridLayout in Android ?

GridLayout induced in Android 4.0 as New Feature.

LinearLayout and RelativeLayout are the most common layouts used in user
interface design in Android. For simple user interfaces they are a good choice but
when the user interface gets complicated, the use of nested LinearLayout tends
to increase. Nested layouts (of any type) can hurt performance, and furthermore
nested LinearLayout deeper than 10 may cause a crash in your application.
Thus, you should either avoid using too many nested LinearLayout blocks or
you should use RelativeLayout in order to decrease nested LinearLayout blocks.
Another drawback of these layouts for complicated user interfaces is the difficulty
in readability. It is difficult to maintain nested LinearLayout or RelativeLayout
layouts that have many views. It is a good choice to use GridLayout in these
cases. Too many nested LinearLayouts could be avoided by using GridLayout.
Furthermore, it is much easier to maintain GridLayout. Many of the user interfaces
that use LinearLayout, RelativeLayout, or TableLayout can be converted to
GridLayout where GridLayout will provide performance enhancements. One of
the major advantages of GridLayout over other layouts is that you can control the
alignment of a view in both horizontal and vertical axes.

Example GridLayout:

<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:columnCount="3"
    android:rowCount="3" >
    <Button android:text="1,1" />
    <Button android:text="1,2" />
    <Button android:text="1,3" />
    <Button android:text="2,1" />
    <Button android:text="2,2" />
    <Button android:text="2,3" />
    <Button android:text="3,1" />
    <Button android:text="3,2" />
    <Button android:text="3,3" />
</GridLayout>

Output Screen:








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...