Below i have created a QR code generator using zxing library. The way it works now is that the user inputs text into the editText and when they tap on the Generate button, it displays a QR code with that text.
My question is, is there a way for me to alter the below code so that it gets the current userID (from firebase Authentication when the users signs up) and prints it to the editText button?
My goal is for the QR code to take the current user that is signed in and get their userID from Firebase and create a QR code based only on the userID.
If so id appreciate any help or if their is an easier way im open to suggestions - Thanks in advance.
CodeQR.java
public class CodeQR extends AppCompatActivity {
EditText User_txt;
Button generateBtn;
ImageView qrImage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_code_q_r);
User_txt = (EditText)findViewById(R.id.user_id);
generateBtn = (Button)findViewById(R.id.generate_btn);
qrImage = (ImageView)findViewById(R.id.imageview);
generateBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
try{
BitMatrix bitMatrix = multiFormatWriter.encode(User_txt.getText().toString(),BarcodeFormat.QR_CODE,500,500);
BarcodeEncoder barcodeEncoder = new BarcodeEncoder();
Bitmap bitmap = barcodeEncoder.createBitmap(bitMatrix);
qrImage.setImageBitmap(bitmap);
}catch (Exception e){
e.printStackTrace();
}
}
});
}
}
CodeQR.xml
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="413dp"
android:layout_height="160dp"
android:layout_weight="1"
android:background="@drawable/header_menu"
android:gravity="bottom"
android:orientation="vertical"
android:padding="12dp"
tools:ignore="MissingConstraints">
<TextView
android:id="@+id/code_txt"
android:layout_width="281dp"
android:layout_height="64dp"
android:text="QR Code"
android:textColor="#000000"
android:textSize="30sp" />
</LinearLayout>
<EditText
android:id="@+id/user_id"
android:layout_width="match_parent"
android:layout_height="104dp"
android:hint="Enter your UserID"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.287" />
<Button
android:id="@+id/generate_btn"
android:layout_width="178dp"
android:layout_height="84dp"
android:layout_gravity="center"
android:gravity="center"
android:text="GET QR Code"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.536"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.459" />
<ImageView
android:id="@+id/imageview"
android:layout_width="281dp"
android:layout_height="245dp"
android:layout_gravity="center"
android:background="#ffffff"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.569"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.82" />
</androidx.constraintlayout.widget.ConstraintLayout>
You can try following code -
public class CodeQR extends AppCompatActivity {
EditText User_txt;
Button generateBtn;
ImageView qrImage;
@Override
protected void onCreate(Bundle savedInstanceState) {
.........
generateBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//you can put a check here for null user
setupUserQRcode(FirebaseAuth.getInstance().getCurrentUser().getUid());
}
});
}
private void setupUserQRcode(String firebaseUID) {
MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
try {
BarcodeEncoder barcodeEncoder = new BarcodeEncoder();
BitMatrix bitMatrix = multiFormatWriter.encode(firebaseUID, BarcodeFormat.QR_CODE, 500, 500);
Bitmap bitmap = barcodeEncoder.createBitmap(bitMatrix);
qrImage.setImageBitmap(bitmap);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Make sure you setup your project with Firebase first.