Monthly Archives: August 2012

Android – JellyBean Features

Jelly Bean

Some new features introduced in Jelly bean are listed below:

Offline voice typing: In contrast with the conventional SIRI assistant, voice dictations in Jelly bean can be done in flight mode or without an internet connection. All the phrasing and voice processing is performed locally, i.e. by the phone processor itself. No server interaction is required in Android 4.1 version. Since no internet connection is required, there are no server processing or server load lags and delays in dictation.

Triple buffering: Android used to lag in the race with iPhone and windows phone in terms of smooth visual display and notifications. Gone are those days with the introduction of V4.1; smooth visual and screen transitions can be experienced by the user even in load conditions. Triple buffer is that technique which provides the essence of smoothness in which three buffers of memory are deployed to process pixels even in load conditions. Two buffers are used as same i.e. front buffer and back buffer. Third tier of buffer activates when the back buffer fails to provide the output on the screen.
This maintains the user experience in load conditions and transitions are presented in a smooth manner.

NFC enabled: Expanded as Near field communication technology, it is the new buzzword for Android 4.1 OS. Along with the bluetooth capability, NFC is another example of machine to machine communication between two devices with a tap.

Google Now: Introduced for Jelly Bean, Google now is a search application which provides the search results automatically. It is like the evolution of the application. It learns about the search suited for a particular instant based on search history, location history and returns the best suited searches. This app is accessed via Google search app.

Jelly Bean

Automatic widget resizing: Unlike conventional widget structure, Android 4.1 has introduced the automatic resizing of widget according to the home screen. Icons size is also changed according to the space.

Advanced notification panel: A notification panel cannot be usual one if its about the new version of Android. This is the reason that Jelly bean is introduced with an advanced notification panel, instead of linking the notification with it’s local app, the details of the notification are shown in the panel directly. For eg. If a person receives a new mail in his Email account, the notification panel itself can be used to read the mail instead of directing the user to his Email account.

Some other features like, predictive keywords in terms of advanced algorithm for predicting text has been introduced. Better search results with voice searching, increased frame rates, high synch between rendering and touch inputs are a few other add-ons provided in this new version. Touch prediction, according to Google, is something of interest which predicts the next touch on the screen.

Which you think is the best – Android or iPhone?

This is a question that has been here since 2007. We know there are many advantages for Android phones and Apple’s iPhone. So what you think, which one is the best.

I will say my personal opinion. And I think Android is the best.

Android vs iPhone

What is your opinion?

Are you an iPhone or ANDROID User?

Why you think one is better than the other from your personal experience?

Do you think Fragmentation is a curse for ANDROID or iPhone?

Paste you opinion here and Let’s see what real phone users are thinking.

How to start with ActionBar in Android?

With Android 3.0 Google eliminated the need of hardware menu button which is replaced by Action Bars.

Here is a quick example on how to use ActionBars.

Action Bar in Android

First create a new project and name it ActionBarExample.

Now create a folder named “menu” in res folder and create a new XML file inside it named “action_bar_menu.xml”.

action_bar_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:id="@+id/menuitem1" android:showAsAction="ifRoom" android:title="Test1"></item>
    <item android:id="@+id/menuitem2" android:showAsAction="ifRoom" android:title="Test2"></item>
</menu>

Now the java code for implementing this ActionBar.

MainActivity.java

package com.example.actionbarexample;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;

public class MainActivity extends Activity {
	@Override
	  public void onCreate(Bundle savedInstanceState) {   
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.activity_main);
	  }

	  @Override
	  public boolean onCreateOptionsMenu(Menu menu) {
	    MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.action_bar_menu, menu);
	    return true;
	  }

	  @Override
	  public boolean    onOptionsItemSelected(MenuItem item) {
	    switch (item.getItemId()) {
	    case R.id.menuitem1:
	      Toast.makeText(this, "Test 1 Clicked", Toast.LENGTH_SHORT).show();
	      break;
	    case R.id.menuitem2:
	      Toast.makeText(this, "Test 2 Clicked", Toast.LENGTH_SHORT).show();
	      break;

	    default:
	      break;
	    }

	    return true;
	  }
	} 

As there is enough space in the ActionBar otherwise you may see the Overflow menu or you have to use the Option menu button on your phone.

Please leave your comments om this post.

Uploading and Downloading of files – Popular and Useful posts from CoderzHeaven

Here are the links of my most popular posts on downloading and uploading of files to and from your android device.

uploading and downloading images in android

1. How to download a file from a remote site in android? – Another simple example – Method -3

2. How to download a file to your android device from a remote server with a custom progressbar showing progress?

3. How to upload an image from Android device to server? – Method 4

4. Uploading audio, video or image files from Android to server

5. How to Upload Multiple files in one request along with other string parameters in android?

6. How to Download an image in ANDROID programatically?

7. ANDROID – Upload an image to a server

Please leave your comments if you find these posts useful.

Join the Forum discussion on this post

How to set different layouts for Portrait and Landscape in Android?

This simple example shows how to set different layouts for portrait and Landscape in Android.

If you need a detailed explanation please check this url

http://developer.android.com/guide/practices/screens_support.html

Android Supports these different categories of screens.

1. xlarge screens are at least 960dp x 720dp

2. large screens are at least 640dp x 480dp

3. normal screens are at least 470dp x 320dp

4. small screens are at least 426dp x 320dp

We use configuration qualifier for doing this.

To use a configuration qualifier:

  1. Create a new directory in your project’s res/ directory and name it using the format:<resources_name>-<qualifier>
    • <resources_name> is the standard resource name (such as drawable or layout).
    • <qualifier> is a configuration qualifier from table 1, below, specifying the screen configuration for which these resources are to be used (such as hdpi or xlarge).

    You can use more than one <qualifier> at a time—simply separate each qualifier with a dash.

  2. Save the appropriate configuration-specific resources in this new directory. The resource files must be named exactly the same as the default resource files.

For example, xlarge is a configuration qualifier for extra large screens. When you append this string to a resource directory name (such as layout-xlarge), it indicates to the system that these resources are to be used on devices that have an extra large screen.

Table 1. Configuration qualifiers that allow you to provide special resources for different screen configurations.

Screen characteristic Qualifier Description
Size small Resources for small size screens.
normal Resources for normal size screens. (This is the baseline size.)
large Resources for large size screens.
xlarge Resources for extra large size screens.
Density ldpi Resources for low-density (ldpi) screens (~120dpi).
mdpi Resources for medium-density (mdpi) screens (~160dpi). (This is the baseline density.)
hdpi Resources for high-density (hdpi) screens (~240dpi).
xhdpi Resources for extra high-density (xhdpi) screens (~320dpi).
nodpi Resources for all densities. These are density-independent resources. The system does not scale resources tagged with this qualifier, regardless of the current screen’s density.
tvdpi Resources for screens somewhere between mdpi and hdpi; approximately 213dpi. This is not considered a “primary” density group. It is mostly intended for televisions and most apps shouldn’t need it—providing mdpi and hdpi resources is sufficient for most apps and the system will scale them as appropriate. If you find it necessary to provide tvdpi resources, you should size them at a factor of 1.33*mdpi. For example, a 100px x 100px image for mdpi screens should be 133px x 133px for tvdpi.
Orientation land Resources for screens in the landscape orientation (wide aspect ratio).
port Resources for screens in the portrait orientation (tall aspect ratio).
Aspect ratio long Resources for screens that have a significantly taller or wider aspect ratio (when in portrait or landscape orientation, respectively) than the baseline screen configuration.
notlong Resources for use screens that have an aspect ratio that is similar to the baseline screen configuration.

We will start with an example.

Please take a look at the screenshot which describes the above description.

L-2

The main.xml shown inside each folder may be same or different.
Android will automatically select each layout according to the Device resolution whether it is small, normal or HD.

i.e if the Device is of normal resolution and in the portrait mode then the layout inside “layout-normal” will be selected. When we rotate the same device for landscape then then layout under “layout-normal-land” will be selected.

L-1

L-2

Please leave your valuable comments on this post.

How to make a Button appear like a TextView in Android?

Hello all…..

This is a simple trick where we can make the Button appear like a TextView in Android.

This is done by setting the background of Button to null like this.

b.setBackgroundDrawable(null);

This is the xml that contains the button.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<Button  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    android:textColor="#FFF"
    android:id="@+id/but"
    />
</LinearLayout>

How to stream an audio in Android?

This is a simple example to how to stream an audio in android.

Here is the java code for that.

package com.coderzheaven.pack;

import android.app.Activity;
import android.app.ProgressDialog;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnErrorListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class StreamAudioDemo extends Activity implements OnClickListener,
OnPreparedListener, OnErrorListener, OnCompletionListener {

    MediaPlayer mp;
    ProgressDialog pd;
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Button bt = (Button)findViewById(R.id.play);
    bt.setOnClickListener(this);
}

   @Override
   public void onPrepared(MediaPlayer mp) {
       Log.i("StreamAudioDemo", "prepare finished");
       pd.setMessage("Playing.....");
       mp.start();
  }

  @Override
  public void onClick(View v) {
       try
        {
    	    pd = new ProgressDialog(this);
    	    pd.setMessage("Buffering.....");
    	    pd.show();
            mp = new MediaPlayer();
            mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
            mp.setOnPreparedListener(this);
            mp.setOnErrorListener(this);
            mp.setDataSource("http://www.robtowns.com/music/blind_willie.mp3");
            mp.prepareAsync();
            mp.setOnCompletionListener(this);
        }
        catch(Exception e)
        {
            Log.e("StreamAudioDemo", e.getMessage());
        }
  	}

  	@Override
  	public boolean onError(MediaPlayer mp, int what, int extra) {
      pd.dismiss();
      return false;
  	}

	@Override
	public void onCompletion(MediaPlayer mp) {
		pd.dismiss();
		Toast.makeText(getApplicationContext(), "Completed", Toast.LENGTH_LONG).show();		
	}
}

This is the xml that contains the button.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<Button  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Stream Audio and Play"
    android:id="@+id/play"
    />
</LinearLayout>

When you run this program you will see a button and on clicking on that button you will see a dialog with message “Buffering…..”. Once the buffering is complete the audio will be start playing.

Stream_1

Stream_2

Stream_3

Please leave your valuable comments on this post.

How to load an image from the assets folder in android?

Here is a simple example showing how to load an image stores in assets folder in android?

package com.coderzheaven.pack;
import java.io.IOException;
import java.io.InputStream;
import android.app.Activity;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView;

public class LoadImageFromAssetsDemo extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        // Get the AssetManager
        AssetManager manager = getAssets();

        // Read a Bitmap from Assets
        try {
        	InputStream open = manager.open("icon.png");
        	Bitmap bitmap = BitmapFactory.decodeStream(open);
        	// Assign the bitmap to an ImageView in this layout
        	ImageView view = (ImageView) findViewById(R.id.ImageView01);
        	view.setImageBitmap(bitmap);
        } catch (IOException e) {
        	e.printStackTrace();
        } 
    }
}

Done.