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.
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!!
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);
}
}}}
data intent equal null in onActivityResult(int requestCode, int resultCode, Intent data) when i make facebook login. and FacebookCallbacks didn't call
below will find my code :
ChatMainActivity.class
public class ChatMainActivity extends AppCompatActivity {
FacebookLogin facebookLogin;
String userType="";
public void getHashkey(){
try {
PackageInfo info = getPackageManager().getPackageInfo(getApplicationContext().getPackageName(), PackageManager.GET_SIGNATURES);
for (Signature signature : info.signatures) {
MessageDigest md = MessageDigest.getInstance("SHA");
md.update(signature.toByteArray());
Log.i("Base64", Base64.encodeToString(md.digest(),Base64.NO_WRAP));
}
} catch (PackageManager.NameNotFoundException e) {
Log.d("Name not found", e.getMessage(), e);
} catch (NoSuchAlgorithmException e) {
Log.d("Error", e.getMessage(), e);
}
}
String currentUserId="";
/* #Override
public void onBackPressed() {
CustomDialog customDialog=new CustomDialog(this);
customDialog.show();
super.onBackPressed();
}*/
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getHashkey();
FirebaseMessaging.getInstance().subscribeToTopic("android");
MySharedPreferences.setUpMySharedPreferences(this,Constants.constKey);
currentUserId=MySharedPreferences.getUserSetting(Constants.currentUserId);
Log.d("asdasd",currentUserId);
facebookLogin=new FacebookLogin(this);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
userType="u";
facebookLogin.login(userType);
}
});
FloatingActionButton fab1 = (FloatingActionButton) findViewById(R.id.fab1);
fab1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
/*userType="a";
Intent intent = new Intent(ChatMainActivity.this, ChatActivity.class);
Bundle bundle = new Bundle();
String name="admin";
bundle.putString("name",name);
bundle.putString("id", "admin");
bundle.putString("img", "admin");
bundle.putString("userType",userType);
intent.putExtra("userData", bundle);
Toast.makeText(ChatMainActivity.this,"welcome "+name,Toast.LENGTH_SHORT).show();
startActivity(intent);*/
Intent intent=new Intent(ChatMainActivity.this,UsersActivit.class);
startActivity(intent);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
facebookLogin.registerCallbackManager(requestCode, resultCode, data);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
LoginManager.getInstance().logOut();
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
protected void onResume() {
/*Intent intent=new Intent(this ,ChatService.class);
stopService(intent);*/
/* if( currentUserId!=null &&!currentUserId.equals("")) {
Intent intent = new Intent(this, ChatService.class);
startService(intent);
}*/
super.onResume();
}
#Override
protected void onDestroy() {
// Log.d("asdasd",currentUserId);
/* if( currentUserId!=null &&!currentUserId.equals("")) {
Intent intent = new Intent(this, ChatService.class);
startService(intent);
}*/
super.onDestroy();
}
}
FacebookLogin .class
public class FacebookLogin implements FacebookCallback<LoginResult> {
Activity activity;
private List<String> facebookPermissions;
private CallbackManager callbackManager;
private LoginManager loginManager;
private ProfileTracker profileTracker = new ProfileTracker() {
#Override
protected void onCurrentProfileChanged(Profile oldProfile, Profile currentProfile) {
if (currentProfile != null) {
Intent intent = new Intent(activity, ChatActivity.class);
Bundle bundle = new Bundle();
String name = currentProfile.getFirstName() + " " + currentProfile.getLastName();
bundle.putString("name", name);
bundle.putString("id", currentProfile.getId());
intent.putExtra("userData", bundle);
Toast.makeText(activity, "welcome " + name, Toast.LENGTH_SHORT).show();
activity.startActivity(intent);
}
}
};
private AccessTokenTracker accessTokenTracker = new AccessTokenTracker() {
#Override
protected void onCurrentAccessTokenChanged(AccessToken oldAccessToken, AccessToken currentAccessToken) {
}
};
public FacebookLogin(Activity activity) {
this.activity = activity;
facebookPermissions = Arrays.asList("public_profile", "email");
profileTracker.startTracking();
accessTokenTracker.startTracking();
}
private void initFacebookLogin() {
FacebookSdk.sdkInitialize(activity.getApplicationContext());
callbackManager = CallbackManager.Factory.create();
loginManager = LoginManager.getInstance();
//loginManager.registerCallback(callbackManager,this);
}
String userType = "";
public void login(String type) {
if (NetworkState.ConnectionAvailable(activity)) {
userType = type;
initFacebookLogin();
/*loginManager.logInWithReadPermissions(activity, facebookPermissions);*/
LoginManager.getInstance().registerCallback(callbackManager, this);
LoginManager.getInstance().logInWithReadPermissions(activity, facebookPermissions);
} else
Toast.makeText(activity, "No Internet Connection", Toast.LENGTH_SHORT).show();
}
public void registerCallbackManager(final int requestCode, final int resultCode, final Intent data) {
callbackManager.onActivityResult(requestCode, resultCode, data);
}
#Override
public void onSuccess(LoginResult loginResult) {
Profile profile = Profile.getCurrentProfile();
if (profile != null) {
Intent intent = new Intent(activity, ChatActivity.class);
Bundle bundle = new Bundle();
String name = profile.getFirstName() + " " + profile.getLastName();
bundle.putString("name", name);
bundle.putString("id", profile.getId());
bundle.putString("img", profile.getProfilePictureUri(100, 100).toString());
bundle.putString("userType", userType);
intent.putExtra("userData", bundle);
Toast.makeText(activity, "welcome " + name, Toast.LENGTH_SHORT).show();
activity.startActivity(intent);
}
// Toast.makeText(activity,"welcome ",Toast.LENGTH_SHORT).show();
}
#Override
public void onCancel() {
}
#Override
public void onError(FacebookException error) {
}
}
This is what I do:
declare a "Facebook" LoginButton and a FacebookCallbackManager:
LoginButton btLogin;
CallbackManager mFacebookCallbackManager;
init Facebook callbacks handler:
private void initFacebookLoginCallbacks() {
mFacebookCallbackManager = CallbackManager.Factory.create();
LoginManager.getInstance().registerCallback(mFacebookCallbackManager,
new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
// logged in successfully!
}
#Override
public void onCancel() {
Log.v(TAG, "On cancel");
}
#Override
public void onError(FacebookException error) {
Log.v(TAG, "On error: " + error.toString());
}
});
}
Call onActivityResult on my FacebookCallbackManager
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
mFacebookCallbackManager.onActivityResult(requestCode, resultCode, data);
}
I need to call this function twice in my application pointing to two different activities. I have a unique request code for each call, however my app seems to crash everytime it launches the second activity.
Here is my code (only relevant parts):
MainActivity:
//Request Info vars
static final int GET_DETAILS = 1;
static final int EDIT_DETAILS = 2;
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
public void onMapClick(LatLng latLng) {
lat = latLng.latitude;
lon = latLng.longitude;
startActivityForResult(new Intent(MapsActivity.this,NewMarkerActivity.class), GET_DETAILS);
}
});
mMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
#Override
public void onInfoWindowClick(Marker marker) {
current_marker = marker;
startActivityForResult(new Intent(MapsActivity.this,EditMarkerActivity.class), EDIT_DETAILS);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
if (requestCode == GET_DETAILS) {
if (resultCode == RESULT_OK) {
String marker_title=data.getStringExtra("title");
String marker_snippet = data.getStringExtra("snippet");
addMarker(lat, lon, marker_title, marker_snippet);
mMap.animateCamera(CameraUpdateFactory.newLatLng(new LatLng(lat, lon)));
}
} if (requestCode == EDIT_DETAILS) {
if (resultCode == RESULT_OK) {
String marker_title=data.getStringExtra("title");
String marker_snippet = data.getStringExtra("snippet");
current_marker.setTitle(marker_title);
current_marker.setSnippet(marker_snippet);
}
}
}
EditMarkerActivity:
public class EditMarkerActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.edit_marker_activity);
Button save_btn = (Button)findViewById(R.id.btn_save);
save_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
EditText editName = (EditText)findViewById(R.id.editName);
String marker_title = editName.getText().toString();
EditText editSnippet = (EditText)findViewById(R.id.editSnippet);
String marker_snippet = editSnippet.getText().toString();
Intent _result = new Intent();
_result.putExtra("title", marker_title);
_result.putExtra("snippet", marker_snippet);
setResult(Activity.RESULT_OK, _result);
finish();
}
});
}
}
NewMarkerActivity:
public class NewMarkerActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_marker_activity);
Button save_btn = (Button)findViewById(R.id.btn_save);
save_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
EditText editName = (EditText)findViewById(R.id.editName);
String marker_title = editName.getText().toString();
EditText editSnippet = (EditText)findViewById(R.id.editSnippet);
String marker_snippet = editSnippet.getText().toString();
Intent _result = new Intent();
_result.putExtra("title", marker_title);
_result.putExtra("snippet", marker_snippet);
setResult(Activity.RESULT_OK, _result);
finish();
}
});
}
}
Any obvious issues? Help or insight to this problem will be greatly appreciated :)
Here is my output from Logcat:
android.content.ActivityNotFoundException: Unable to find explicit activity class {com.geekybrackets.virtualtourguide/com.geekybrackets.virtualtourguide.EditMarkerActivity}; have you declared this activity in your AndroidManifest.xml?
Turns out the issue was that I hadn't defined the activity in the manifest file.
Dear old me, now i know to check my errors !
Thanks again guys, case closed.