How to get result URL from QR Code Scanner clickable? - java

I just follow this tutorial video:
https://www.youtube.com/watch?v=Fe7F4Jx7rwo
And in the end after we scanned the QR Code, it only show a text even if its a link. How can I change it to clickable link from url that I put from my QR Code?
Here's my code that I use in MainActivity:
private Button scan_btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
scan_btn = (Button) findViewById(R.id.scan_btn);
final Activity activity = this;
scan_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
IntentIntegrator Integrator = new IntentIntegrator(activity);
Integrator.setDesiredBarcodeFormats(IntentIntegrator.QR_CODE_TYPES);
Integrator.setPrompt("SCAN NOW");
Integrator.setCameraId(0);
Integrator.setBeepEnabled(false);
Integrator.setBarcodeImageEnabled(false);
Integrator.initiateScan();
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if(result != null){
if(result.getContents()==null) {
Toast.makeText(this, "You cancelled the scanning", Toast.LENGTH_LONG).show();
}
else{
Toast.makeText(this, result.getContents(), Toast.LENGTH_LONG).show();
super.onActivityResult(requestCode, resultCode, data);
}
}}}

Related

How do I go back to MainActivity after login successful?

After login successful this returns to Main2Activity and I can log in again and again.but start app again after closing and removing from recent list it start in MainActivity. how to directly navigate Main2Activity to MainActivity after if condition is true.
Main2Activity code
public class Main2Activity extends AppCompatActivity {
private static final String TAG = "Main2Activity";
int AUTHUI_REQUEST_CODE = 1001;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
if (FirebaseAuth.getInstance().getCurrentUser() != null) {
startActivity(new Intent(this, MainActivity.class));
this.finish();
}
}
public void loginRegister(View view) {
List<AuthUI.IdpConfig> providers = Arrays.asList(
new AuthUI.IdpConfig.EmailBuilder().build(),
new AuthUI.IdpConfig.GoogleBuilder().build(),
new AuthUI.IdpConfig.PhoneBuilder().build()
);
Intent intent = AuthUI.getInstance()
.createSignInIntentBuilder()
.setAvailableProviders(providers)
.setTosAndPrivacyPolicyUrls("https://example.com","https://example.com")
.setLogo(R.drawable.i789)
.build();
startActivityForResult(intent, AUTHUI_REQUEST_CODE);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == AUTHUI_REQUEST_CODE){
if (requestCode == RESULT_OK){
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
Log.d(TAG, "onActivityResult: "+ user.getEmail());
if (user.getMetadata().getCreationTimestamp() == user.getMetadata().getLastSignInTimestamp()){
Toast.makeText(this, "Welcome", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(this, "Welcome back again", Toast.LENGTH_SHORT).show();
}
Intent t = new Intent(this,MainActivity.class);
startActivity(t);
this.finish();
}else{
IdpResponse response =IdpResponse.fromResultIntent(data);
if (response == null){
Log.d(TAG, "onActivityResult: the user has cancelled the sign in request");
}else {
Log.e(TAG, "onActivityResult: ",response.getError() );
}
}
}
}
}
code- Main Activity
public class MainActivity extends AppCompatActivity implements FirebaseAuth.AuthStateListener {
private static final String TAG = "MainActivity";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
private void startLogin(){
Intent intent = new Intent(this,Main2Activity.class);
startActivity(intent);
finish();
}
public void signout(View view){
AuthUI.getInstance().signOut(this);
}
#Override
protected void onStart() {
super.onStart();
FirebaseAuth.getInstance().addAuthStateListener(this);
}
#Override
protected void onStop() {
super.onStop();
FirebaseAuth.getInstance().removeAuthStateListener(this);
}
#Override
public void onAuthStateChanged(#NonNull FirebaseAuth firebaseAuth) {
if (firebaseAuth.getCurrentUser() == null){
startLogin();
return;
}
firebaseAuth.getCurrentUser().getIdToken(true)
.addOnSuccessListener(new OnSuccessListener<GetTokenResult>() {
#Override
public void onSuccess(GetTokenResult getTokenResult) {
Log.d(TAG, "onSuccess: "+getTokenResult.getToken());
}
});
}
}
I will suggest you that you create a splash screen (startup screen) which will initially on start check for the authentication whether user is logged in or not from the server. All this will be handled in a background thread and the ui thread will carry on the splash screen animation(if you wish any).So if the user is logged in, you navigate to mainActivity else navigate to loginActivity. Hope this helps you.

Scope problem - put intent extra and start activity from 2 different functions

I have 2 listeners, 1 needs to get the intent extra, which is an image uri, and the other one needs to start the other activity with that extra.
But I think that due to them being in different scopes, the extra is not really put for the intent that starts the other activity.
This is the code:
private Intent intent = new Intent(this, OtherActivity.class);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button button= findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
intent = new Intent(v.getContext(), OtherActivity.class);
startActivity(intent);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == Thing.SPECIFIC_REQUEST_CODE) {
Thing.ActivityResult result = Thing.getActivityResult(data);
if (resultCode == RESULT_OK) {
Uri resultUri = result.getUri();
intent.putExtra("imageUri", resultUri.toString());
}
}
}
I want that the extra that I put in onActivityResult will be sent to the activity started by the onClickListener when the button is clicked
my suggestion is to use a field to store the image URI :
1- define a String variable in your class
String imageURI = "";
2- in your onActivityResult :
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == Thing.SPECIFIC_REQUEST_CODE) {
Thing.ActivityResult result = Thing.getActivityResult(data);
if (resultCode == RESULT_OK) {
Uri resultUri = result.getUri();
imageURI = resultUri.toString();
}
}
3-in your onClickListener :
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
intent = new Intent(v.getContext(), OtherActivity.class);
intent.putExtra("imageUri", imageURI);
startActivity(intent);
}
});
In the onClick you are re-initializing the Intent, so any extras you may have set are lost. If you remove the line above startActivity you should be fine.
I would not go that way thought. I would not have the Intent as a class variable but rather the image URI. So whenever a click event happens, I would check for the image URI having been set, create a new Intent and then start the Activity.
Your code would become like this:
private String intentImage = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (intentImage != null) {
Intent intent = new Intent(this, OtherActivity.class);
intent.putExtra("imageUri", intentImage);
startActivity(intent);
}
});
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == Thing.SPECIFIC_REQUEST_CODE) {
Thing.ActivityResult result = Thing.getActivityResult(data);
if (resultCode == RESULT_OK) {
Uri resultUri = result.getUri();
intentImage = resultUri.toString();
}
}
}
Also keep in mind that you don't have any startActivityForResult in your code. So if this is your full activity, you would never get onActivityResult invoked.

URL Result from QR Code Scanner only show text

I'm new at programming and I tried to build QR Code Scanner app. My problem is in the Scanner, I can't have the result clickable even though the QR Code contains URL link on it. It just shows a pop-up text.
Here's my code:
Scanner:
public class ReaderActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reader);
Button scan_btn = findViewById(R.id.scan_btn);
final Activity activity = this;
IntentIntegrator qrScan = new IntentIntegrator(this);
scan_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
IntentIntegrator integrator = new IntentIntegrator(activity);
integrator.setDesiredBarcodeFormats(IntentIntegrator.QR_CODE_TYPES);
integrator.setPrompt("Scan");
integrator.setCameraId(0);
integrator.setBeepEnabled(false);
integrator.setBarcodeImageEnabled(false);
integrator.initiateScan();
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if(result != null){
if(result.getContents()==null){
Toast.makeText(this, "You cancelled the scanning", Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(this, result.getContents(), Toast.LENGTH_LONG).show();
}
}
else {
super.onActivityResult(requestCode, resultCode, data);
}
}
}
Can anybody fix this?

URL result in scanner

I'm still new at programming and I tried to build a code scanner for my project.
The problem is the result only show a text. It can't be click or direct to browser.
public class ReaderActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reader);
Button scan_btn = findViewById(R.id.scan_btn);
final Activity activity = this;
IntentIntegrator qrScan = new IntentIntegrator(this);
scan_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
IntentIntegrator integrator = new IntentIntegrator(activity);
integrator.setDesiredBarcodeFormats(IntentIntegrator.QR_CODE_TYPES);
integrator.setPrompt("Scan");
integrator.setCameraId(0);
integrator.setBeepEnabled(false);
integrator.setBarcodeImageEnabled(false);
integrator.initiateScan();
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if(result != null){
if(result.getContents()==null){
Toast.makeText(this, "You cancelled the scanning", Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(this, result.getContents(), Toast.LENGTH_LONG).show();
}
}
else {
super.onActivityResult(requestCode, resultCode, data);
}
}
}
can somebody fix this?
you can check if result.getContents() start with "http" or "https" use string.indexOf, get it and put to your browser.

Save QR code text in memory and show it in another activity

I have an app that scans QR code. And it to show in another activity. How to pass QR Code text to another activity? Or save it in memory?
MainActivity
public class MainActivity extends AppCompatActivity implements ZXingScannerView.ResultHandler{
private ZXingScannerView zXingScannerView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
zXingScannerView = new ZXingScannerView(getApplicationContext());
setContentView(zXingScannerView);
zXingScannerView.setResultHandler(this);
zXingScannerView.startCamera();
}
#Override
protected void onPause() {
super.onPause();
zXingScannerView.stopCamera();
}
#Override
public void handleResult(Result result) {
Toast.makeText(getApplicationContext(), result.getText(),Toast.LENGTH_SHORT).show();
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Result");
builder.setMessage(result.getText());
AlertDialog alertDialog = builder.create();
alertDialog.show();
zXingScannerView.resumeCameraPreview(this);
}
}
Call method startActivityForResult(new Intent(this, YourCallbackActivity.class), 1001);
Use setResult() method for get call back in another activity.
#Override
public void handleResult (Result result) {
Intent returnIntent = new Intent();
returnIntent.putExtra("result",String.valueOf(rawResult));
setResult(Activity.RESULT_OK,returnIntent);
finish();
}
now
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1001) {
if (resultCode == RESULT_OK && data != null) {
String result = data.getStringExtra("result");
//do whatever you want
}
}
}
I already did, hope it will help you!!

Categories