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.
Related
I have a RecyclerView and for each item, you can start the EditActivity (for Result) to update your text with Firebase.
The problem is that when you come back to the RecyclerView, data is not refreshed
Here's code from my adapter :
holder.editBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent edit_intent = new Intent(holder.authorText.getContext(), EditActivity.class);
edit_intent.putExtra("text", textList.get(position).getBody());
edit_intent.putExtra("id", textList.get(position).textId);
((Activity) context).startActivityForResult(edit_intent, 1);
}
});
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if(resultCode == RESULT_OK){
notifyDataSetChanged();
}
}
}
Here's code from my EditActivity :
edit_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mProgress.setVisibility(View.VISIBLE);
edit_btn.setVisibility(View.INVISIBLE);
String new_text = edit_text.getText().toString();
mFirestore.collection("Text").document(text_id).update("body", new_text).addOnCompleteListener(new OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull Task<Void> task) {
if(task.isSuccessful()){
Toast.makeText(EditActivity.this, R.string.changes, Toast.LENGTH_LONG).show();
Intent intent = new Intent();
intent.putExtra("newText", new_text);
setResult(RESULT_OK, intent);
finish();
}
else{
String error = task.getException().getMessage();
Toast.makeText(EditActivity.this, R.string.error + error, Toast.LENGTH_LONG).show();
}
mProgress.setVisibility(View.INVISIBLE);
edit_btn.setVisibility(View.VISIBLE);
}
});
}
});
How to refresh the RecyclerView and setText with the new Text ?
I would be very grateful for your help :)
There are few things you're doing wrong.
Pass the clickedItem position to EditActivity to update it later on.
holder.editBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent edit_intent = new Intent(holder.authorText.getContext(), EditActivity.class);
edit_intent.putExtra("text", textList.get(position).getBody());
edit_intent.putExtra("id", textList.get(position).textId);
edit_intent.putExtra("position", position);
((Activity) context).startActivityForResult(edit_intent, 1);
}
});
In EditActivity, Save the position in a variable & Return it back with new text
setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mProgress.setVisibility(View.VISIBLE);
edit_btn.setVisibility(View.INVISIBLE);
String new_text = edit_text.getText().toString();
mFirestore.collection("Text").document(text_id).update("body", new_text).addOnCompleteListener(new OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull Task<Void> task) {
if(task.isSuccessful()){
Toast.makeText(EditActivity.this, R.string.changes, Toast.LENGTH_LONG).show();
Intent intent = new Intent();
intent.putExtra("newText", new_text);
//Return the position s well
intent.putExtra("position", position);
setResult(RESULT_OK, intent);
finish();
}
else{
String error = task.getException().getMessage();
Toast.makeText(EditActivity.this, R.string.error + error, Toast.LENGTH_LONG).show();
}
mProgress.setVisibility(View.INVISIBLE);
edit_btn.setVisibility(View.VISIBLE);
}
});
}
});
override onActivityResult() in your adaptor's parent activity
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if(resultCode == RESULT_OK){
String newText = data.getStringExtra("newText");
int itemPosition = data.getIntExtra("position");
//Pass these values to adapter through `updateItem` method
adapter.updateItem(newText, itemPosition);
}
}
}
Define the updateItem method inside adapter class
public void updateItem(String newData, int index)
{
textList.set(index, newData);
adapter.notifyItemChanged(index);
}
Cheers :)
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?
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);
}