1
+ ---
2
+ layout: article
3
+ title: Start with Android (Java)
4
+ description: Get started with Appwrite on Android using Java and learn how to build secure and scalable apps using our powerful backend.
5
+ difficulty: beginner
6
+ readtime: 3
7
+ back: /docs/quick-starts
8
+ ---
9
+
10
+ Learn how to setup your first Android project powered by Appwrite and the [Appwrite Android SDK](https://github.com/appwrite/sdk-for-android) using Java.
11
+
12
+ {% section #step-1 step=1 title="Create Android project" %}
13
+ Open Android Studio and click **New Project** to create a new project.
14
+
15
+ Choose your desired project template, for example **Empty Activity**, and click **Next**.
16
+
17
+ Now enter your app **name** and **package name**. You will need both of these later when you create your project in the Appwrite console. Click **Finish** to create your project.
18
+
19
+ {% /section %}
20
+
21
+ {% section #step-2 step=2 title="Create Appwrite project" %}
22
+ Head to the [Appwrite Console](https://cloud.appwrite.io/console).
23
+
24
+ {% only_dark %}
25
+ 
26
+ {% /only_dark %}
27
+ {% only_light %}
28
+ 
29
+ {% /only_light %}
30
+
31
+ If this is your first time using Appwrite, create an account and create your first project.
32
+
33
+ Then, under **Add a platform**, add an **Android app**.
34
+
35
+ Add your app's **name** and **package name**, your package name is the one entered when creating an Android project. For existing projects, you should use the **applicationId** in your app-level [build.gradle](https://github.com/appwrite/playground-for-android/blob/master/app/build.gradle#L11) file.
36
+
37
+ {% only_dark %}
38
+ 
39
+ {% /only_dark %}
40
+ {% only_light %}
41
+ 
42
+ {% /only_light %}
43
+
44
+ You can skip optional steps.
45
+
46
+ {% /section %}
47
+
48
+ {% section #step-3 step=3 title="Add the Appwrite SDK" %}
49
+ To add the Appwrite SDK for Android as a dependency, add the following to your app-level **build.gradle** file inside the **dependencies** block.
50
+
51
+ ```java
52
+ implementation "io.appwrite:sdk-for-android:7.0.0"
53
+ ```
54
+
55
+ In order to allow creating OAuth sessions, the following activity needs to be added inside the `<application>` tag, along side the existing `<activity>` tags in your [AndroidManifest.xml](https://github.com/appwrite/playground-for-flutter/blob/master/android/app/src/main/AndroidManifest.xml).
56
+ Be sure to replace the **[PROJECT_ID]** string with your actual Appwrite project ID.
57
+ You can find your Appwrite project ID in you project settings screen in your Appwrite Console.
58
+
59
+ ```xml
60
+ <manifest ...>
61
+ ...
62
+ <application ...>
63
+ ...
64
+ <!-- Add this inside the `<application>` tag, along side the existing `<activity>` tags -->
65
+ <activity android:name="io.appwrite.views.CallbackActivity" android:exported="true">
66
+ <intent-filter android:label="android_web_auth">
67
+ <action android:name="android.intent.action.VIEW" />
68
+ <category android:name="android.intent.category.DEFAULT" />
69
+ <category android:name="android.intent.category.BROWSABLE" />
70
+ <data android:scheme="appwrite-callback-[PROJECT_ID]" />
71
+ </intent-filter>
72
+ </activity>
73
+ </application>
74
+ </manifest>
75
+ ```
76
+ {% /section %}
77
+
78
+ {% section #step-4 step=4 title="Create Appwrite Helper Class" %}
79
+ Find your project's ID in the **Settings** page.
80
+
81
+ {% only_dark %}
82
+ 
83
+ {% /only_dark %}
84
+ {% only_light %}
85
+ 
86
+ {% /only_light %}
87
+
88
+ Create a new file `AppwriteHelper.java` and add the following code to it, replacing `[PROJECT_ID]` with your project ID.
89
+
90
+ ```java
91
+ package YOUR_ROOT_PACKAGE_HERE;
92
+
93
+ import android.content.Context;
94
+
95
+ import java.util.Map;
96
+
97
+ import io.appwrite.Client;
98
+ import io.appwrite.ID;
99
+ import io.appwrite.coroutines.CoroutineCallback;
100
+ import io.appwrite.models.Session;
101
+ import io.appwrite.models.User;
102
+ import io.appwrite.services.Account;
103
+
104
+ public class AppwriteHelper {
105
+ private static Client client;
106
+ private static Account account;
107
+
108
+ public static void init(Context context) {
109
+ client = new Client(context)
110
+ .setEndpoint("https://<REGION>.cloud.appwrite.io/v1")
111
+ .setProject("[PROJECT_ID]");
112
+
113
+ account = new Account(client);
114
+ }
115
+
116
+ public interface AuthCallback<T> {
117
+ void onSuccess(T result);
118
+ void onError(Exception error);
119
+ }
120
+
121
+ public static void login(String email, String password, final AuthCallback<Session> callback) {
122
+ account.createEmailPasswordSession(email, password, new CoroutineCallback<>(result -> {
123
+ callback.onSuccess(result);
124
+ return null;
125
+ }, error -> {
126
+ callback.onError(error);
127
+ return null;
128
+ }));
129
+ }
130
+
131
+ public static void register(String email, String password, final AuthCallback<User<Map<String, Object>>> callback) {
132
+ account.create(
133
+ ID.unique(),
134
+ email,
135
+ password,
136
+ null,
137
+ new CoroutineCallback<>(result -> {
138
+ callback.onSuccess(result);
139
+ return null;
140
+ }, error -> {
141
+ callback.onError(error);
142
+ return null;
143
+ })
144
+ );
145
+ }
146
+
147
+ public static void logout(final AuthCallback<Object> callback) {
148
+ account.deleteSession("current", new CoroutineCallback<>(result -> {
149
+ callback.onSuccess(result);
150
+ return null;
151
+ }, error -> {
152
+ callback.onError(error);
153
+ return null;
154
+ }));
155
+ }
156
+ }
157
+ ```
158
+ {% /section %}
159
+
160
+ {% section #step-5 step=5 title="Create a login UI in XML" %}
161
+ First, update your `activity_main.xml` layout file:
162
+
163
+ ```xml
164
+ <?xml version="1.0" encoding="utf-8"?>
165
+ <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
166
+ xmlns:app="http://schemas.android.com/apk/res-auto"
167
+ xmlns:tools="http://schemas.android.com/tools"
168
+ android:layout_width="match_parent"
169
+ android:layout_height="match_parent"
170
+ android:orientation="vertical"
171
+ android:padding="16dp"
172
+ tools:context=".MainActivity">
173
+
174
+ <TextView
175
+ android:id="@+id/textViewStatus"
176
+ android:layout_width="match_parent"
177
+ android:layout_height="wrap_content"
178
+ android:gravity="center"
179
+ android:padding="16dp"
180
+ android:textSize="18sp"
181
+ android:visibility="gone" />
182
+
183
+ <Button
184
+ android:id="@+id/buttonLogout"
185
+ android:layout_width="match_parent"
186
+ android:layout_height="wrap_content"
187
+ android:text="Logout"
188
+ android:layout_marginBottom="16dp"
189
+ android:visibility="gone" />
190
+
191
+ <EditText
192
+ android:id="@+id/editTextEmail"
193
+ android:layout_width="match_parent"
194
+ android:layout_height="wrap_content"
195
+ android:layout_marginBottom="8dp"
196
+ android:hint="Email"
197
+ android:inputType="textEmailAddress" />
198
+
199
+ <EditText
200
+ android:id="@+id/editTextPassword"
201
+ android:layout_width="match_parent"
202
+ android:layout_height="wrap_content"
203
+ android:layout_marginBottom="16dp"
204
+ android:hint="Password"
205
+ android:inputType="textPassword" />
206
+
207
+ <LinearLayout
208
+ android:layout_width="match_parent"
209
+ android:layout_height="wrap_content"
210
+ android:orientation="horizontal">
211
+
212
+ <Button
213
+ android:id="@+id/buttonLogin"
214
+ android:layout_width="0dp"
215
+ android:layout_height="wrap_content"
216
+ android:layout_weight="1"
217
+ android:layout_marginEnd="8dp"
218
+ android:text="Login" />
219
+
220
+ <Button
221
+ android:id="@+id/buttonRegister"
222
+ android:layout_width="0dp"
223
+ android:layout_height="wrap_content"
224
+ android:layout_weight="1"
225
+ android:layout_marginStart="8dp"
226
+ android:text="Register" />
227
+ </LinearLayout>
228
+
229
+ </LinearLayout>
230
+ ```
231
+ {% /section %}
232
+
233
+ {% section #step-6 step=6 title="Create MainActivity" %}
234
+ Now update your `MainActivity.java` file with the following code:
235
+
236
+ ```java
237
+ package YOUR_ROOT_PACKAGE_HERE;
238
+
239
+ import android.os.Bundle;
240
+ import android.util.Log;
241
+ import android.view.View;
242
+ import android.widget.Button;
243
+ import android.widget.EditText;
244
+ import android.widget.TextView;
245
+ import android.widget.Toast;
246
+
247
+ import androidx.appcompat.app.AppCompatActivity;
248
+
249
+ import java.util.Map;
250
+
251
+ import io.appwrite.models.Session;
252
+ import io.appwrite.models.User;
253
+
254
+ public class MainActivity extends AppCompatActivity {
255
+ private static final String TAG = "MainActivity";
256
+
257
+ private EditText editTextEmail;
258
+ private EditText editTextPassword;
259
+ private Button buttonLogin;
260
+ private Button buttonRegister;
261
+ private TextView textViewStatus;
262
+ private Button buttonLogout;
263
+
264
+ @Override
265
+ protected void onCreate(Bundle savedInstanceState) {
266
+ super.onCreate(savedInstanceState);
267
+
268
+ // Initialize Appwrite
269
+ AppwriteHelper.init(getApplicationContext());
270
+
271
+ setContentView(R.layout.activity_main);
272
+
273
+ // Initialize UI components
274
+ editTextEmail = findViewById(R.id.editTextEmail);
275
+ editTextPassword = findViewById(R.id.editTextPassword);
276
+ buttonLogin = findViewById(R.id.buttonLogin);
277
+ buttonRegister = findViewById(R.id.buttonRegister);
278
+ textViewStatus = findViewById(R.id.textViewStatus);
279
+ buttonLogout = findViewById(R.id.buttonLogout);
280
+
281
+ // Set up click listeners
282
+ buttonLogin.setOnClickListener(v -> login());
283
+ buttonRegister.setOnClickListener(v -> register());
284
+ buttonLogout.setOnClickListener(v -> logout());
285
+ }
286
+
287
+ private void login() {
288
+ String email = editTextEmail.getText().toString().trim();
289
+ String password = editTextPassword.getText().toString().trim();
290
+
291
+ if (email.isEmpty() || password.isEmpty()) {
292
+ Toast.makeText(this, "Please enter email and password", Toast.LENGTH_SHORT).show();
293
+ return;
294
+ }
295
+
296
+ AppwriteHelper.login(email, password, new AppwriteHelper.AuthCallback<Session>() {
297
+ @Override
298
+ public void onSuccess(Session result) {
299
+ runOnUiThread(() -> {
300
+ Toast.makeText(MainActivity.this, "Login successful", Toast.LENGTH_SHORT).show();
301
+ showLoggedInUI(email);
302
+ });
303
+ }
304
+
305
+ @Override
306
+ public void onError(Exception error) {
307
+ runOnUiThread(() -> {
308
+ Log.e(TAG, "Login failed", error);
309
+ Toast.makeText(MainActivity.this, "Login failed: " + error.getMessage(), Toast.LENGTH_SHORT).show();
310
+ });
311
+ }
312
+ });
313
+ }
314
+
315
+ private void register() {
316
+ String email = editTextEmail.getText().toString().trim();
317
+ String password = editTextPassword.getText().toString().trim();
318
+
319
+ if (email.isEmpty() || password.isEmpty()) {
320
+ Toast.makeText(this, "Please enter email and password", Toast.LENGTH_SHORT).show();
321
+ return;
322
+ }
323
+
324
+ AppwriteHelper.register(email, password, new AppwriteHelper.AuthCallback<User<Map<String, Object>>>() {
325
+ @Override
326
+ public void onSuccess(User<Map<String, Object>> result) {
327
+ runOnUiThread(() -> {
328
+ Toast.makeText(MainActivity.this, "Registration successful. You can now login.", Toast.LENGTH_SHORT).show();
329
+ });
330
+ }
331
+
332
+ @Override
333
+ public void onError(Exception error) {
334
+ runOnUiThread(() -> {
335
+ Log.e(TAG, "Registration failed", error);
336
+ Toast.makeText(MainActivity.this, "Registration failed: " + error.getMessage(), Toast.LENGTH_SHORT).show();
337
+ });
338
+ }
339
+ });
340
+ }
341
+
342
+ private void logout() {
343
+ AppwriteHelper.logout(new AppwriteHelper.AuthCallback<Object>() {
344
+ @Override
345
+ public void onSuccess(Object result) {
346
+ runOnUiThread(() -> {
347
+ Toast.makeText(MainActivity.this, "Logout successful", Toast.LENGTH_SHORT).show();
348
+ showLoginUI();
349
+ });
350
+ }
351
+
352
+ @Override
353
+ public void onError(Exception error) {
354
+ runOnUiThread(() -> {
355
+ Log.e(TAG, "Logout failed", error);
356
+ Toast.makeText(MainActivity.this, "Logout failed: " + error.getMessage(), Toast.LENGTH_SHORT).show();
357
+ });
358
+ }
359
+ });
360
+ }
361
+
362
+ private void showLoggedInUI(String email) {
363
+ editTextEmail.setVisibility(View.GONE);
364
+ editTextPassword.setVisibility(View.GONE);
365
+ buttonLogin.setVisibility(View.GONE);
366
+ buttonRegister.setVisibility(View.GONE);
367
+
368
+ textViewStatus.setVisibility(View.VISIBLE);
369
+ buttonLogout.setVisibility(View.VISIBLE);
370
+
371
+ textViewStatus.setText("Logged in as " + email);
372
+ }
373
+
374
+ private void showLoginUI() {
375
+ editTextEmail.setVisibility(View.VISIBLE);
376
+ editTextPassword.setVisibility(View.VISIBLE);
377
+ buttonLogin.setVisibility(View.VISIBLE);
378
+ buttonRegister.setVisibility(View.VISIBLE);
379
+
380
+ textViewStatus.setVisibility(View.GONE);
381
+ buttonLogout.setVisibility(View.GONE);
382
+ }
383
+ }
384
+ ```
385
+ {% /section %}
386
+
387
+ {% section #step-7 step=7 title="All set" %}
388
+ Run your project by clicking **Run app** in Android Studio.
389
+ {% /section %}
0 commit comments