Skip to content

Instantly share code, notes, and snippets.

@EphraimB
Created May 9, 2019 18:36
Show Gist options
  • Save EphraimB/f2c05184690a6aef9536a16b36ad509a to your computer and use it in GitHub Desktop.
Save EphraimB/f2c05184690a6aef9536a16b36ad509a to your computer and use it in GitHub Desktop.
Android app crashing
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp">
<TextView
android:id="@+id/titleTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Sign In" />
<TextView
android:id="@+id/subtitleTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Please confirm fingerprint to continue" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/fingerprintImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="28dp"
app:srcCompat="@mipmap/ic_launcher" />
<TextView
android:id="@+id/paraLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="42dp"
android:text="Touch sensor" />
</LinearLayout>
<LinearLayout
android:id="@+id/buttons"
style="?android:attr/buttonBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:orientation="horizontal">
<Button
android:id="@+id/cancelButton"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:text="@string/cancel" />
</LinearLayout>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/constraintLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/paay_background"
tools:context=".MainActivity">
<LinearLayout
android:id="@+id/shipping"
android:layout_width="353dp"
android:layout_height="46dp"
android:layout_marginStart="8dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="8dp"
android:background="@drawable/button_border"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginTop="15dp"
android:text="@string/shipping_text"
android:textColor="#2b3680" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginTop="15dp"
android:text="239 N 9th Street Apt 526" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:layout_marginTop="15dp"
android:text=">" />
</LinearLayout>
<LinearLayout
android:id="@+id/payment"
android:layout_width="353dp"
android:layout_height="46dp"
android:layout_marginStart="8dp"
android:layout_marginTop="12dp"
android:layout_marginEnd="8dp"
android:background="@drawable/button_border"
android:onClick="switchToPayment"
android:text="@string/payment_text"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/shipping">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginTop="15dp"
android:text="@string/payment_text"
android:textColor="#2b3680" />
<TextView
android:id="@+id/paymentMethod"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginTop="15dp"
android:text="My Overstock Store Card" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:layout_marginTop="15dp"
android:text=">" />
</LinearLayout>
<android.support.constraint.ConstraintLayout
android:layout_width="411dp"
android:layout_height="496dp"
android:layout_marginTop="112dp"
android:background="@drawable/button_half_border"
android:paddingBottom="25dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/payment">
<TextView
android:id="@+id/textView9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="36dp"
android:layout_marginTop="24dp"
android:layout_marginBottom="8dp"
android:text="$57.59"
app:layout_constraintBottom_toTopOf="@+id/button"
app:layout_constraintStart_toEndOf="@+id/textView6"
app:layout_constraintTop_toBottomOf="@+id/textView4"
app:layout_constraintVertical_bias="0.047" />
<TextView
android:id="@+id/textView8"
android:layout_width="46dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="24dp"
android:layout_marginBottom="8dp"
android:text="1"
app:layout_constraintBottom_toTopOf="@+id/button"
app:layout_constraintStart_toEndOf="@+id/textView5"
app:layout_constraintTop_toBottomOf="@+id/textView3"
app:layout_constraintVertical_bias="0.043" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="28dp"
android:layout_marginTop="8dp"
android:text="Item"
android:textColor="#2b3680"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="184dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:text="Quantity"
android:textColor="#2b3680"
app:layout_constraintBottom_toTopOf="@+id/button"
app:layout_constraintStart_toEndOf="@+id/textView"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="44dp"
android:layout_marginTop="8dp"
android:text="Price"
android:textColor="#2b3680"
app:layout_constraintStart_toEndOf="@+id/textView3"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="28dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:text="Men's Stuhrling Original\nAscot Swiss Watch with\nLeather Strap ITEM# 18296734"
app:layout_constraintBottom_toTopOf="@+id/button"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView"
app:layout_constraintVertical_bias="0.025" />
<TextView
android:id="@+id/textView6"
android:layout_width="46dp"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginTop="24dp"
android:layout_marginBottom="8dp"
android:text="1"
app:layout_constraintBottom_toTopOf="@+id/button"
app:layout_constraintStart_toEndOf="@+id/textView5"
app:layout_constraintTop_toBottomOf="@+id/textView3"
app:layout_constraintVertical_bias="0.278" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="36dp"
android:layout_marginTop="24dp"
android:layout_marginBottom="8dp"
android:text="$57.59"
app:layout_constraintBottom_toTopOf="@+id/button"
app:layout_constraintStart_toEndOf="@+id/textView6"
app:layout_constraintTop_toBottomOf="@+id/textView4"
app:layout_constraintVertical_bias="0.263" />
<Button
android:id="@+id/button"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="332dp"
android:layout_marginEnd="8dp"
android:background="@drawable/blue_gradient_button_border"
android:onClick="confirmPaymentClick"
android:text="Confirm Payment - $57.59"
android:textColor="#ffffff"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.645"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:background="@drawable/red_gradient_button_border"
android:text="@string/declinePayment_text"
android:textColor="#ffffff"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.645"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button"
app:layout_constraintVertical_bias="0.386" />
<TextView
android:id="@+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="28dp"
android:layout_marginTop="28dp"
android:text="Total"
android:textColor="#2b3680"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView5" />
</android.support.constraint.ConstraintLayout>
</android.support.constraint.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/paay_background">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:background="@drawable/button_border"
android:paddingBottom="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0">
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="36dp"
android:layout_marginTop="44dp"
android:text="Select Payment Method"
android:textColor="#2196F3"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<LinearLayout
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/bottom_border"
android:onClick="MasterCardClick"
android:padding="20dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView2">
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginStart="8dp"
android:layout_marginTop="56dp"
android:layout_marginEnd="8dp"
android:src="@drawable/mastercard1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="70dp"
android:layout_marginEnd="8dp"
android:text="MasterCard - Ending in 2301" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="30dp"
android:layout_marginTop="70dp"
android:layout_marginEnd="8dp"
android:text="Edit >"
android:textColor="#2b3680" />
</LinearLayout>
<LinearLayout
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/bottom_border"
android:onClick="ApplePayClick"
android:padding="20dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button2">
<ImageView
android:layout_width="90dp"
android:layout_height="50dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="8dp"
android:src="@drawable/applepaylogo" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="50dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="8dp"
android:text="ApplePay" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="85dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="8dp"
android:text="Edit >"
android:textColor="#2b3680" />
</LinearLayout>
<LinearLayout
android:id="@+id/button4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/bottom_border"
android:onClick="CoinBaseClick"
android:padding="20dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button3">
<ImageView
android:layout_width="90dp"
android:layout_height="50dp"
android:layout_marginTop="10dp"
android:src="@drawable/bitcoin_logo_small" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="8dp"
android:text="CoinBase - Bitcoin (BTC)" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="40dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="8dp"
android:text="Edit >"
android:textColor="#2b3680" />
</LinearLayout>
<LinearLayout
android:id="@+id/button5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/bottom_border"
android:onClick="OverstockClick"
android:padding="20dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button4">
<ImageView
android:layout_width="90dp"
android:layout_height="50dp"
android:layout_marginTop="5dp"
android:src="@drawable/overstock_card_logo" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="40dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="8dp"
android:text="Overstock" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="100dp"
android:layout_marginTop="25dp"
android:layout_marginEnd="8dp"
android:text="Edit >"
android:textColor="#2b3680" />
</LinearLayout>
<Button
android:id="@+id/button6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="2dp"
android:layout_marginTop="90dp"
android:layout_marginEnd="2dp"
android:background="@drawable/blue_gradient_button_border"
android:text="Add card"
android:textColor="@android:color/white"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button5" />
</android.support.constraint.ConstraintLayout>
</android.support.constraint.ConstraintLayout>
package com.example.paay;
import android.Manifest;
import android.app.KeyguardManager;
import android.content.pm.PackageManager;
import android.hardware.fingerprint.FingerprintManager;
import android.os.Build;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatDialogFragment;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Button;
public class BiometricActivity extends AppCompatDialogFragment {
Context mContext;
public BiometricActivity(Context mContext) {
this.mContext = mContext;
}
private static final String TAG = "BiometricActivity";
//widgets
private TextView buttonCancel;
private ImageView mFingerprintImage;
private TextView mParaLabel;
private FingerprintManager fingerprintManager;
private KeyguardManager keyguardManager;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.activity_biometric, container, false);
buttonCancel = view.findViewById(R.id.cancelButton);
mFingerprintImage = (ImageView) view.findViewById(R.id.fingerprintImage);
mParaLabel = (TextView) view.findViewById(R.id.paraLabel);
buttonCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getDialog().dismiss();
}
});
// Check 1: Android version should be greater or equal to Marshmallow
// Check 2: Device has Fingerprint Scanner
// Check 3: Have permission to use fingerprint scanner in the app
// Check 4: Lock screen is secured with atleast 1 type of lock
// Check 5: Atleast 1 Fingerprint is registered
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
fingerprintManager = (FingerprintManager) mContext.getSystemService(mContext.FINGERPRINT_SERVICE);
keyguardManager = (KeyguardManager) mContext.getSystemService(mContext.KEYGUARD_SERVICE);
if (!fingerprintManager.isHardwareDetected()) {
mParaLabel.setText("Fingerprint Scanner not detected in Device");
}
else if (ContextCompat.checkSelfPermission(mContext, Manifest.permission.USE_FINGERPRINT) != PackageManager.PERMISSION_GRANTED) {
mParaLabel.setText("Permission not granted to use Fingerprint Scanner");
}
else if (!keyguardManager.isKeyguardSecure()) {
mParaLabel.setText("Add Lock to your Phone in Settings");
}
else if (!fingerprintManager.hasEnrolledFingerprints()) {
mParaLabel.setText("You should add atleast 1 Fingerprint to use this feature");
}
else {
mParaLabel.setText("Place your Finger on Scanner to Access the App.");
FingerprintHandler fingerprintHandler = new FingerprintHandler (getActivity());
fingerprintHandler.startAuth(fingerprintManager, null);
}
}
return view;
}
}
package com.example.paay;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.hardware.fingerprint.FingerprintManager;
import android.os.Build;
import android.os.CancellationSignal;
import android.support.v4.content.ContextCompat;
import android.widget.ImageView;
import android.widget.TextView;
@TargetApi(Build.VERSION_CODES.M)
public class FingerprintHandler extends FingerprintManager.AuthenticationCallback {
private Context context;
public FingerprintHandler(Context context) {
this.context = context;
}
public void startAuth(FingerprintManager fingerprintManager, FingerprintManager.CryptoObject cryptoObject) {
CancellationSignal cancellationSignal = new CancellationSignal();
fingerprintManager.authenticate(cryptoObject, cancellationSignal, 0, this, null);
}
@Override
public void onAuthenticationError(int errorCode, CharSequence errString) {
this.update("There was an Auth Error. " + errString, false);
}
@Override
public void onAuthenticationFailed() {
this.update("Auth Failed. ", false);
}
@Override
public void onAuthenticationHelp(int helpCode, CharSequence helpString) {
this.update("Error: " + helpString, false);
}
@Override
public void onAuthenticationSucceeded(FingerprintManager.AuthenticationResult result) {
this.update("You can now access the app.", true);
}
private void update(String s, boolean b) {
TextView paraLabel = (TextView) ((Activity)context).findViewById(R.id.paraLabel);
ImageView imageView = (ImageView) ((Activity)context).findViewById(R.id.fingerprintImage);
paraLabel.setText(s);
if (b == false) {
paraLabel.setTextColor(ContextCompat.getColor(context, R.color.colorAccent));
}
else {
paraLabel.setTextColor(ContextCompat.getColor(context, R.color.colorPrimary));
imageView.setImageResource(R.mipmap.action_done);
}
}
}
package com.example.paay;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BiometricActivity lBiometricActivity = new BiometricActivity(this);
TextView paymentMethod = (TextView) findViewById(R.id.paymentMethod);
Bundle extras = getIntent().getExtras();
String value = ""; // You can do it in better and cleaner way
if (extras != null) {
value = extras.getString("ButtonClick");
} else {
value = (String) paymentMethod.getText();
}
paymentMethod.setText(value);
}
public void switchToPayment(View view) {
Intent intent = new Intent(MainActivity.this, PaymentActivity.class);
startActivity(intent);
}
public void confirmPaymentClick(View view) {
BiometricActivity dialog = new BiometricActivity(this);
dialog.show(getSupportFragmentManager(), "BiometricActivity");
}
}
package com.example.paay;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.content.Intent;
public class PaymentActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_payment);
}
public void MasterCardClick(View view) {
Intent intent = new Intent(PaymentActivity.this, MainActivity.class);
intent.putExtra("ButtonClick","MasterCard");
startActivity(intent);
}
public void ApplePayClick(View view) {
Intent intent = new Intent(PaymentActivity.this, MainActivity.class);
intent.putExtra("ButtonClick","ApplePay");
startActivity(intent);
}
public void CoinBaseClick(View view) {
Intent intent = new Intent(PaymentActivity.this, MainActivity.class);
intent.putExtra("ButtonClick","CoinBase");
startActivity(intent);
}
public void OverstockClick(View view) {
Intent intent = new Intent(PaymentActivity.this, MainActivity.class);
intent.putExtra("ButtonClick","Overstock");
startActivity(intent);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment