0 comments

Android change language

In android you can change language, textview or image etc, by this tutorial you will learn how to change the language, first you have to create ChangeLanguage class which contain static method call changeLanguage and changeLanguage method call in all UI class that you want to change the language.

ChangeLanguage.java
package montercodz.blogspot.com;

import java.util.Locale;

import android.content.Context;
import android.content.res.Configuration;
import android.util.Log;

public class ChangeLanguage {
 public static String ENGLISH = "en";
 public static String FRENCH = "fr";
 public static String LANGUAGE = "fr";
 
 public static void changeLanguage(Context context,String language){
  Locale localeSetting = new Locale(language);
     Locale.setDefault(localeSetting);
     Configuration configSetting = new Configuration();
     configSetting.locale = localeSetting;
     context.getResources().updateConfiguration(configSetting, null);
     Log.d("Localization.java","Language have been changed!");
 }
}

MainClass.java
package montercodz.blogspot.com;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainClass extends Activity implements OnClickListener {
 private Button btnEnglish;
 private Button btnFrench;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        btnEnglish = (Button)findViewById(R.id.btnEnglish);
        btnFrench = (Button)findViewById(R.id.btnFrench);
        
        //set Button listener
        btnEnglish.setOnClickListener(this);
        btnFrench.setOnClickListener(this);
    }
 @Override
 public void onClick(View v) {
  // TODO Auto-generated method stub
  if(btnEnglish == v){
   //set English language start
   ChangeLanguage.LANGUAGE = ChangeLanguage.ENGLISH;
   ChangeLanguage.changeLanguage(this,ChangeLanguage.LANGUAGE);
   Intent i = new Intent(getApplication(),SecodeUserInterface.class);
   startActivity(i);
  }else if(btnFrench == v){
   //set French language start
   ChangeLanguage.LANGUAGE = ChangeLanguage.FRENCH;
   ChangeLanguage.changeLanguage(this,ChangeLanguage.LANGUAGE);
   Intent i = new Intent(getApplication(),SecodeUserInterface.class);
   startActivity(i);
  }
 }
}

SecodeUserInterface.java
package montercodz.blogspot.com;

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

public class SecodeUserInterface extends Activity{
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  // TODO Auto-generated method stub
  super.onCreate(savedInstanceState);
  //set language change be fore setContentView this set use in all UI language change
  ChangeLanguage.changeLanguage(this, ChangeLanguage.LANGUAGE);
  setContentView(R.layout.second_user_interface);
 }
}


main.xml


    
     
     
    


second_user_interface.xml

    

    
    
    
    


String.xml (for english)


 Change Language Example
    Language description changes depending on language


String.xml (for French)


    Changements langage de description de fonction de la langue


to change with other language you need to change your directory name click Here for more language.

AndroidManifest.xml

    

    
        
            
                
                
            
        
        
         
                  
        

    


your directory should look like this bellow picture

this is your buttonDrawable Image

Source code you can download here

Change language Source code download
0 comments

Android Rotate Animation Example

This tutorial teach you how to use animation and set animation listener

The first you need to have rotate.xml which store in res/anim folder this the picture bellow.


rotate.xml

    


main.xml




this is your refresh drawable image copy it to your res/drawable folder

AnimationExample.java
package montercodz.blogspot.com;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

public class AnimationExample extends Activity implements OnClickListener {
 private ImageView ivRefresh;
 private Button btnStartAnimation;
 private Button btnStopAnimation;
 private Animation ani;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ivRefresh = (ImageView)findViewById(R.id.ivRefresh);
        btnStartAnimation = (Button)findViewById(R.id.btnRun);
        btnStopAnimation = (Button)findViewById(R.id.btnStop);
        btnStopAnimation.setEnabled(false);
        btnStartAnimation.setOnClickListener(this);
        btnStopAnimation.setOnClickListener(this);
    }
    
    private Animation getRotateAnimation(){
     Animation anim = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.rotate);
  anim.setInterpolator(new LinearInterpolator());
  return anim;
    }
    
 @Override
 public void onClick(View v) {
  // TODO Auto-generated method stub
  if(v == btnStartAnimation){
   btnStopAnimation.setEnabled(true);
   btnStartAnimation.setEnabled(false);
   ani = getRotateAnimation();
   ani.setAnimationListener(new AnimationListener() {
    
    @Override
    public void onAnimationStart(Animation animation) {
     // TODO Auto-generated method stub
     Toast.makeText(getApplicationContext(), "Animation Start", Toast.LENGTH_SHORT).show();
    }
    
    @Override
    public void onAnimationRepeat(Animation animation) {
     // TODO Auto-generated method stub
     Toast.makeText(getApplicationContext(), "Animation Repeat", Toast.LENGTH_SHORT).show();
    }
    
    @Override
    public void onAnimationEnd(Animation animation) {
     // TODO Auto-generated method stub
     Toast.makeText(getApplicationContext(), "Animation End", Toast.LENGTH_SHORT).show();
    }
   });
   ivRefresh.setAnimation(ani);
  }else if(v == btnStopAnimation){
   btnStartAnimation.setEnabled(true);
   btnStopAnimation.setEnabled(false);
   ivRefresh.clearAnimation();
  }
 }
}
Download source code Click here
0 comments

Android Floating button example

This example is use floating button to float over your listview

main.xml
 
 
 
   
 
     
     
 
   
 
 

FloatingViewExample.java
package montercodz.blogspot.com;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Toast;

public class FloatingViewExample extends ListActivity implements OnClickListener {
 private Button btnFloat;
 private Button btnFloat1;
 String[] mStrings = new String[] {"Item 1", "Item 2", "Item 3", "Item 4", 
 "Item 5", "Item 6", "Item 7", "Item 8", "Item 9", "Item 10", "Item 11", "Item 12"};
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        btnFloat = (Button)findViewById(R.id.floating_button);
        btnFloat1 = (Button)findViewById(R.id.floating_button1); 
        
        ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, mStrings);
        this.setListAdapter(adapter); 
        //set Floating button listener
        btnFloat.setOnClickListener(this);
        btnFloat1.setOnClickListener(this);
    }
    
 @Override
 public void onClick(View v) {
  // TODO Auto-generated method stub
  if (v == btnFloat){
   this.setSelection(0);
   Toast.makeText(getApplicationContext(), "Move to top", Toast.LENGTH_SHORT).show();
  }else if(v == btnFloat1){
   this.setSelection(mStrings.length-1);
   Toast.makeText(getApplicationContext(), "Move to buttom", Toast.LENGTH_SHORT).show();
  }
 }
}
0 comments

Android add image to spinner

This example you will learn how to add image to the spinner. so you need to have 2 xml file. one is your spinner list item, and the other one is your main.xml

item_list.xml







main.xml






PutImageToSpinnerExample.java
package example.com;

import java.util.ArrayList;

public class PutImageToSpinnerExample extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ArrayList> list = new ArrayList>();
        HashMap map = new HashMap();
        map.put("Name", "Item One");
        map.put("Icon", R.drawable.icon);
        list.add(map);

        map = new HashMap();
        map.put("Name", "Item Two");
        map.put("Icon", R.drawable.icon);
        list.add(map);

        Spinner spin = (Spinner) findViewById(R.id.spin);
        myAdapter adapter = new myAdapter(getApplicationContext(), list,
                R.layout.item_list, new String[] { "Name", "Icon" },
                new int[] { R.id.imagetext, R.id.image });

        spin.setAdapter(adapter);
    }
}

class myAdapter extends SimpleAdapter {
	
	private Context mcontext;
    public myAdapter(Context context, List> data,
            int resource, String[] from, int[] to) {
        super(context, data, resource, from, to);
        
        mcontext = context;

    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
    	LayoutInflater object = (LayoutInflater)mcontext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    	   if (convertView == null) {
               convertView = object.inflate(R.layout.item_list,
                       null);
           }

           HashMap data = (HashMap) getItem(position);

           ((TextView) convertView.findViewById(R.id.imagetext))
                   .setText((String) data.get("Name"));
           ((ImageView) convertView.findViewById(R.id.image))
                   .setImageResource((Integer) data.get("Icon"));

           return convertView;
    }

}
Download source code Click here
0 comments

Android ExifInterface Example

ExifInterface class you to get metadata of your image. for example it get image taken date, Location you take the picture, image orientation etc

exifFile.java
import java.io.File;
import java.io.IOException;

import android.app.Activity;
import android.database.Cursor;
import android.media.ExifInterface;
import android.net.Uri;
import android.provider.MediaStore;

public class MyExif {

private File exifFile;    //It's the file passed from constructor
private String exifFilePath;  //file in Real Path format
private Activity parentActivity; //Parent Activity

private String exifFilePath_withoutext;
private String ext;

private ExifInterface exifInterface;
private Boolean exifValid = false;;

//Exif TAG
//for API Level 8, Android 2.2
private String exif_DATETIME = "";
private String exif_FLASH = "";
private String exif_FOCAL_LENGTH = "";
private String exif_GPS_DATESTAMP = "";
private String exif_GPS_LATITUDE = "";
private String exif_GPS_LATITUDE_REF = "";
private String exif_GPS_LONGITUDE = "";
private String exif_GPS_LONGITUDE_REF = "";
private String exif_GPS_PROCESSING_METHOD = "";
private String exif_GPS_TIMESTAMP = "";
private String exif_IMAGE_LENGTH = "";
private String exif_IMAGE_WIDTH = "";
private String exif_MAKE = "";
private String exif_MODEL = "";
private String exif_ORIENTATION = "";
private String exif_WHITE_BALANCE = "";

//Constructor from path
MyExif(String fileString, Activity parent){
 exifFile = new File(fileString);
 parentActivity = parent;
 exifFilePath = fileString;
 PrepareExif();
}

//Constructor from URI
MyExif(Uri fileUri, Activity parent){
 exifFile = new File(fileUri.toString());
 parentActivity = parent;
 exifFilePath = getRealPathFromURI(fileUri);
 PrepareExif();
}

private void PrepareExif(){

 int dotposition= exifFilePath.lastIndexOf(".");
 exifFilePath_withoutext = exifFilePath.substring(0,dotposition);
 ext = exifFilePath.substring(dotposition + 1, exifFilePath.length());

 if (ext.equalsIgnoreCase("jpg")){
  try {
   exifInterface = new ExifInterface(exifFilePath);
   ReadExifTag();
   exifValid = true;
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
}

private void ReadExifTag(){

 exif_DATETIME = exifInterface.getAttribute(ExifInterface.TAG_DATETIME);
 exif_FLASH = exifInterface.getAttribute(ExifInterface.TAG_FLASH);
 exif_FOCAL_LENGTH = exifInterface.getAttribute(ExifInterface.TAG_FOCAL_LENGTH);
 exif_GPS_DATESTAMP = exifInterface.getAttribute(ExifInterface.TAG_GPS_DATESTAMP);
 exif_GPS_LATITUDE = exifInterface.getAttribute(ExifInterface.TAG_GPS_LATITUDE);
 exif_GPS_LATITUDE_REF = exifInterface.getAttribute(ExifInterface.TAG_GPS_LATITUDE_REF);
 exif_GPS_LONGITUDE = exifInterface.getAttribute(ExifInterface.TAG_GPS_LONGITUDE);
 exif_GPS_LONGITUDE_REF = exifInterface.getAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF);
 exif_GPS_PROCESSING_METHOD = exifInterface.getAttribute(ExifInterface.TAG_GPS_PROCESSING_METHOD);
 exif_GPS_TIMESTAMP = exifInterface.getAttribute(ExifInterface.TAG_GPS_TIMESTAMP);
 exif_IMAGE_LENGTH = exifInterface.getAttribute(ExifInterface.TAG_IMAGE_LENGTH);
 exif_IMAGE_WIDTH = exifInterface.getAttribute(ExifInterface.TAG_IMAGE_WIDTH);
 exif_MAKE = exifInterface.getAttribute(ExifInterface.TAG_MAKE);
 exif_MODEL = exifInterface.getAttribute(ExifInterface.TAG_MODEL);
 exif_ORIENTATION = exifInterface.getAttribute(ExifInterface.TAG_ORIENTATION);
 exif_WHITE_BALANCE = exifInterface.getAttribute(ExifInterface.TAG_WHITE_BALANCE);

}

private String getRealPathFromURI(Uri contentUri) {
      String[] proj = { MediaStore.Images.Media.DATA };
      Cursor cursor = parentActivity.managedQuery(contentUri, proj, null, null, null);
      int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
      cursor.moveToFirst();
      return cursor.getString(column_index);
  }

public String getSummary(){
 if(!exifValid){
  return ("Invalide EXIF!");
 }else{
  return( exifFilePath + " : \n" +

    "Name without extension: " + exifFilePath_withoutext + "\n" +
    "with extension: " + ext + "\n" +
   
    //"Date Time: " + exif_DATETIME + "\n" +
    //"Flash: " + exif_FLASH + "\n" +
    //"Focal Length: " + exif_FOCAL_LENGTH + "\n" +
    //"GPS Date Stamp: " + exif_GPS_DATESTAMP + "\n" +
    "GPS Latitude: " + exif_GPS_LATITUDE + "\n" +
    "GPS Latitute Ref: " + exif_GPS_LATITUDE_REF + "\n" +
    "GPS Longitude: " + exif_GPS_LONGITUDE + "\n" +
    "Orientation: " + exif_ORIENTATION + "\n" +
    "GPS Longitude Ref: " + exif_GPS_LONGITUDE_REF);
    //"Processing Method: " + exif_GPS_PROCESSING_METHOD + "\n" +
    //"GPS Time Stamp: " + exif_GPS_TIMESTAMP + "\n" +
    //"Image Length: " + exif_IMAGE_LENGTH + "\n" +
    //"Image Width: " + exif_IMAGE_WIDTH + "\n" +
    //"Make: " + exif_MAKE + "\n" +
    //"Model: " + exif_MODEL + "\n" +
    //"Orientation: " + exif_ORIENTATION + "\n" +
    //"White Balance: " + exif_WHITE_BALANCE + "\n");
 }
}

public void UpdateGeoTag(){
 //with dummy data
 final String DUMMY_GPS_LATITUDE = "22/1,21/1,299295/32768";
 final String DUMMY_GPS_LATITUDE_REF = "N";
 final String DUMMY_GPS_LONGITUDE = "114/1,3/1,207045/4096";
 final String DUMMY_GPS_LONGITUDE_REF = "E";

 exifInterface.setAttribute(ExifInterface.TAG_GPS_LATITUDE, DUMMY_GPS_LATITUDE);
 exifInterface.setAttribute(ExifInterface.TAG_GPS_LATITUDE_REF, DUMMY_GPS_LATITUDE_REF);
 exifInterface.setAttribute(ExifInterface.TAG_GPS_LONGITUDE, DUMMY_GPS_LONGITUDE);
 exifInterface.setAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF, DUMMY_GPS_LONGITUDE_REF);
 try {
  exifInterface.saveAttributes();
 } catch (IOException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
 }


}
}

ExifInterfaceExample.java
import java.io.File;
import java.io.FileNotFoundException;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class ExifInterfaceExample extends Activity {
 TextView textTargetUri;
 ImageView targetImage;
 Button buttonSaveImage;

 File targetFile;
 String exifAttribute;
 MyExif myExif;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button buttonLoadImage = (Button)findViewById(R.id.loadimage);
        buttonSaveImage = (Button)findViewById(R.id.saveimage);
        textTargetUri = (TextView)findViewById(R.id.targeturi);
        targetImage = (ImageView)findViewById(R.id.targetimage);
      
        buttonLoadImage.setOnClickListener(new Button.OnClickListener(){

    @Override
    public void onClick(View arg0) {
     // TODO Auto-generated method stub
     buttonSaveImage.setEnabled(false);
     Intent intent = new Intent(Intent.ACTION_PICK,
       android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
     startActivityForResult(intent, 0);
    }});
      
        buttonSaveImage.setOnClickListener(new Button.OnClickListener(){

    @Override
    public void onClick(View arg0) {
     // TODO Auto-generated method stub
     myExif.UpdateGeoTag(); //with dummy data
    }});
    }

  @Override
  protected void onActivityResult(int requestCode,
    int resultCode, Intent data) {
   // TODO Auto-generated method stub
   super.onActivityResult(requestCode, resultCode, data);

   if (resultCode == RESULT_OK){
    Uri targetUri = data.getData();
   
    Bitmap bitmap;
    try {
     bitmap = BitmapFactory.decodeStream(getContentResolver()
       .openInputStream(targetUri));
     targetImage.setImageBitmap(bitmap);
     buttonSaveImage.setEnabled(true);
    } catch (FileNotFoundException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   
    myExif = new MyExif(targetUri, this);
    textTargetUri.setText(myExif.getSummary());
   }
  }
}

main.xml



0 comments

Android how to rotate Image

This tutorial will show you how to rotate image.


main.xml

 
 


RotateImage.java
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class RotateImage extends Activity {
 private ImageView img;
 private Button btn;
 private Bitmap bmp;
 private int r = 0;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        img=(ImageView)findViewById(R.id.ImageView01);
        btn = (Button)findViewById(R.id.btn);
        bmp = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
        btn.setOnClickListener(new View.OnClickListener() {
   
   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    r += 90;
    if(r >= 360) r=0;
    setRotation(r);
   }
  });
        
    }
    
    private void setRotation(int rotate){
      // Getting width & height of the given image.
        int w = bmp.getWidth();
        int h = bmp.getHeight();
        // Setting post rotate to 90
        Matrix mtx = new Matrix();
        mtx.postRotate(rotate);
        // Rotating Bitmap
        Bitmap rotatedBMP = Bitmap.createBitmap(bmp, 0, 0, w, h, mtx, true);
        BitmapDrawable bmd = new BitmapDrawable(rotatedBMP);

        img.setImageDrawable(bmd);
    }
}
0 comments

Android flash Screen example

This tutorial show you about android flash Screen
first you need to have 2 xml file. one is for you flash screen, and the other is for your main

flash.xml





main.xml





SplashScreen.java
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;

public class SplashScreen extends Activity {
    protected boolean _active = true;
    protected int _splashTime = 5000;
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);
        
        // thread for displaying the SplashScreen
        Thread splashTread = new Thread() {
            @Override
            public void run() {
                try {
                    int waited = 0;
                    while(_active && (waited < _splashTime)) {
                        sleep(100);
                        if(_active) {
                            waited += 100;
                        }
                    }
                } catch(InterruptedException e) {
                    // do nothing
                } finally {
                    finish();
                    Intent i = new Intent(getApplicationContext(),MyApp.class);
                    startActivity(i);
                    stop();
                }
            }
        };
        splashTread.start();
    }
    
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            _active = false;
        }
        return true;
    }
}
MyApp.java
package com.droidnova.android;

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

public class MyApp extends Activity {
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

you will missing the drawable image so copy this image and pass it in your res/drawable
flash.jpg

your Manifest.xml


    
        
            
                
                
            
        
        
        
    
    

Download the source code Click here
0 comments

Android get image from gallery

This tutorial you will learn how to get image from gallery and set to imageview.

main.xml

    
     
  
    






GetImageFromGalleryExample.java
package example.com.test;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class GetImageFromGalleryExample extends Activity implements OnClickListener {
 private static final int SELECT_PICTURE = 1;
 private String selectedImagePath = null;
 private ImageView iv;
 private Button button,button1;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        button = (Button)findViewById(R.id.button1);
        button1 = (Button)findViewById(R.id.button2);
        iv = (ImageView)findViewById(R.id.iv1);
        button.setOnClickListener(this);
        button1.setOnClickListener(this);
    }
    
    public String getPath(Uri uri) {
        String[] projection = { MediaStore.Images.Media.DATA };
        Cursor cursor = managedQuery(uri, projection, null, null, null);
        int column_index = cursor
                .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    }
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
     // TODO Auto-generated method stub
     super.onActivityResult(requestCode, resultCode, data);
     if (resultCode == RESULT_OK) {
            if (requestCode == SELECT_PICTURE) {
                Uri selectedImageUri = data.getData();
                //SelectedImagePath is not use in this tutorial but you can use it when ever u want
                //selectedImagePath = getPath(selectedImageUri);
                iv.setImageURI(selectedImageUri);
            }
        }
    }

 @Override
 public void onClick(View v) {
  // TODO Auto-generated method stub
  if (v == button){
   Intent intent = new Intent();
            intent.setType("image/*");
            intent.putExtra("scale", true);
            intent.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(Intent.createChooser(intent,"Select Picture"), SELECT_PICTURE);
  }else{
   iv.setImageResource(R.drawable.icon);
  }
 }
}
1 comments

Android RadioButton Example

RadioButton is used to select any one option from the given group.

In Android by using RadioGroup only we can use RadioButton. We can add any number of RadioButtons to a single RadioGroup. we can add any number of RadioGroup to a single Layout.

main.xml


     
     
     
     
     
     
          
          
          

RadioButtonExample.java
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.TextView;

public class RadioButtonExample extends Activity implements OnClickListener {
     private RadioButton rb1;
     private RadioButton rb2;
     private Button b1;
     private TextView t1;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        rb1=(RadioButton)findViewById(R.id.option1);
        rb2=(RadioButton)findViewById(R.id.option2);
        b1=(Button)findViewById(R.id.selected);
        b1.setOnClickListener(this);
        t1=(TextView)findViewById(R.id.TextView01);
    }
     @Override
     public void onClick(View v) {
          // TODO Auto-generated method stub
          if(v == b1){
          if(rb1.isChecked() == true)
              t1.setText("Selected is : "+rb1.getText());
          if(rb2.isChecked() == true)
              t1.setText("Selected is : "+rb2.getText());
          }
     }
}
1 comments

Android get image from camera

In order to get image from camera we need to use Intent of "MediaStore.ACTION_IMAGE_CAPTURE", we can request Android build-in Camera App or other Service Provider to take picture.
main.xml



AndroidImageCapture.java
package com.AndroidImageCapture;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class AndroidImageCapture extends Activity {
  
 ImageView imageiewImageCaptured;
  
   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);
       Button buttonImageCapture = (Button)findViewById(R.id.captureimage);
       imageiewImageCaptured = (ImageView)findViewById(R.id.imagecaptured);
       
       buttonImageCapture.setOnClickListener(buttonImageCaptureOnClickListener);
   }
   
   Button.OnClickListener buttonImageCaptureOnClickListener
   = new Button.OnClickListener(){
  @Override
  public void onClick(View arg0) {
   // TODO Auto-generated method stub
   Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
   startActivityForResult(intent, 0);
    
  }};
 @Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  // TODO Auto-generated method stub
  super.onActivityResult(requestCode, resultCode, data);
   
  if (resultCode == RESULT_OK)
  {
   Bundle extras = data.getExtras();
   Bitmap bmp = (Bitmap) extras.get("data");
   imageiewImageCaptured.setImageBitmap(bmp); 
  }
   
 }
}

Manifest.xml
//Add permission for your camera