<commit_before>---
toc_title: Build your own UI
description: Customize the UI of Lock in your App
---

# Lock Android: Build your own UI

<%= include('../../_includes/_package', { pkgRepo: 'native-mobile-samples', pkgBranch: 'master', pkgPath: 'Android/custom-ui-sample', pkgFilePath: 'Android/custom-ui-sample/app/src/main/res/values/auth0.xml', pkgType: 'replace' }) %>

**Otherwise, if you already have an existing application, please follow the steps below.**

> We are going to use the library [EventBus](https://github.com/greenrobot/EventBus) in order to post authentication related events like *authentication done* and  *authentication failed*

1.  Add the following dependencies to your project:
  ```gradle
  compile 'com.auth0.android:core:1.15.+'
  compile 'de.greenrobot:eventbus:2.4.+'
  ```

1. Create a new resource file named `auth0.xml` under `values` and add the following content, replacing the values with your Auth0 ClientId and Domain
  ```xml
  <?xml version="1.0" encoding="utf-8"?>
  <resources>
      <string name="auth0_client_id">${account.clientId}</string>
      <string name="auth0_domain_name">${account.namespace}</string>
  </resources>
  ```

1. Create two classes `AuthenticationEvent` and `ErrorEvent` that will represent a successful authentication or a failed one
  ```java
  public class AuthenticationEvent {
      private final UserProfile profile;
      private final Token token;

      public AuthenticationEvent(UserProfile profile, Token token) {
          this.profile = profile;
          this.token = token;
      }

      public Token getToken() { return token; }

      public UserProfile getProfile() { return profile; }
  }

  public class ErrorEvent {
      private Dialog dialog;
      private int title;
      private int message;
      private Throwable throwable;

      public ErrorEvent(Dialog dialog) {
          this.dialog = dialog;
      }

      public ErrorEvent(int title, int message, Throwable throwable) {
          this.title = title;
          this.message = message;
          this.throwable = throwable;
      }
  }
  ```

1. Then in your Login *Activity* add the following fields
  ```java
      private AuthenticationAPIClient client;
      private EventBus eventBus;
  ```

1. In the same *Activity* `onCreate` method add the following lines to initialize **Auth0** and the fields we added earlier
  ```java
      Auth0 auth0 = new Auth0(getString(R.string.auth0_client_id), getString(R.string.auth0_domain_name));
      this.client = auth0.newAuthenticationAPIClient();
      this.eventBus = new EventBus();
  ```

1. When you need to login your user with email/password credentials, just paste the following code
  ```java
      String email = ... // Get email
      String password = ... // Get password
      Map<String, Object> authenticationParameters = ... // Additional authentication parameters
      client.login(email, password)
          .addParameters(authenticationParameters)
          .start(new AuthenticationCallback() {
              @Override
              public void onSuccess(UserProfile userProfile, Token token) {
                  eventBus.post(new AuthenticationEvent(userProfile, token));
              }

              @Override
              public void onFailure(Throwable throwable) {
                  eventBus.post(new ErrorEvent(R.string.login_failed_title, R.string.login_failed_message, throwable));
              }
          });
  ```
  > For more details about the parameters you can check [this wiki page](/libraries/lock-android/sending-authentication-parameters).

1. Finally handle both `AuthenticationEvent` and `ErrorEvent`
  ```java
      @Override
      protected void onStart() {
          super.onStart();
          eventBus.register(this);
      }<commit_msg>Added requirement for Connection string <commit_after>---
toc_title: Build your own UI
description: Customize the UI of Lock in your App
---

# Lock Android: Build your own UI

<%= include('../../_includes/_package', { pkgRepo: 'native-mobile-samples', pkgBranch: 'master', pkgPath: 'Android/custom-ui-sample', pkgFilePath: 'Android/custom-ui-sample/app/src/main/res/values/auth0.xml', pkgType: 'replace' }) %>

**Otherwise, if you already have an existing application, please follow the steps below.**

> We are going to use the library [EventBus](https://github.com/greenrobot/EventBus) in order to post authentication related events like *authentication done* and  *authentication failed*

1.  Add the following dependencies to your project:
  ```gradle
  compile 'com.auth0.android:core:1.15.+'
  compile 'de.greenrobot:eventbus:2.4.+'
  ```

1. Create a new resource file named `auth0.xml` under `values` and add the following content, replacing the values with your Auth0 ClientId and Domain
  ```xml
  <?xml version="1.0" encoding="utf-8"?>
  <resources>
      <string name="auth0_client_id">${account.clientId}</string>
      <string name="auth0_domain_name">${account.namespace}</string>
  </resources>
  ```

1. Create two classes `AuthenticationEvent` and `ErrorEvent` that will represent a successful authentication or a failed one
  ```java
  public class AuthenticationEvent {
      private final UserProfile profile;
      private final Token token;

      public AuthenticationEvent(UserProfile profile, Token token) {
          this.profile = profile;
          this.token = token;
      }

      public Token getToken() { return token; }

      public UserProfile getProfile() { return profile; }
  }

  public class ErrorEvent {
      private Dialog dialog;
      private int title;
      private int message;
      private Throwable throwable;

      public ErrorEvent(Dialog dialog) {
          this.dialog = dialog;
      }

      public ErrorEvent(int title, int message, Throwable throwable) {
          this.title = title;
          this.message = message;
          this.throwable = throwable;
      }
  }
  ```

1. Then in your Login *Activity* add the following fields
  ```java
      private AuthenticationAPIClient client;
      private EventBus eventBus;
  ```

1. In the same *Activity* `onCreate` method add the following lines to initialize **Auth0** and the fields we added earlier
  ```java
      Auth0 auth0 = new Auth0(getString(R.string.auth0_client_id), getString(R.string.auth0_domain_name));
      this.client = auth0.newAuthenticationAPIClient();
      this.eventBus = new EventBus();
  ```

1. When you need to login your user with email/password credentials, just paste the following code
  ```java
      String email = ... // Get email
      String password = ... // Get password
      //add your DB connection name, since you are using email / pwd login
      //will throw a 400 error if not set
      ParameterBuilder parameterBuilder = ParameterBuilder.newBuilder();
      Map<String, Object> authenticationParameters = parameterBuilder
                .setConnection("YOUR_DB_CONNECTION_NAME")
                .asDictionary();
      client.login(email, password)
          .addParameters(authenticationParameters)
          .start(new AuthenticationCallback() {
              @Override
              public void onSuccess(UserProfile userProfile, Token token) {
                  eventBus.post(new AuthenticationEvent(userProfile, token));
              }

              @Override
              public void onFailure(Throwable throwable) {
                  eventBus.post(new ErrorEvent(R.string.login_failed_title, R.string.login_failed_message, throwable));
              }
          });
  ```
  > For more details about the parameters you can check [this wiki page](/libraries/lock-android/sending-authentication-parameters).

1. Finally handle both `AuthenticationEvent` and `ErrorEvent`
  ```java
      @Override
      protected void onStart() {
          super.onStart();
          eventBus.register(this);
      }
{'fraction_non_alphanumeric': 0.10090342282733172, 'fraction_numerical': 0.007507316452474869, 'mean_word_length': 2.9939024390243905, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 2, '>': 25, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 8}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6534632', 'n_tokens_mistral': 2094, 'n_tokens_neox': 2052, 'n_words': 674}