Sunday, March 5, 2017

Use camera in Android

Camera is a useful and frequently used feature in Android applications. So here is the simple way to implement feature of camera in your Android application.

1. Manifest permission: 
     
     Provide following uses-feature to use camera in your app.
  
    <uses-feature android:name="android.hardware.camera"     
     android:required="true" />
2. Button to open camera:
    
    Just take a button in your activity_main.xml layout file. So on click of that of button we can open         camera. And an Image View to display image.

3. Now do some stuff in your MainActivity.Java or any other file where you want to implement.

   public class MainActivity extends AppCompatActivity {
    static final int REQUEST_IMAGE_CAPTURE = 1;
    private ImageView mImageView;
    private Button mCam;

       @Override    
       protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        mImageView = (ImageView) findViewById(R.id.image);
        mCam = (Button) findViewById(R.id.cam);

        mCam.setOnClickListener(new View.OnClickListener() {
            @Override            
             public void onClick(View view) {
                dispatchTakePictureIntent();
            }
        });
    }


    private void dispatchTakePictureIntent() {
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
            startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
        }
    }


    @Override   
     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
            Bundle extras = data.getExtras();
            Bitmap imageBitmap = (Bitmap) extras.get("data");
            mImageView.setImageBitmap(imageBitmap);
        }
    }
    
} 


You can see the result below:


That's it...... Thanks

Sunday, May 24, 2015

EditText in Android

Hi, I am going to explain the use of EditText in android. EditText is basically a small android UI element which can get the input from the user, So it is used to get the input from the user to the application. For example: If I want to login to the application then I need EditText to provide username and Password etc.












             Fig: showing EditText used in UI   .

Edit Text is most Important UI element and used frequently in almost every application. So every developer should know the use of EditText in android.

Declare EditText in XML:

 
1. id => used to get the reference in Java class.
2. width: how wide your EditText will be.
3  Height: how much will be the height of your edittext.
4. Hint: this will show that what to write in this edittext.


Use EditText in Java Class:







Properties of EditText:

   1. Id:  This is the key of EditText with the help of which we can access EditText in our Java class.
           
                    use     =>    android:id="@+id/editText1"

   2. Width: This property will declare that what will be the width of Edit text in UI. We can set width                     in three ways
                     * wrap_content   -     Width will be according to the amount of text written.
                     * match_parent   -     width will be equal to the width of parent layout.
                     * fixed                -      width will be fixed ex: 20 dp, 30 dp etc.
   
                 use   =>      android:layout_width="wrap_content"
                                                             OR
                                    android:layout_width="match_parent"
                                                              OR
                                    android:layout_width="50dp"

  3. Height: This property will declare that what will be the height of Edit text in UI. The height can                        be declared same as width.

                use   =>      android:layout_height="wrap_content"
                                                             OR
                                    android:layout_height="match_parent"
                                                              OR
                                    android:layout_height="50dp"

 4. Hint: This light colored text is basically an indication of what to type in this EditText. This text ill                erased while start typing.

               use   =>      android:hint="username"

 5. Style: This property is used to set the text style of EditText. This can be bold or italic

                                 android:textStyle="bold"
                                                    OR
                                 android:textStyle="italic"

6. Text color:  This property is used to change the color of Text typed in EditText.

                                android:textColor="#ffffff"
                                            OR
                                android:textColor="@android:color/white"

7. Background: This property is used to change the background of EditText. you can apply color for                             background or an Image or custom drawable. 

                              android:background="#b4b4b4"
                                                    OR           
                              android:background="@drawable/ic_launcher"



This is all about the basics of EditText. For more information you can use this link.

Thanks!!!!!!!!!!!!!!!!