Wednesday, October 12, 2011

Custom ProgressDilaog like iPhone

        We use Android default ProgressDialog  when we run any background operation to indicate user to wait till the operation finished. So that user will not be able to select anything on the screen till ProgressDialog dismiss but the ProressDialog can only be shown in the center of the Screen. We can't set its position and also we can't remove its default background (white boarder) .

   The alternative is that we can use ProgressBar but we have to prevent user interaction during the Progress shown. Here is the code how we can achieve this..

 Create a layout xml named popup_example.xml


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_centerInParent="true">
<ProgressBar
android:id="@android:id/progress"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dip"/>

<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Loading Content"
android:layout_margin="10dip"
android:textColor="#FFFFFF"
android:layout_gravity="center_vertical" />
</LinearLayout>
</RelativeLayout>


Then we have to use this layout in our Activity as follows..
Create a Activity names CustomDialog


import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

public class CustomDialog
{
private static Dialog mDialog = null;

public CustomDialog()
{
// do Nothing
}
public static void showProgressDialog(Context mContext, String text, boolean cancellable)
{
removeDialog();

mDialog = new Dialog(mContext, android.R.style.Theme_Translucent_NoTitleBar);

LayoutInflater mInflater = LayoutInflater.from(mContext);
View layout = mInflater.inflate(R.layout.popup_example, null);
mDialog.setContentView(layout);

TextView mTextView = (TextView) layout.findViewById(R.id.text);

if (text.equals(""))
mTextView.setVisibility(View.GONE);
else
mTextView.setText(text);

mDialog.setOnKeyListener(new DialogInterface.OnKeyListener()
{
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event)
{
switch (keyCode)
{
case KeyEvent.KEYCODE_BACK:
return true;
case KeyEvent.KEYCODE_SEARCH:
return true;
}
return false;

}
});

mDialog.setCancelable(false);

mDialog.show();
}

public static void removeDialog(){

if (mDialog != null)
mDialog.dismiss();
}
}

To use this CustomDialog we have to write this in our Activity 

CustomDialog _progress = new CustomDialog();
_progress.showProgressDialog(this,"Please Wait.... ",false);


Happy Coding.....

Wednesday, December 22, 2010

custom list view in android

You can create custom listview in android and you can add any layout to this. Here is the code for this .

first you have create a xml file for listview as follows.

"mainllist.xml" and save it in the layout folder

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content">
 
 
  <ListView android:id="@+id/android:list"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:headerDividersEnabled="true"
            android:cacheColorHint="#00000000"/>          
 
</LinearLayout>

You have call this from you activity which extends ListActivity  using
setContentView(R.layout.mainlistview);



Then you have to create the layout which you want to put inside the listview

 as follows

save it as "agentcontact.xml" in layout folder


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content">
 
  <TableRow android:id="@+id/subAgentContactTableRow"
            android:layout_width = "fill_parent"
            android:layout_height="wrap_content"
            android:gravity="left">
 
 <TextView android:id="@+id/agentContactListTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:textSize="15px"
android:textColor="#444444"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"/>

 <TableRow android:id="@+id/subAgentContactTableRow1"
           android:layout_width = "fill_parent"
           android:layout_height="wrap_content"
           android:gravity="right"
           android:paddingRight="5px">

 <Button android:id = "@+id/agentContactButton"
         android:text = "Contact"        
              android:layout_width = "wrap_content"
         android:layout_height = "wrap_content" />              
               
 </TableRow>
   </TableRow>
</LinearLayout>


Then we have to create a Custom ArrayAdapter calss which will handle all operation on the listview.
as follows.

class IconicAdapter extends ArrayAdapter {
Activity context;

IconicAdapter(Activity context) {

super(context, R.layout.agentcontact, pocketAgentLocation);
this.context=context;
}

public View getView(int position, View convertView,ViewGroup parent) {

LayoutInflater inflater=context.getLayoutInflater();
View agntContct=inflater.inflate(R.layout.agentcontact, null);


TextView label=(TextView)agntContct.findViewById(R.id.agentContactListTextView);
label.setText(pocketAgentLocation.get(position)._title);
label.setWidth(_listTextWidth);

Button _button  = (Button)agntContct.findViewById(R.id.agentContactButton);
_button .setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

Toast.makeText(YOURCLASS.this,"Hii",Toast.LENGTH_LONG).show();
}
});
return(agntContct);
}
}

Thnaks.....

Sunday, December 12, 2010

How to attach file in email

Hi this is my first blog hope this will help you.
Thanks.............

 attaching file in email is simple here is my code.....


public void sendMail(String emailid,String mailbody){

final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
                               
emailIntent.setType("image/png");
emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://"+Environment.getExternalStorageDirectory()+"nameof your file"));
        emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{emailid});
        emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,"Transaction Details");
        emailIntent.putExtra(android.content.Intent.EXTRA_TEXT,mailbody);
    
        try {
        this.startActivity(Intent.createChooser(emailIntent, "Send mail..."));
        } catch (android.content.ActivityNotFoundException ex) {
      
        Toast.makeText(this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
        }

}