add camera permission between…. tag.
We will use camera of the android device to scan the barcode.
For using camera, we have to get permission from the user.
Step 4: Updating activity_main.xml file:
Copy following code into android_main.xml file:
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.exampledemo.parsaniahardik.scanbarcodeqrdemonuts.MainActivity">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn"
android:text="Scan Barcode or QR code" />
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tvresult"
android:textColor="#000"
android:layout_marginTop="10dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Result will be here"/>
I have taken one button and one textview.
When the user will click the button, camera preview will be opened and you can scan barcode or QRCode from here.
After successful scanning, system will enter the value of the barcode in the textview.
Step 5: Updating MainActivity.java file:
Update MainActivity.java as per below code:
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
public static TextView tvresult;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvresult = (TextView) findViewById(R.id.tvresult);
Button btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, ScanActivity.class);
startActivity(intent);
}
});
}
}
Here we will open ScanActivity on the button click.