Wednesday, 22 October 2014

How to show Android Activity as a dialog in Android

we can apply a dialog theme to an activity so that it is displayed as a floating dialog.

To apply a dialog theme to an activity, just modify the <Activity> element in the AndroidManifest.xml file by adding the android:theme attribute(android:theme="@android:style/Theme.Dialog") to the Activity.


 In Manifest file just add one simple attribute to show activity as a dialog.

 <activity
android:label="@string/app_name"
android:name=".Activity"
android:theme="@android:style/Theme.Dialog"  />

Then here we got one doubt how to close this activity dialog? 

solution is just call finish( ) method to close the activity dialog.









Saturday, 16 August 2014

Exception occurred while reading or writing file {0}The Axis2 facets cannot be installed since the Axis2 runtime location has not been set. Go to the Web Services preference page and set the Axis2 runtime location under Axis2 Preferences

This is the exception am getting when am trying to create a web service.


Exception occurred while reading or writing file {0}The Axis2 facets cannot be installed since the Axis2 runtime location has not been set.   Go to the Web Services preference page and set the Axis2 runtime


How to Resolve this Issue ?

Create Dummy dynamic web project with axis2 feature and leave it. Next try your old project .













Tuesday, 5 August 2014

how to change port no of jboss manually

Here the procedure to change the port number of jboss (or) check the current port number of jboss server

These are the steps to change port number of jboss manually.

Steps:

 1) Go to below path

     jboss-6.0.0.Final\server\default\deploy\jbossweb.sar\  

  2) Fnd out server.xml in jbossweb.sar folder

  3) Open server.xml

  4) Check this below tag in xml file and change port attribute in xml .

    <Connector protocol="HTTP/1.1" port="8243" address="${jboss.bind.address}" 
           redirectPort="${jboss.web.https.port}" />

 5) Restart your JBoss server.

  Now check from your browser

   http://localhost:8243/







Thursday, 3 April 2014

how to disable or restrict specific number in keyboard while entering into Edittext Android

Hi here the below code used to restrict or disable specific number while enter in Edittext.

You no need to use any filters or lot of lines code just using simple attribute in Edittext.

i.e using android:digits attribute you can resolve this issue

Code to restrict '0' while entering any number in Edittext.

android:digits="123456789"

Code to restrict '1' while entering any number in Edittext.

 android:digits="234567890"
Code to restrict '2' while entering any number in Edittext.

 android:digits="134567890"

Code to restrict '3' while entering any number in Edittext.

 android:digits="124567890"

Code to restrict '4' while entering any number in Edittext.

 android:digits="123567890"

Code to restrict '5' while entering any number in Edittext.

 android:digits="123467890"

Code to restrict '6' while entering any number in Edittext.

 android:digits="123457890"

Code to restrict '7' while entering any number in Edittext.

 android:digits="123456890"

Code to restrict '8' while entering any number in Edittext.

 android:digits="123456790"

Code to restrict '9' while entering any number in Edittext.

 android:digits="123456780"


Code to restrict '0' while entering any number in Edittext.

Example:

 <EditText
        android:id="@+id/number"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:digits="123456789"
        android:hint="Enter Pin"
        android:inputType="number" >

Friday, 7 March 2014

How to show .gif images in android (load .gif image from url example source)

Here the below example shows how to access .gif images from web and shown in android programmatically(Load animated GIF from Internet)


when an application attempts to perform a networking(Internet) operation(i.e connecting to internet,dowload images etc..) on its main thread.we will get "NetworkOnMainThreadException"

In such cases we should implement this requirement using background task like(asynctask,threads)

Here the below code to load gif image from URL

1) Create ShowGifView.java  class which extends View

This is the main class ShowGifView  takes input as context object and url of .gif image .

get the image from url and reload view.

package com.androidsurya.giffromurl;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Movie;
import android.os.AsyncTask;
import android.util.AttributeSet;
import android.view.View;

public class ShowGifView extends View {

// Set true to use decodeStream
// Set false to use decodeByteArray
private static final boolean DECODE_STREAM = true;

private InputStream gifInputStream;
private Movie gifMovie;
private int movieWidth, movieHeight;
private long movieDuration;
private long mMovieStart;

static String gifURL;

public ShowGifView(Context context, String a) {
super(context);
gifURL = a;
init(context);
}

public ShowGifView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}

public ShowGifView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}

private void init(final Context context) {
setFocusable(true);

gifMovie = null;
movieWidth = 0;
movieHeight = 0;
movieDuration = 0;
Loder task = new Loder();
task.execute(new String[] { gifURL });

}

private static byte[] streamToBytes(InputStream is) {
ByteArrayOutputStream os = new ByteArrayOutputStream(1024);
byte[] buffer = new byte[1024];
int len;
try {
while ((len = is.read(buffer)) >= 0) {
os.write(buffer, 0, len);
}
} catch (java.io.IOException e) {
}
return os.toByteArray();
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(movieWidth, movieHeight);
}

public int getMovieWidth() {
return movieWidth;
}

public int getMovieHeight() {
return movieHeight;
}

public long getMovieDuration() {
return movieDuration;
}

@Override
protected void onDraw(Canvas canvas) {

long now = android.os.SystemClock.uptimeMillis();
if (mMovieStart == 0) { // first time
mMovieStart = now;
}

if (gifMovie != null) {

int dur = gifMovie.duration();
if (dur == 0) {
dur = 1000;
}

int relTime = (int) ((now - mMovieStart) % dur);

gifMovie.setTime(relTime);

gifMovie.draw(canvas, 0, 0);
invalidate();

}

}

private class Loder extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
URL gifURL;
try {
gifURL = new URL(urls[0]);
HttpURLConnection connection = (HttpURLConnection) gifURL
.openConnection();

gifInputStream = connection.getInputStream();

// Insert dummy sleep
// to simulate network delay
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

if (DECODE_STREAM) {
gifMovie = Movie.decodeStream(gifInputStream);
} else {
byte[] array = streamToBytes(gifInputStream);
gifMovie = Movie.decodeByteArray(array, 0, array.length);
}
movieWidth = gifMovie.width();
movieHeight = gifMovie.height();
movieDuration = gifMovie.duration();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}

@Override
protected void onPostExecute(String result) {
invalidate();
requestLayout();

}
}

}


2) Create showgif.xml file under layout folder. 

Copy the below code

Here addView used to attach .gif view to this layout.

<?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:orientation="vertical" >

    <RelativeLayout
        android:id="@+id/addView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </RelativeLayout>

</LinearLayout>

3) Create ShowGifActivity.java  

package com.androidsurya.giffromurl;

import android.app.Activity;
import android.os.Bundle;
import android.widget.RelativeLayout;

public class ShowGifActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.showgif);
RelativeLayout myLayout = (RelativeLayout) findViewById(R.id.addView);
                    //code to add gif image view to RelativeLayout -here created object to ShowGifView.class and pass the current object and url
myLayout.addView(new com.androidsurya.giffromurl.ShowGifView(this,
"http://www.dan-dare.org/Dan%20Sonic/SonicTailsPlaneAni.gif"));
}
}


4) Last but important thing is AndroidManifest.xml

Note : Add Internet permission and   android:hardwareAccelerated="false" Tags.

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.androidsurya.giffromurl"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
      />
<uses-permission  android:name="android.permission.INTERNET"/>
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.androidsurya.giffromurl.ShowGifActivity"
            android:hardwareAccelerated="false"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Output Screenshot:





















Friday, 28 February 2014

Actionbar in android 2.2 (Below Android 3.0 Version) Example - ActionBarSherlock Example

The action bar provides several key functions:
 

Provides a dedicated space for giving your app an identity and indicating the user's location in the app.

Makes important actions prominent and accessible in a predictable way (such as Search).

Supports consistent navigation and view switching within apps (with tabs or drop-down lists).


The ActionBar APIs were first added in Android 3.0 (API level 11) but they are also available in the Support Library for compatibility with Android 2.1 (API level 7) and above.

Develop Application using ActionBar APIs in Above Android 3.0(API level 11) is Very Easy.ActionBar APIs Provides On wards Android 3.0(API level 11).

But How to if you run the same application on versions < 3.0 you won’t. Okay, It shows lot's of error messages and Application is crashed.

Then what is soultion for this issue ?


So, how do we get the ActionBar on pre-Honeycomb?using ActionBarSherlock this library does is that if you’re using your app in a device with Android 2.x, it gives you an ActionBar that is a copy of the real ActionBar (it behaves the same way), but if you’re on Android 3.0 or later, you’ll get the standard ActionBar .
The best thing is that the methods that you can use with this ActionBar are the same than with the standard one.
 
How to use ActionBarSherlock in Below Android 3.0 (API level 11) ?

Here are the steps to get the library into Eclipse:


First of all, go to Sherlock ActionBar library’s website(http://actionbarsherlock.com/) and Download the latest version (current is 4.0.0).

Then (unzip) open the file you downloaded, enter the root folder and you will see three folders: library, samples and website.

Extract the library folder into your work space folder. You can rename it if you want to something like “Sherlock-ActionBar-lib-4.0.0”.

Open Eclipse. Click File -> New -> Android Project.

Now select “Create project from existing source”, and select the folder that you copied into your work space folder.

You should also type a name such as “Sherlock-ActionBar-lib-4.0.0” :). Now press Next.
Select Android 4.1 as target SDK and press Finish.

Open the libs folder in the Project that we've created, right-click the “android-support-v4.jar” file and select Build Path -> Add to Build Path.

Now, we have the Android Library Project of the Sherlock ActionBar in our work space and in Eclipse.

Now create a  a new Android Project, targeting at least Android 4.1 SDK. We will call ActionBarSherlockExample with MainActivity Class.

Add Library(Sherlock-ActionBar-lib) project to ActionBarSherlockExample.

Steps for Adding (Sherlock-ActionBar-lib) to Current Project:

Select Project->Right click Select Properties from menu->Now Properties Dialog visible now select Android on (Left side panel)->Now you can see Is Add button to Add Library project to current project just select (Sherlock-ActionBar-lib) and add library project to current project and click on apply button.

Now Open your Activity(MainActivity.java)  which is created in current project.
 
public class MainActivity extends Activity {

}


Now Change extends Activity to SherlockActivity

See below code

public class MainActivity extends SherlockActivity {

}

Final Step ( Open the AndroidManifest of your ActionBarSherlockExample project and write inside the application tag just add below attribute)

android:theme="@style/Theme.Sherlock.Light.DarkActionBar"


Now you can run your current project in below 
 Android 3.0 (API level 11) version the you can see ActionBar .


Android Project:


Android Activity(MainActivity.java)

package com.androidsurya.actionbarsherlock;

import com.actionbarsherlock.app.SherlockActivity;
import android.os.Bundle;

public class MainActivity extends SherlockActivity {

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

}

AndroidManifest file

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

    
<uses-sdk android:minSdkVersion="7" />

    
<application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/Theme.Sherlock.Light.DarkActionBar" >
        <activity
            android:name="com.androidsurya.actionbarsherlock.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                
<category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>









 

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