View

Saturday 2 June 2012

Dial Application

How to USE Dial the Number in Android

Its a simple default Android api to use the Dial the number .
startActivity(new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + num_et.getText())));

Here i used 1 Edit text for typing the Number and 1 button to make a cal options.
Button cal_btn;
EditText num_et;
public void onClick(View v) {
   
if(num_et!=null ){
startActivity(new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + num_et.getText()))); 
     }
   
   }
  });
Dont forgot to give the cal permission like:
<uses-permission android:name="android.permission.CALL_PHONE" />

Thursday 17 May 2012

How to use Logcat Backup file in SD card

I used sample application to Backup the Logcat file in SD card.First it create the Sample_log_file.txt in the SD card Location and then we give  write to read the logcat file command like -d -f

The following code is
try {
          File filename = new File(Environment.getExternalStorageDirectory()+"/Sample_log_file.txt");
          filename.createNewFile();
          String cmd = "logcat -d -f "+filename.getAbsolutePath();
          Runtime.getRuntime().exec(cmd);
       } catch (IOException e) {
          e.printStackTrace();
        }
You must give the permission in AndroidManifest.xml
 <uses-permission android:name="android.permission.READ_LOGS"/>
 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

Go and check the file in DDMS->open File Explorer->open Mnt(management)->SD Card->sample_log_file.txt
  



             /    **Log file shown in the word**/



Saturday 5 May 2012

Notification

How to use Notification in Android?


Android represent the Notifications by using Notification class.We have to create a notification by using NotificationManager class,it can be received from the Activity via getSystemService() method.

Eg: NotificationManager notificationManager = (NotificationManager)getSystemService (NOTIFICATION_SERVICE);


Here i use two edit text for giving the email and message  and one button for create a notification.
Main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <EditText
        android:id="@+id/username_et"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="10dp"
    android:inputType="text"/>
    <EditText
        android:id="@+id/mess_et"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="5dp"
        android:inputType="text" />
    <Button
        android:id="@+id/button1"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:onClick="createNotification"
        android:text="Create Notification" android:layout_gravity="center_horizontal"/>
   
</LinearLayout>

Tuesday 1 May 2012

How to use Image can convert into ByteArray

How to Image can convert into ByteArray
Here I used barcode image can convert into Byte Array and the Byte Array can reconvert into the original form.I used two Buttons,textview for values and one static image then ByteArrayOutputStream for conversion.
Here java code ,The class name as ImageConversion.java

public class ImageConvertScreen extends Activity implements OnClickListener {
    /** Called when the activity is first created. */
  Button img_byte,byte_img;
  ImageView image;
  TextView value;
  public ByteArrayOutputStream bos;
  public Bitmap bm;
  public byte[] bitmapdata;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);      
        image = (ImageView) findViewById(R.id.img_convert);
        value=(TextView)findViewById(R.id.convert_txt);
        img_byte =(Button)findViewById(R.id.img_byte);
        byte_img =(Button)findViewById(R.id.byte_img);
        img_byte.setOnClickListener(this);
        byte_img.setOnClickListener(this);
     
     
       bm = BitmapFactory.decodeResource(getResources(),R.drawable.barcode);
       bos = new ByteArrayOutputStream();
       bm.compress(Bitmap.CompressFormat.JPEG, 40 , bos);
      
      
    }
    public void onClick(View v) {
  if (v == img_byte) { 
   bitmapdata = bos.toByteArray();
      Log.w("Image Conversion", String.valueOf(bitmapdata.length));
      String converted_txt="";
         for (int i = 0; i < bitmapdata.length; i++) {
       Log.w("Image Conversion", String.valueOf(bitmapdata[i]));
          converted_txt=converted_txt+bitmapdata[i];
         }
         value.setText(converted_txt);
         value.setVisibility(View.VISIBLE);
         image.setVisibility(View.INVISIBLE);
  
  } else if (v==byte_img){
    bm = BitmapFactory.decodeByteArray(bitmapdata , 0, bitmapdata .length);
        image.setImageBitmap(bm);
        image.setVisibility(View.VISIBLE);
        value.setText("");
        value.setVisibility(View.INVISIBLE);
        Log.w("Image Conversion", "converted");

  }
    }
   
}


Thursday 8 September 2011

Phone internal and External storage usage

How to check Phone internal/external memory usage using programmatically in android

Android have a default API to check the phone internal and external memory usage.In My application
i used both as well as memory informations.The internal and external both are same,just to change
the Environment path.
Internal=StatFs stat=new StatFs(Environment.getDataDirectory().getPath());
External=StatFs stat=new StatFs(Environment.getExternalStorageDirectory().getPath());
The user can just hit the seek bar its show the occupied space in bytes,availabel space in MB,
Before that first to create new emulatore with size can set it in Kb or MB.I used 24MB.
public class Internal_Storage extends Activity  implements SeekBar.OnSeekBarChangeListener{
 SeekBar sb;
 int progress;
 Boolean value=false;
 Button press_btn;
 TextView availabel_in_tv,occupied_in_tv,tv;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.internal_storage);
        availabel_in_tv=(TextView)findViewById(R.id.availabel_in_tv);
        occupied_in_tv=(TextView)findViewById(R.id.occupied_in_tv);
        final TextView reading = (TextView) findViewById(R.id.textView1);
        sb = (SeekBar)findViewById(R.id.sb);
        sb.setMax(100);
    @Override
    public void onProgressChanged(SeekBar seekBar, int size, boolean fromtouch) {
        reading.setText("Loading "+size+"% ");
        method1();
       
        }
    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
       
        }
    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
    }
  });
    }
    @Override
    public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {
    }
 @Override
 public void onStartTrackingTouch(SeekBar seekBar) {
  // TODO Auto-generated method stub

 }
 @Override
 public void onStopTrackingTouch(SeekBar seekBar) {
  // TODO Auto-generated method stub
 }
public String method1(){
StatFs stat=new StatFs(Environment.getDataDirectory().getPath()); //**ITS show  phone internal  storage size**//
       or
StatFs stat=new StatFs(Environment.getExternalStorageDirectory().getPath());  //**ITS show  phone SD card storage size**//

    int blockSize = stat.getBlockSize();
    int availableBlocks = stat.getAvailableBlocks();     
    System.out.println("statFs:"+"=" +stat);
    System.out.println("SPACE AVAILABEL :"+"=" +availableBlocks);
     //occup_size_tv.setTextSize(availableBlocks);
     occupied_in_tv.setText(Integer.toString(availableBlocks));
     System.out.println("SIZE OCCUPY:"+"=" +blockSize);
  
     availabel_in_tv.setText((Formatter.formatFileSize(this,availableBlocks*blockSize)));
     System.out.println("FREE SPACE:="+Formatter.formatFileSize(this,availableBlocks*blockSize));
     System.out.println("FREE SPACE IN BYTES:"+availableBlocks +1042.f*1024.f);
     // bytesAvailable / (1024.f * 1024.f);
     return Formatter.formatShortFileSize(this, availableBlocks * blockSize);


 }
the same code repeated for internal and external.class
Then next class Memory_Information.class,its show total memory usage details
public class Memory_Information extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.memory_information);   
        TextView CPUinfo = (TextView) findViewById(R.id.textView1);
        CPUinfo.setText(ReadCPUinfo());    
    }
    private String ReadCPUinfo()
    {
     ProcessBuilder cmd;
   
     StringBuffer strMemory = new StringBuffer();
     //final ActivityManager activityManager =(ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
   
     ActivityManager actvityManager = (ActivityManager) this.getSystemService( ACTIVITY_SERVICE );
     ActivityManager.MemoryInfo mInfo = new ActivityManager.MemoryInfo ();
     actvityManager.getMemoryInfo( mInfo );
   
     strMemory.append("Available Memory : ");
     strMemory.append(mInfo.availMem);
     strMemory.append("\n");
     strMemory.append("\n");
   
     String result=strMemory.toString();
   
     try{
      String[] args = {"/system/bin/cat", "/proc/meminfo"};
      cmd = new ProcessBuilder(args);
   
      Process process = cmd.start();
      InputStream in = process.getInputStream();
      byte[] re = new byte[1024];
      while(in.read(re) != -1){
       System.out.println(new String(re));
       result = result + new String(re);
      }
      in.close();
     } catch(IOException ex){
      ex.printStackTrace();
     }
     return result;
    }
}
you may check in console also or u can clarify the usage in phones settings. go to settings/SD card & phone storage/


How to check Phone internal/external memory usage using programmatically in android

Android have a default API to check the phone internal and external memory usage.In My application
i used both as well as memory informations.The internal and external both are same,just to change
the Environment path.
Internal=StatFs stat=new StatFs(Environment.getDataDirectory().getPath());
External=StatFs stat=new StatFs(Environment.getExternalStorageDirectory().getPath());
The user can just hit the seek bar its show the occupied space in bytes,availabel space in MB,
Before that first to create new emulatore with size can set it in Kb or MB.I used 24MB.
public class Internal_Storage extends Activity  implements SeekBar.OnSeekBarChangeListener{
 SeekBar sb;
 int progress;
 Boolean value=false;
 Button press_btn;
 TextView availabel_in_tv,occupied_in_tv,tv;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.internal_storage);
        availabel_in_tv=(TextView)findViewById(R.id.availabel_in_tv);
        occupied_in_tv=(TextView)findViewById(R.id.occupied_in_tv);
        final TextView reading = (TextView) findViewById(R.id.textView1);
        sb = (SeekBar)findViewById(R.id.sb);
        sb.setMax(100);
    @Override
    public void onProgressChanged(SeekBar seekBar, int size, boolean fromtouch) {
        reading.setText("Loading "+size+"% ");
        method1();
       
        }
    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
       
        }
    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
    }
  });
    }
    @Override
    public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {
    }
 @Override
 public void onStartTrackingTouch(SeekBar seekBar) {
  // TODO Auto-generated method stub

 }
 @Override
 public void onStopTrackingTouch(SeekBar seekBar) {
  // TODO Auto-generated method stub
 }
public String method1(){
StatFs stat=new StatFs(Environment.getDataDirectory().getPath()); //**ITS show  phone internal  storage size**//
       or
StatFs stat=new StatFs(Environment.getExternalStorageDirectory().getPath());  //**ITS show  phone SD card storage size**//

    int blockSize = stat.getBlockSize();
    int availableBlocks = stat.getAvailableBlocks();     
    System.out.println("statFs:"+"=" +stat);
    System.out.println("SPACE AVAILABEL :"+"=" +availableBlocks);
     //occup_size_tv.setTextSize(availableBlocks);
     occupied_in_tv.setText(Integer.toString(availableBlocks));
     System.out.println("SIZE OCCUPY:"+"=" +blockSize);
  
     availabel_in_tv.setText((Formatter.formatFileSize(this,availableBlocks*blockSize)));
     System.out.println("FREE SPACE:="+Formatter.formatFileSize(this,availableBlocks*blockSize));
     System.out.println("FREE SPACE IN BYTES:"+availableBlocks +1042.f*1024.f);
     // bytesAvailable / (1024.f * 1024.f);
     return Formatter.formatShortFileSize(this, availableBlocks * blockSize);


 }
the same code repeated for internal and external.class
Then next class Memory_Information.class,its show total memory usage details
public class Memory_Information extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.memory_information);   
        TextView CPUinfo = (TextView) findViewById(R.id.textView1);
        CPUinfo.setText(ReadCPUinfo());    
    }
    private String ReadCPUinfo()
    {
     ProcessBuilder cmd;
   
     StringBuffer strMemory = new StringBuffer();
     //final ActivityManager activityManager =(ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
   
     ActivityManager actvityManager = (ActivityManager) this.getSystemService( ACTIVITY_SERVICE );
     ActivityManager.MemoryInfo mInfo = new ActivityManager.MemoryInfo ();
     actvityManager.getMemoryInfo( mInfo );
   
     strMemory.append("Available Memory : ");
     strMemory.append(mInfo.availMem);
     strMemory.append("\n");
     strMemory.append("\n");
   
     String result=strMemory.toString();
   
     try{
      String[] args = {"/system/bin/cat", "/proc/meminfo"};
      cmd = new ProcessBuilder(args);
   
      Process process = cmd.start();
      InputStream in = process.getInputStream();
      byte[] re = new byte[1024];
      while(in.read(re) != -1){
       System.out.println(new String(re));
       result = result + new String(re);
      }
      in.close();
     } catch(IOException ex){
      ex.printStackTrace();
     }
     return result;
    }
}
you may check in console also or u can clarify the usage in phones settings. go to settings/SD card & phone storage/
 
  



Wednesday 17 August 2011

How to create contextmenu in android

creating a context menu is very simple,Here i use Textview name as press me on long,when the textview is pressed for long,its open a context menu.In that context menu i use New,edit,copy,paste and delete.
Inside of the onCreate Method,we have to registerForcontextmenu(controls name)
Example: press=(TextView)findViewById(R.id.press_tv);
               registerForContextMenu(press); 
Then we  need to override the onCreateContextMenu method to create the menu:
 @Override 
   public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {  
  super.onCreateContextMenu(menu, v, menuInfo);  
   menu.setHeaderTitle("Context Menu");  
   menu.add(0, v.getId(), 0, "New");  
   menu.add(0, v.getId(), 0, "Edit");  
   menu.add(0, v.getId(), 0, "copy");  
   menu.add(0, v.getId(), 0, "Paste");  
   menu.add(0, v.getId(), 0, "Delete");  
when the item is selected,that items action can perform here,using onContextItemSelected method.
Here i use function for only "New" just passing the Intent,If u want some functions mean, you write a else if () and perform some more actions there.
 @Override 
  public boolean onContextItemSelected(MenuItem item) {  
   if(item.getTitle()=="New"){
    Intent np=new Intent(getApplicationContext(),NewPage.class);
    startActivity(np);
    function1(item.getItemId());}  
   else if(item.getTitle()=="Save"){
    //Some funtion perform here//
    function2(item.getItemId());}  
    else {return false;}  
  return true;  
 }
create a method for function1.... function n,
private void function2(int itemId) {
  // TODO Auto-generated method stub
 
 }
 private void function1(int itemId) {
  // TODO Auto-generated method stub
 
 } 
Result is:
           

pic1

pic2

 
  

pic3

pic4
Download full source code 

Friday 5 August 2011

How to create FeedBack Form inside of the customDialogBox?

How to create a form in dialog box,Here i use sample feed back form inside of the custom dialogbox,First u have to create a button in main.xml file,Because when the button is press ,FeedBack Form can open in customdialogbox,so thats why i had put a Button(Press Dialog).After that you also create a new xml file name as dialog.xml,In that xml ,i use Name,Mail,Comment and two buttons like send and cancel.
code for dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:orientation="vertical" android:layout_height="wrap_content">
    <LinearLayout android:id="@+id/linearLayout1" android:layout_height="wrap_content" android:layout_width="match_parent">
        <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Name" android:textSize="15dp" android:layout_marginLeft="10dp" android:layout_marginTop="5dp"></TextView>
        <EditText android:id="@+id/editText1" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:layout_width="200dp" android:layout_marginLeft="16dp"></EditText>
    </LinearLayout>
    <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/linearLayout2">
        <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Mail Id:" android:layout_marginTop="5dp" android:layout_marginLeft="5dp"></TextView>
        <EditText android:id="@+id/editText2" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:layout_width="200dp" android:layout_marginLeft="16dp"></EditText>
    </LinearLayout>
    <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/linearLayout3">
        <TextView android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Comment" android:layout_marginLeft="5dp" android:layout_marginTop="5dp"></TextView>
        <EditText android:id="@+id/editText3" android:layout_height="100dp" android:layout_width="200dp"></EditText>
    </LinearLayout>
    <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/linearLayout4">
        <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Send" android:layout_marginLeft="70dp"></Button>
        <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Cancel"></Button>
    </LinearLayout>
</LinearLayout>
Then i have to create a main class name as FeedbackFormusingInsideDialogBox.java
static final int CUSTOM_DIALOG_ID = 0;
Button customDialog_Dismiss;
The class have to declare as a
its a very few line code to creat a dialogbox.Just button press click listener,Dialog onCreateDialog and cancel button listener.
here i put full code .just check it

public class FeedbackFormusingInsideDialogBox extends Activity {
 static final int CUSTOM_DIALOG_ID = 0;
 Button customDialog_Dismiss;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);      
        Button buttonStartDialog = (Button)findViewById(R.id.startdialog);
        buttonStartDialog.setOnClickListener(new Button.OnClickListener(){
   @Override
   public void onClick(View arg0) {
    // TODO Auto-generated method stub
    showDialog(CUSTOM_DIALOG_ID);
   }
        });
              
    }     
    private Button.OnClickListener customDialog_DismissOnClickListener  = new Button.OnClickListener(){ 
  @Override
  public void onClick(View arg0) {
   // TODO Auto-generated method stub
   dismissDialog(CUSTOM_DIALOG_ID);
  }    
    };   
 @Override
 protected Dialog onCreateDialog(int id) {
  // TODO Auto-generated method stub
  Dialog dialog = null;;
     switch(id) {
     case CUSTOM_DIALOG_ID:
      dialog = new Dialog(this);
      dialog.setContentView(R.layout.dialogbox);
      dialog.setTitle("Feed Back Form");
             customDialog_Dismiss = (Button)dialog.findViewById(R.id.button2);
             customDialog_Dismiss.setOnClickListener(customDialog_DismissOnClickListener);
         break;
     }
     return dialog;
 } 
 }
Result :