Wednesday, July 27, 2011

opening a Dial-er Screen Android

In order open a Dial-er screen with the mobile no also android provides an intent.

Using this we will go to Dial-er screen.

In order use the intent we have to include a permission in the manifast.xml
<uses-permission android:name = "android.permission.CALL_PHONE"/>

Example:

Intent intent_Call = new Intent(Intent.ACTION_DIAL);
intent_Call.setData(Uri.parse("tel:" + strMobNo)); //strMobNo is the mobile no that we have to pass
startActivity(intent_Call);                                       //like 0123456789

how to make call in Android

Android provides default intent to manage calling.

Using this we directly go to calling screen.


In order use the intent we have to include a permission in the manifast.xml
<uses-permission android:name = "android.permission.CALL_PHONE"/>

Example:

Intent intent_Call = new Intent(Intent.ACTION_CALL);
intent_Call.setData(Uri.parse("tel:" + strMobNo)); //strMobNo is the mobile no that we have to pass
startActivity(intent_Call);                                       //like 0123456789

how to send an email

The snippet helps to send email from your android mobile.

Using this intent it will list a list of mail clients that are available to handle this intent.

If we select one of them then it will send email using that mail client.

Example.
 Intent emailIntent = new Intent(Intent.ACTION_SEND);
//Message Type
emailIntent.setType("text/image");

//To Address. Here strArrTo is an string array
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, strArrTO);

//CC . Here strArrCC is an string array
emailIntent.putExtra(android.content.Intent.EXTRA_CC, strArrCC);

//BCC. Here strArrBCC is an string array
emailIntent.putExtra(android.content.Intent.EXTRA_BCC, strArrBCC);

//Subject line. Here strSUB is string
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, strSUB);

//Body..Here strMes is string
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, strMes);

//Starting Intent...
startActivity(emailIntent);

Friday, July 15, 2011

Context Menu Android

Context Menu is similar to right click on your desktop system.

In Android context menu is used with list view. In we long press list view item then the menu event fires.

In order to use Context Menu with the list view we have register the Context Menu for the list view. The following snippet explain the use of Context Menu with the list view.

Example:

main.xml
 <?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">
  
    <ListView
        android:layout_width="fill_parent"
        android:id="@+id/lvContextMenu"
        android:layout_height="fill_parent">
    </ListView>   
       
</LinearLayout>

Home.java
package com.lvcm.activities;
import android.app.Activity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class Home extends Activity
{
    ListView lvContextMenu;
    ListAdapter adapter;
    private String[] MenuItems = {"Edit", "Delete"};
   
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        //Getting the List View from the xml file
        lvContextMenu = (ListView)findViewById(R.id.lvContextMenu);
        lvContextMenu.setAdapter(new ListAdapter());
       
        //Registering Context Menu
        registerForContextMenu(lvContextMenu);
    }
   
    // Context Menu Creation
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)
    {
        if (v.getId()==R.id.lvContextMenu)
        {           
            menu.setHeaderTitle("CONTEXT MENU");
            for (int i = 0; i< MenuItems.length; i++)
            {
                menu.add(Menu.NONE, i, i, MenuItems[i]);
            }
      }
    }
   
   // Context Menu Item Selection
    @Override
    public boolean onContextItemSelected(MenuItem item)
    {
        AdapterView.AdapterContextMenuInfo info =                                                                            (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
      
        // Getting the Id
        int menuItemIndex = item.getItemId();
        Toast.makeText(Home.this, "Clicked Item Position :"+info.position+"\n"+"Seleted Option Id :"+menuItemIndex, Toast.LENGTH_SHORT).show();       
        return true;
    }
     
    //List Adapter
    public class ListAdapter extends BaseAdapter
    {
        @Override
        public int getCount()
        {           
            return 10;
        }

        @Override
        public Object getItem(int arg0)
        {           
            return arg0;
        }

        @Override
        public long getItemId(int arg0)
        {           
            return arg0;
        }

        @Override
        public View getView(int arg0, View arg1, ViewGroup arg2)
        {
            TextView tv;
           
            if(arg1 == null)
            {
                tv = new TextView(Home.this);
            }
            else
            {
                tv = (TextView) arg1;
            }           
            tv.setText("list item");
            return tv;
        }       
    }
}
OutPut:

Thursday, July 14, 2011

Fonts Android / Custom Fonts

In android by default the following fonts are available.
DEFAULT
DEFAUT_BOLD
SERIF
SANS-SERIF
MONO SPACE

The Typeface class is used to specify the style of a font. In this example i am creating a textview and apply the fonts to that textview.

TextView tv = new TextView(this);

//For DEFAULT
tv.setTypeface(Typeface.DEFAULT);

//For DEFAUT_BOLD
tv.setTypeface(Typeface.DEFAULT_BOLD);

//For SERIF
tv.setTypeface(Typeface.SERIF);

//For SANS-SERIF
tv.setTypeface(Typeface.SANS_SERIF);  
//For MONO SPACE
tv.setTypeface(Typeface.MONOSPACE);

//Custom Fonts
To create custom font first we have to download the font file (.ttf). Copy that file to Assest folder of your project.

//Creation of custom font
Typeface tf = Typeface.createFromAsset(getAssets(), "BaroqueScript.ttf");
tv.setTypeface(tf);

Note:
For more info about Typeface visit
http://developer.android.com/reference/android/graphics/Typeface.html

Expandable list view move group icon indicator to right

The setIndicatorBounds(int left, int right) is used to set the indicator bounds for the group view of an expandable list view.

explvList.setIndicatorBounds(width-GetDipsFromPixel(35), width-GetDipsFromPixel(5));

Here width means device width.


//Convert pixel to dip
public int GetDipsFromPixel(float pixels)
{
        // Get the screen's density scale
        final float scale = getResources().getDisplayMetrics().density;
        // Convert the dps to pixels, based on density scale
        return (int) (pixels * scale + 0.5f);

}

Note:

The imp point is indicator icon design.

The icon should be like this height of the icon is equal to parent view height with transparent background. In that we put our required image in the center.

The width is equal to width specified in the setBounds method. Here in my snippet it is 35.

Other wise the icon is disturbed.

Output :

Tuesday, July 12, 2011

Reducing dimness while running Progress Dialog in Android

In Android while running progress dialog the screen become dim. The following snippet helpful to reduce the dimness.
 pdlg = ProgressDialog.show(getParent(), "Loading...", "");
WindowManager.LayoutParams lp = pdlg.getWindow().getAttributes();
 lp.dimAmount=0.0f;        //Dim Amount
 //Applying properties to progress dialog
 pdlg.getWindow().setAttributes(lp);
//Dismiss the dialog
pdlg.dismiss();