Welcome back readers,
Today we will see how we can open the mobile phone
camera through Android studio app that we will build today So let’s begin
Step 1 - Create Empty Project if you don't know how to create empty project go and check it out https://prathameshbusa.blogspot.com/2021/05/hello-world-in-android-studio.html and follow steps from 1 to 5
Step 2 - Now let’s switch to XML File and try to
understand code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:id="@+id/cam"
android:layout_width="350dp"
android:layout_height="150dp"
android:text="CAMERA"
android:textSize="30dp"
android:layout_marginLeft="30dp"
android:layout_marginTop="200dp"
app:icon="@android:drawable/ic_menu_camera"
app:iconPadding="10dp"
app:iconSize="150dp"
/>
</LinearLayout>
- As you can see here we have use Linear Layout by
default it would be constraintlayout.widget.ConstraintLayout you
need to change it with LinearLayout and keep rest of section as it
is
- Now we will insert one Button on which we can
click and open the camera the attributes used in button are id which we
will require to identify button in java code its similar to roll
number that student have to identify
- Next is height and width which
you can set according to your needs
- Next is text used to print some text on button and
text size used to set size
- Further I have used margin Left and Top
which will leave space from Left and Top respectively
- Now we are inserting the image of camera from the
icon attribute which have some inbuild images and then we set
padding and icon size as per requirement
Well done half of the work is
over .I hope its easy to understand if not please drop out your
suggestion in comment box
Step 4 - Now Switch to Java
Code and lets try to understand the code
package com.example.camera;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
public class MainActivity extends
AppCompatActivity {
Button Camera;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Camera=findViewById(R.id.cam);
Camera.setOnClickListener(new View.OnClickListener() {
@Override
public
void onClick(View v) {
Intent i= new
Intent();
i.setAction(MediaStore.ACTION_VIDEO_CAPTURE);
startActivity(i);
}
});
}
}
- First line of Code package com.example.camera is nothing but your project named package and further this are all the library imported in our app in which we don’t need to write it some are present by default and some can be imported by hitting alt+enter while coding
- Now lets declare Button Camera in Main class and attach the id of button to this Camera button variable its like Calling the student by roll number here Camera is Student and Id is roll number
- Now we will write on click listener method for action that has to be performed when button is clicked to do so we write variable name followed by method name no need to write all the thing just we need to select from suggestion
- Intent i =new Intent(); this line will create intent which provides information of available components provided by the system that is to be invoked.
- i.setAction(MediaStore.ACTION_VIDEO_CAPTURE); This will perform Action of Video Capture and in last we will start this intent Now lets run the app
