How to ZXING Barcode Scanner not full screen only half screen

I want create application Scan Barcode using ZXING Barcode Scanner

Like Blackberry Messenger

enter image description here

This is my code "MainActivity.java"

package com.example.ridwan.myapplication;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;

import info.vividcode.android.zxing.CaptureActivity;
import info.vividcode.android.zxing.CaptureActivityIntents;

public class MainActivity extends AppCompatActivity  {

    private TextView tvScanResult;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Intent captureIntent = new Intent(MainActivity.this, CaptureActivity.class);
        CaptureActivityIntents.setPromptMessage(captureIntent, "Barcode scanning...");
        startActivityForResult(captureIntent, 0);

        tvScanResult = (TextView) findViewById(R.id.tv_scanresult);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 0) {
            if (resultCode == Activity.RESULT_OK && data != null) {
                String value = data.getStringExtra("SCAN_RESULT");
                tvScanResult.setText(value);
            } else if (resultCode == Activity.RESULT_CANCELED) {
                tvScanResult.setText("Scanning Gagal, mohon coba lagi.");
            }
        } else {

        }
        super.onActivityResult(requestCode, resultCode, data);
    }


}

Then this is my "activity_main.xml"

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    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.example.ridwan.myapplication.MainActivity"
    tools:showIn="@layout/activity_main">

    <TextView
        android:layout_marginTop="50dp"
        android:id="@+id/tv_scanresult_title"
        android:layout_centerHorizontal="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Result Scan : " />

    <TextView
        android:layout_below="@id/tv_scanresult_title"
        android:id="@+id/tv_scanresult"
        android:layout_centerHorizontal="true"
        android:layout_width="wrap_content"
        android:textColor="#ff1493"
        android:layout_height="wrap_content"
        android:text="_" />

</RelativeLayout>

Can you give me solution ? i want to barcode in fragment.


I have achieved the same effect/UI you are looking for by using ZXing Android Embedded. Very straightforward to implement - and it also includes a torch functionality.


Step 1:

Add This Libray in Gradle in Dependancy

implementation 'com.google.zxing:core:3.2.1'
implementation 'com.journeyapps:zxing-android-embedded:3.2.0@aar'

Step 2:

BarcodeActivity.java

    public class BarcodeActivity extends AppCompatActivity {


       private EditText editTextProductId;
       private Button buttonGenerate, buttonScan;
       private ImageView imageViewResult;

       @Override
       protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_barcode);
          initView();
       }

       private void initView() {
           editTextProductId = findViewById(R.id.editTextProductId);
           imageViewResult = findViewById(R.id.imageViewResult);
           buttonGenerate = findViewById(R.id.buttonGenerate);

           buttonGenerate.setOnClickListener(new View.OnClickListener() {
               @Override
               public void onClick(View view) {
                   buttonGenerate_onClick(view);
               }
           });
           buttonScan = findViewById(R.id.buttonScan);
           buttonScan.setOnClickListener(new View.OnClickListener() {
               @Override
               public void onClick(View view) {
                   buttonScan_onClick(view);
               }
           });
       }

       private void buttonGenerate_onClick(View view) {
           try {
               String productId = editTextProductId.getText().toString();
               Hashtable<EncodeHintType, ErrorCorrectionLevel> hintMap = new Hashtable<EncodeHintType, ErrorCorrectionLevel>();
               hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
               Writer codeWriter;
               codeWriter = new Code128Writer();
               BitMatrix byteMatrix = codeWriter.encode(productId, BarcodeFormat.CODE_128,400, 200, hintMap);
               int width = byteMatrix.getWidth();
               int height = byteMatrix.getHeight();
               Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
               for (int i = 0; i < width; i++) {
                   for (int j = 0; j < height; j++) {
                       bitmap.setPixel(i, j, byteMatrix.get(i, j) ? Color.BLACK : Color.WHITE);
                   }
               }
               imageViewResult.setImageBitmap(bitmap);
           } catch (Exception e) {
               Toast.makeText(getApplicationContext(), e.getMessage(), 
               Toast.LENGTH_LONG).show();
           }
       }

       private void buttonScan_onClick(View view) {
           IntentIntegrator intentIntegrator = new IntentIntegrator(this);
   intentIntegrator.setDesiredBarcodeFormats(IntentIntegrator.ALL_CODE_TYPES);
           intentIntegrator.setCameraId(0);
           intentIntegrator.initiateScan();
       }

       @Override
       protected void onActivityResult(int requestCode, int resultCode, Intent data) {
           IntentResult intentResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
           if (intentResult != null) {
               String productId = intentResult.getContents();
               Toast.makeText(getApplicationContext(), productId, Toast.LENGTH_LONG).show();
           }
       }

    }

Step 3:

activity_barcode.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    android:orientation="vertical"
    tools:ignore="HardcodedText">

<EditText
    android:id="@+id/editTextProductId"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10"
    android:hint="Product Id"
    android:inputType="textPersonName" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <Button
        android:id="@+id/buttonGenerate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Generate Barcode" />

    <Button
        android:id="@+id/buttonScan"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Scan Barcode" />
</LinearLayout>

<ImageView
    android:id="@+id/imageViewResult"
    android:layout_width="match_parent"
    android:layout_height="335dp" />


</LinearLayout>