Delay to read barcodes after starting camera in Zxing? - java

I'm facing a problem with the barcode scanner from zxing library for android. Actually, the scanner works fine, but sometimes it's so fast that the user doesn't have time to position the camera so it gets all the barcode, and then the barcode is read partially.I want to know if there is a way to, after starting the camera,have a delay until the first reading. Here is a piece of my code:
public class Produto extends AppCompatActivity {
private IntentIntegrator qrScan;
private Button btnCodBarras;
private Button btnEnviar;
private Button btnExcluir;
private EditText nomeCliente;
private ListView listaProdutos;
private ArrayList<String> produtos = new ArrayList<>();
private MyCustomAdapter adapter;
private SharedPreferences settings;
private String nome;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_produto);
configScan();
btnCodBarras = (Button) findViewById(R.id.btnCodBarras);
btnEnviar = (Button) findViewById(R.id.btnEnviar);
listaProdutos = (ListView) findViewById(R.id.listaProdutos);
nomeCliente = (EditText) findViewById(R.id.cliente);
adapter=new MyCustomAdapter(produtos,getApplicationContext(),listaProdutos);
listaProdutos.setAdapter(adapter);
btnCodBarras.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
qrScan.initiateScan();
}
});
I'm just calling via intent. And I can't seem to be able to modify IntentIntegrator.java and the other zxing files.
Any ideas?

Related

Why the button added dynamically does not receive the input?

I was developing an application that It is a simple game of cards and I added a button, in a dynamic way at the view, which represents a card of the game, but this button doesn't receive any input when It is pressed.
The question is: Why the button does not receive any input?
I've already tried to use the Log to print the message but nothing is printed.
public class MainActivity extends AppCompatActivity {
private Banco banco;
private Giocatore giocatore1;
private Giocatore giocatore2;
private Giocatore giocatoreAttuale;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
giocatore1 = new GiocatoreUmano();
giocatore2 = new GiocatoreUmano();
banco = new Banco(giocatore1,giocatore2);
giocatoreAttuale=giocatore1;
DaiCarteAiGiocatori();
DaiCarteAlPlayerOne();
}
private void DaiCarteAlPlayerOne(){
Button primaCarta = new Button(this); //BUTTON OF THE CARD
primaCarta.setText(giocatore1.getCartaByIndex(0).getSeme().toString()+":"+giocatore1.getCartaByIndex(0).getValore());
ConstraintLayout layout = findViewById(R.id.baseLayout);
layout.addView(primaCarta);
Animation animation = AnimationUtils.loadAnimation(this, R.anim.move_cards); // THE ANIMATION
primaCarta.startAnimation(animation);
//when the button is pressed the log should print the message of the card
primaCarta.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
banco.giocaCarte(giocatore1,giocatore1.getCartaByIndex(0));
Log.i("CARTA","Carta Premuta");
}
});
}
private void DaiCarteAiGiocatori(){
banco.pescaCarte();
}
}
Thanks for the help.
see my changes in three places commented by //!!!
public class MainActivity extends AppCompatActivity implements View.OnClickListener { //!!!
private Banco banco;
private Giocatore giocatore1;
private Giocatore giocatore2;
private Giocatore giocatoreAttuale;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
giocatore1 = new GiocatoreUmano();
giocatore2 = new GiocatoreUmano();
banco = new Banco(giocatore1,giocatore2);
giocatoreAttuale=giocatore1;
DaiCarteAiGiocatori();
DaiCarteAlPlayerOne();
}
private void DaiCarteAlPlayerOne(){
Button primaCarta = new Button(this); //BUTTON OF THE CARD
primaCarta.setOnClickListener(MainActivity.this); //!!!
primaCarta.setText(giocatore1.getCartaByIndex(0).getSeme().toString()+":"+giocatore1.getCartaByIndex(0).getValore());
ConstraintLayout layout = findViewById(R.id.baseLayout);
layout.addView(primaCarta);
Animation animation = AnimationUtils.loadAnimation(this, R.anim.move_cards); // THE ANIMATION
primaCarta.startAnimation(animation);
}
//!!!
#Override
public void onClick(View v) {
banco.giocaCarte(giocatore1,giocatore1.getCartaByIndex(0));
Log.i("CARTA","Carta Premuta");
}
private void DaiCarteAiGiocatori(){
banco.pescaCarte();
}

Try to check two values if there are equal

i'm trying to check to values . The first value i get from a QR Code, the second is a String which i have added to my source code. For the QR function iam using the ZXingScannerView Library. Here is my code, but unfortunately my if is going always to else. Here is my Code:
public class Konto extends AppCompatActivity implements
ZXingScannerView.ResultHandler{
private SQLiteHandler db;
private SessionManager session;
private ZXingScannerView mScannerView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_konto
);
ImageButton test = (ImageButton) findViewById(R.id.testforsql);
test.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mScannerView = new ZXingScannerView(Konto.this);
setContentView(mScannerView);
mScannerView.setResultHandler(Konto.this);
mScannerView.startCamera();
}
});
}
#Override
public void handleResult(Result result) {
Log.w("handleResult",result.getText());
/* AlertDialog.Builder builder = new AlertDialog.Builder(Konto.this);
builder.setTitle("Scan Result");
builder.setMessage(result.getText());
AlertDialog alertDialog = builder.create();
alertDialog.show();*/
if (result.equals("test")){
Toast.makeText(getApplicationContext(),"works",Toast.LENGTH_LONG);
}else {
Toast.makeText(getApplicationContext(),"works not",Toast.LENGTH_LONG).show();
}
mScannerView.resumeCameraPreview(Konto.this);
}
}
Not familiar with API, but don't you need to call
result.getText().equals
Instead of
result.equals

Trouble with Intent in Android Studio

I have been trying to get a string to go from one java class to another for a project of mine. The code I have been experimenting with is not working. When I press the button, I know it opens the other Java class because it creates the other layout, but it doesn't show the string. Please help me.
First Java Class:
public class MainActivity extends AppCompatActivity {
private Button button;
Context context;
private EditText editText;
String number = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;
editText = (EditText) findViewById(R.id.editText);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (editText.getText().toString() != null) {
String value = "value";
Intent secondscreenIntent = new Intent(context, SecondScreenJ.class);
secondscreenIntent.putExtra("Number", editText.getText().toString());
startActivity(secondscreenIntent);
}
}
});
}
}
Second Java Class:
public class SecondScreenJ extends Activity {
String number = null;
private TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.secondscreen);
textView = (TextView) findViewById(R.id.textView);
Bundle extras = getIntent().getExtras();
if (extras != null){
number = extras.getString("number");
}
textView.setText(number);
}
}
you are putting "Number" as key but, in your second activity you are trying to retrieve "number"
so change number to Number and it will work.
Its case Sensitive.
Don't make your keys hard-coded in that way.
Just declare public static variable in MainActivity and use it from SecondScreenJ
public class MainActivity extends AppCompatActivity {
private Button button;
Context context;
private EditText editText;
public static String NUMBER_KEY = "Number";
String number = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;
editText = (EditText) findViewById(R.id.editText);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (editText.getText().toString() != null) {
String value = "value";
Intent secondscreenIntent = new Intent(context, SecondScreenJ.class);
secondscreenIntent.putExtra(NUMBER_KEY , editText.getText().toString());
startActivity(secondscreenIntent);
}
}
});
}
}
Second Java Class:
public class SecondScreenJ extends Activity {
String number = null;
private TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.secondscreen);
textView = (TextView) findViewById(R.id.textView);
Bundle extras = getIntent().getExtras();
if (extras != null){
number = extras.getString(MainActivity.NUMBER_KEY);
}
textView.setText(number);
}
}
Beware when using the key for putting and getting the extras. They're case sensitive. Replace "number" with "Number" in your second activity.

How to make the button loads the previous page in Android?

I have search a lot in different sites to solve this problem but I can't till now.
I have a simple app with 50 articles, two buttons, previous and next.
All works fine till the last article 50 where the problem is that the user can't choose the previous button 49, only can load from the beginning.
Here is my code: MainActivity.java
public class MainActivity extends AppCompatActivity {
private TextView meditCenterText;
private Button mStartButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
meditCenterText = (TextView) findViewById(R.id.editCenterText);
mStartButton = (Button) findViewById(R.id.startButton);
meditCenterText.setMovementMethod(new ScrollingMovementMethod());
mStartButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String TextView = meditCenterText.getText().toString();
startStory(TextView);
}
});
}
private void startStory(String TextView) {
Intent intent = new Intent(this, StoryActivity.class);
intent.putExtra("TextView", TextView);
startActivity(intent);
}
#Override
protected void onResume() {
super.onResume();
meditCenterText.setText("..... ");
}}
StoryActivity.java
public class StoryActivity extends AppCompatActivity {
public static final String TAG = StoryActivity.class.getSimpleName();
private Story mStory = new Story();
private ImageView mImageView;
private TextView mTextView;
private Page mCurrentPage;
private String mName;
private Button mPreviousButton;
private Button mNextButton;
private ScrollView mScrollView;
private Button mStartButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_story2);
//Intent intent = getIntent();
//mName = intent.getStringExtra(getString(R.string.key_name));
if(mName == null){
mName = "";
}
Log.d(TAG, mName);
mImageView = (ImageView)findViewById(R.id.storyImageView);
mTextView = (TextView)findViewById(R.id.storyTextView);
mPreviousButton = (Button)findViewById(R.id.previousButton);
mNextButton = (Button)findViewById(R.id.nextButton);
mTextView.setMovementMethod(new ScrollingMovementMethod());
loadPage(0);
}
private void loadPage(int choice){
mCurrentPage = mStory.getPage(choice);
Drawable drawable = getResources().getDrawable(mCurrentPage.getImageId());
mImageView.setImageDrawable(drawable);
String pageText = mCurrentPage.getText();
//add the name if placeholder included.
//pageText = String.format(pageText,mName);
mTextView.setText(pageText);
if(mCurrentPage.isSingle()){
mPreviousButton.setVisibility(View.VISIBLE);
mNextButton.setText("Start From The Beginning");
mNextButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mPreviousButton.setVisibility(View.VISIBLE);
loadPage(0);
}
});
}else{
mPreviousButton.setText(mCurrentPage.getChoices().getText1());
mNextButton.setText(mCurrentPage.getChoices().getText2());
mPreviousButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int previousPage = mCurrentPage.getChoices().getPreviousPage();
loadPage(previousPage);
}
});
mNextButton.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
int nextPage = mCurrentPage.getChoices().getNextPage();
loadPage(nextPage);
}
});
}}
}
Thanks for help.
It's probably the logic in your Page class. Have a proper look at it, otherwise from the android side everything is alright. One last thing, you should probably use a ViewPager for your app instead of changing the text every time you load a new oage

OnClickListener throws NullPointerException

I'm trying to set onClick listener to a button from a ViewPager's page but when I try to run the app I get NullPointerException
....
public class MainActivity extends Activity {
private ViewPager vPager;
private MyPagerAdapter pagerAdapter;
private TextView tv;
private MyViewPagerListener pagerListener;
private Button deleteAll;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv=(TextView) findViewById(R.id.page_state);
pagerListener = new MyViewPagerListener();
pagerAdapter = new MyPagerAdapter();
vPager = (ViewPager) findViewById(R.id.vPager);
vPager.setAdapter(pagerAdapter);
vPager.setCurrentItem(0);
vPager.setOnPageChangeListener(pagerListener);
//Here is the problem
ClickListener cl = new ClickListener();
deleteAll = (Button) vPager.findViewById(R.id.btnDelete);
deleteAll.setOnClickListener(cl);
}
.....
}
You should register a new OnClickListener object instead of a ClickListener. Also, you have to provide an implementation for the onClick method like this:
new OnClickListener() {
public void onClick(View v) {
// do something when the button is clicked
}
};

Categories