android-snippets 1.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- android_snippets/__init__.py +4 -0
- android_snippets/cli.py +44 -0
- android_snippets/code_data.py +811 -0
- android_snippets-1.0.0.dist-info/METADATA +11 -0
- android_snippets-1.0.0.dist-info/RECORD +8 -0
- android_snippets-1.0.0.dist-info/WHEEL +5 -0
- android_snippets-1.0.0.dist-info/entry_points.txt +2 -0
- android_snippets-1.0.0.dist-info/top_level.txt +1 -0
android_snippets/cli.py
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"""Command-line interface for printing Android snippets."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import argparse
|
|
6
|
+
import sys
|
|
7
|
+
from collections.abc import Sequence
|
|
8
|
+
|
|
9
|
+
from .code_data import SNIPPETS
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def build_parser() -> argparse.ArgumentParser:
|
|
13
|
+
"""Build and return the command-line argument parser."""
|
|
14
|
+
parser = argparse.ArgumentParser(
|
|
15
|
+
prog="android-snippet",
|
|
16
|
+
description="Print a pre-configured Android code snippet.",
|
|
17
|
+
)
|
|
18
|
+
parser.add_argument(
|
|
19
|
+
"key",
|
|
20
|
+
nargs="?",
|
|
21
|
+
help="snippet key to print (for example: option-menu or toast)",
|
|
22
|
+
)
|
|
23
|
+
return parser
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def main(argv: Sequence[str] | None = None) -> int:
|
|
27
|
+
"""Print the requested snippet, or available keys when no match exists."""
|
|
28
|
+
args = build_parser().parse_args(argv)
|
|
29
|
+
key = args.key.rsplit(".", 1)[-1] if args.key else None
|
|
30
|
+
|
|
31
|
+
if key in SNIPPETS:
|
|
32
|
+
print(SNIPPETS[key])
|
|
33
|
+
return 0
|
|
34
|
+
|
|
35
|
+
if args.key is not None:
|
|
36
|
+
print(f"Unknown snippet key: {args.key}", file=sys.stderr)
|
|
37
|
+
print("Available snippets:")
|
|
38
|
+
for key in SNIPPETS:
|
|
39
|
+
print(f" {key}")
|
|
40
|
+
return 0
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
if __name__ == "__main__": # pragma: no cover
|
|
44
|
+
raise SystemExit(main())
|
|
@@ -0,0 +1,811 @@
|
|
|
1
|
+
"""Built-in Android code snippets."""
|
|
2
|
+
|
|
3
|
+
SNIPPETS: dict[str, str] = {
|
|
4
|
+
"option-menu": """@Override
|
|
5
|
+
public boolean onCreateOptionsMenu(Menu menu) {
|
|
6
|
+
getMenuInflater().inflate(R.menu.menu, menu);
|
|
7
|
+
return true;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
@Override
|
|
11
|
+
public boolean onOptionsItemSelected(MenuItem item) {
|
|
12
|
+
int id = item.getItemId();
|
|
13
|
+
|
|
14
|
+
if (id == R.id.menuHome) {
|
|
15
|
+
Toast.makeText(this, \"Home Selected\", Toast.LENGTH_SHORT).show();
|
|
16
|
+
return true;
|
|
17
|
+
}
|
|
18
|
+
if (id == R.id.menuProfile) {
|
|
19
|
+
Toast.makeText(this, \"Profile Selected\", Toast.LENGTH_SHORT).show();
|
|
20
|
+
return true;
|
|
21
|
+
}
|
|
22
|
+
if (id == R.id.menuSettings) {
|
|
23
|
+
Toast.makeText(this, \"Settings Selected\", Toast.LENGTH_SHORT).show();
|
|
24
|
+
return true;
|
|
25
|
+
}
|
|
26
|
+
if (id == R.id.menuAbout) {
|
|
27
|
+
Toast.makeText(this, \"About Selected\", Toast.LENGTH_SHORT).show();
|
|
28
|
+
return true;
|
|
29
|
+
}
|
|
30
|
+
if (id == R.id.menuExit) {
|
|
31
|
+
finish();
|
|
32
|
+
return true;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
return super.onOptionsItemSelected(item);
|
|
36
|
+
}""",
|
|
37
|
+
|
|
38
|
+
"toast": """Toast.makeText(
|
|
39
|
+
getApplicationContext(),
|
|
40
|
+
\"Message displayed\",
|
|
41
|
+
Toast.LENGTH_SHORT
|
|
42
|
+
).show();""",
|
|
43
|
+
|
|
44
|
+
"text-color": """int colors[] = {
|
|
45
|
+
R.color.amberOrange,
|
|
46
|
+
R.color.black,
|
|
47
|
+
R.color.coralRed,
|
|
48
|
+
R.color.emeraldGreen,
|
|
49
|
+
R.color.primaryBlue
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
btn.setOnClickListener(v -> {
|
|
53
|
+
Random r = new Random();
|
|
54
|
+
int id = 0;
|
|
55
|
+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.VANILLA_ICE_CREAM) {
|
|
56
|
+
id = r.nextInt(colors.length);
|
|
57
|
+
}
|
|
58
|
+
text.setTextColor(ContextCompat.getColor(MainActivity.this, colors[id]));
|
|
59
|
+
});""",
|
|
60
|
+
"date-time": """Date currentDateTime = Calendar.getInstance().getTime();
|
|
61
|
+
|
|
62
|
+
tvNumericDate.setText(new SimpleDateFormat(
|
|
63
|
+
\"dd/MM/yyyy\",
|
|
64
|
+
Locale.getDefault()
|
|
65
|
+
).format(currentDateTime));
|
|
66
|
+
|
|
67
|
+
tvTextualDate.setText(new SimpleDateFormat(
|
|
68
|
+
\"EEEE, MMMM d\",
|
|
69
|
+
Locale.getDefault()
|
|
70
|
+
).format(currentDateTime));
|
|
71
|
+
|
|
72
|
+
tvCurrentTime.setText(new SimpleDateFormat(
|
|
73
|
+
\"hh:mm a\",
|
|
74
|
+
Locale.getDefault()
|
|
75
|
+
).format(currentDateTime));""",
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
"alert-dialog": """public void showAlertDialog(View view) {
|
|
79
|
+
AlertDialog.Builder builder = new AlertDialog.Builder(this);
|
|
80
|
+
builder.setTitle(\"Notification\");
|
|
81
|
+
builder.setMessage(\"This is an alert dialog box triggered by your button click!\");
|
|
82
|
+
builder.setCancelable(false);
|
|
83
|
+
builder.setPositiveButton(\"OK\", new DialogInterface.OnClickListener() {
|
|
84
|
+
@Override
|
|
85
|
+
public void onClick(DialogInterface dialog, int which) {
|
|
86
|
+
Toast.makeText(MainActivity.this, \"Clicked OK\", Toast.LENGTH_SHORT).show();
|
|
87
|
+
dialog.dismiss();
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
builder.setNegativeButton(\"Cancel\", new DialogInterface.OnClickListener() {
|
|
91
|
+
@Override
|
|
92
|
+
public void onClick(DialogInterface dialog, int which) {
|
|
93
|
+
Toast.makeText(
|
|
94
|
+
MainActivity.this,
|
|
95
|
+
\"Dialog Cancelled\",
|
|
96
|
+
Toast.LENGTH_SHORT
|
|
97
|
+
).show();
|
|
98
|
+
dialog.dismiss();
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
AlertDialog alertDialog = builder.create();
|
|
102
|
+
alertDialog.show();
|
|
103
|
+
}""",
|
|
104
|
+
|
|
105
|
+
"audio-video": """protected void onCreate(Bundle savedInstanceState) {
|
|
106
|
+
super.onCreate(savedInstanceState);
|
|
107
|
+
setContentView(R.layout.activity_main);
|
|
108
|
+
|
|
109
|
+
vvPlayer = findViewById(R.id.vvPlayer);
|
|
110
|
+
vvPlayer.setVideoPath(
|
|
111
|
+
\"android.resource://\" + getPackageName() + \"/\" + R.raw.video
|
|
112
|
+
);
|
|
113
|
+
mediaPlayer = MediaPlayer.create(this, R.raw.audio);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
public void toggleVideo(View view) {
|
|
117
|
+
Button btnPlayVideo = (Button) view;
|
|
118
|
+
if (vvPlayer.isPlaying()) {
|
|
119
|
+
vvPlayer.pause();
|
|
120
|
+
btnPlayVideo.setText(\"Play Video\");
|
|
121
|
+
} else {
|
|
122
|
+
vvPlayer.start();
|
|
123
|
+
btnPlayVideo.setText(\"Pause Video\");
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
public void toggleAudio(View view) {
|
|
128
|
+
Button btnPlayAudio = (Button) view;
|
|
129
|
+
if (mediaPlayer != null) {
|
|
130
|
+
if (mediaPlayer.isPlaying()) {
|
|
131
|
+
mediaPlayer.pause();
|
|
132
|
+
btnPlayAudio.setText(\"Play Audio\");
|
|
133
|
+
} else {
|
|
134
|
+
mediaPlayer.start();
|
|
135
|
+
btnPlayAudio.setText(\"Pause Audio\");
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
@Override
|
|
141
|
+
protected void onDestroy() {
|
|
142
|
+
super.onDestroy();
|
|
143
|
+
if (mediaPlayer != null) {
|
|
144
|
+
mediaPlayer.release();
|
|
145
|
+
mediaPlayer = null;
|
|
146
|
+
}
|
|
147
|
+
}""",
|
|
148
|
+
"video-internet": """private VideoView vvVideo;
|
|
149
|
+
|
|
150
|
+
@SuppressLint(\"MissingInflatedId\")
|
|
151
|
+
@Override
|
|
152
|
+
protected void onCreate(Bundle savedInstanceState) {
|
|
153
|
+
super.onCreate(savedInstanceState);
|
|
154
|
+
setContentView(R.layout.activity_main);
|
|
155
|
+
|
|
156
|
+
vvVideo = findViewById(R.id.vvVideo);
|
|
157
|
+
MediaController mediaController = new MediaController(this);
|
|
158
|
+
mediaController.setAnchorView(vvVideo);
|
|
159
|
+
vvVideo.setMediaController(mediaController);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
public void playVideo(View view) {
|
|
163
|
+
String videoUrl = \"https://www.youtube.com/watch?v=O5O3yK8DJCc\";
|
|
164
|
+
Uri uri = Uri.parse(videoUrl);
|
|
165
|
+
vvVideo.setVideoURI(uri);
|
|
166
|
+
vvVideo.start();
|
|
167
|
+
}""",
|
|
168
|
+
"context-menu": """private ConstraintLayout mainLayout;
|
|
169
|
+
|
|
170
|
+
@Override
|
|
171
|
+
protected void onCreate(Bundle savedInstanceState) {
|
|
172
|
+
super.onCreate(savedInstanceState);
|
|
173
|
+
setContentView(R.layout.activity_main);
|
|
174
|
+
|
|
175
|
+
mainLayout = findViewById(R.id.mainLayout);
|
|
176
|
+
View tvLongPress = findViewById(R.id.tvLongPress);
|
|
177
|
+
registerForContextMenu(tvLongPress);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
@Override
|
|
181
|
+
public void onCreateContextMenu(
|
|
182
|
+
ContextMenu menu,
|
|
183
|
+
View view,
|
|
184
|
+
ContextMenu.ContextMenuInfo menuInfo
|
|
185
|
+
) {
|
|
186
|
+
super.onCreateContextMenu(menu, view, menuInfo);
|
|
187
|
+
MenuInflater inflater = getMenuInflater();
|
|
188
|
+
inflater.inflate(R.menu.menu, menu);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
@Override
|
|
192
|
+
public boolean onContextItemSelected(MenuItem item) {
|
|
193
|
+
int itemId = item.getItemId();
|
|
194
|
+
|
|
195
|
+
if (itemId == R.id.color_red) {
|
|
196
|
+
mainLayout.setBackgroundColor(Color.RED);
|
|
197
|
+
return true;
|
|
198
|
+
} else if (itemId == R.id.color_green) {
|
|
199
|
+
mainLayout.setBackgroundColor(Color.GREEN);
|
|
200
|
+
return true;
|
|
201
|
+
} else if (itemId == R.id.color_blue) {
|
|
202
|
+
mainLayout.setBackgroundColor(Color.BLUE);
|
|
203
|
+
return true;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
return super.onContextItemSelected(item);
|
|
207
|
+
}""",
|
|
208
|
+
"radio-check": """int selectedId = rgGender.getCheckedRadioButtonId();
|
|
209
|
+
if (selectedId == -1) {
|
|
210
|
+
Toast.makeText(
|
|
211
|
+
this,
|
|
212
|
+
\"Please select a gender\",
|
|
213
|
+
Toast.LENGTH_SHORT
|
|
214
|
+
).show();
|
|
215
|
+
return;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
RadioButton selectedRadioButton = findViewById(selectedId);
|
|
219
|
+
String gender = selectedRadioButton.getText().toString();
|
|
220
|
+
|
|
221
|
+
StringBuilder hobbies = new StringBuilder();
|
|
222
|
+
if (cbReading.isChecked()) {
|
|
223
|
+
hobbies.append(\"• Reading\\n\");
|
|
224
|
+
}
|
|
225
|
+
if (cbTravel.isChecked()) {
|
|
226
|
+
hobbies.append(\"• Travelling\\n\");
|
|
227
|
+
}
|
|
228
|
+
if (cbMovies.isChecked()) {
|
|
229
|
+
hobbies.append(\"• Watching Movies\\n\");
|
|
230
|
+
}
|
|
231
|
+
if (hobbies.length() == 0) {
|
|
232
|
+
hobbies.append(\"• None\");
|
|
233
|
+
}""",
|
|
234
|
+
"intent-data": """Intent intent = new Intent(this, SecondActivity.class);
|
|
235
|
+
intent.putExtra(\"name\", name);
|
|
236
|
+
intent.putExtra(\"phone\", phone);
|
|
237
|
+
intent.putExtra(\"department\", department);
|
|
238
|
+
intent.putExtra(\"gender\", gender);
|
|
239
|
+
intent.putExtra(\"courses\", courses.toString().trim());
|
|
240
|
+
startActivity(intent);""",
|
|
241
|
+
"custom-broadcast": """btnBroadcast = findViewById(R.id.btnBroadcast);
|
|
242
|
+
|
|
243
|
+
btnBroadcast.setOnClickListener(v -> {
|
|
244
|
+
// Create a custom Intent
|
|
245
|
+
Intent intent = new Intent(\"com.example.myapp.CUSTOM_ACTION\");
|
|
246
|
+
|
|
247
|
+
// Add data to Intent
|
|
248
|
+
intent.putExtra(\"message\", \"Hello from MainActivity!\");
|
|
249
|
+
|
|
250
|
+
// Restrict broadcast to this application
|
|
251
|
+
intent.setPackage(getPackageName());
|
|
252
|
+
|
|
253
|
+
// Send broadcast
|
|
254
|
+
sendBroadcast(intent);
|
|
255
|
+
});
|
|
256
|
+
|
|
257
|
+
// Receiver
|
|
258
|
+
public class MyReceiver extends BroadcastReceiver {
|
|
259
|
+
|
|
260
|
+
@Override
|
|
261
|
+
public void onReceive(Context context, Intent intent) {
|
|
262
|
+
// Receive data from Intent
|
|
263
|
+
String message = intent.getStringExtra(\"message\");
|
|
264
|
+
|
|
265
|
+
// Display received message
|
|
266
|
+
Toast.makeText(context, message, Toast.LENGTH_LONG).show();
|
|
267
|
+
}
|
|
268
|
+
}""",
|
|
269
|
+
"system-intents": """public class MyReceiver extends BroadcastReceiver {
|
|
270
|
+
|
|
271
|
+
@Override
|
|
272
|
+
public void onReceive(Context context, Intent intent) {
|
|
273
|
+
if (Intent.ACTION_AIRPLANE_MODE_CHANGED.equals(intent.getAction())) {
|
|
274
|
+
boolean isEnabled = Settings.Global.getInt(
|
|
275
|
+
context.getContentResolver(),
|
|
276
|
+
Settings.Global.AIRPLANE_MODE_ON,
|
|
277
|
+
0
|
|
278
|
+
) == 1;
|
|
279
|
+
|
|
280
|
+
if (isEnabled) {
|
|
281
|
+
Toast.makeText(
|
|
282
|
+
context,
|
|
283
|
+
\"Airplane Mode ON\",
|
|
284
|
+
Toast.LENGTH_SHORT
|
|
285
|
+
).show();
|
|
286
|
+
} else {
|
|
287
|
+
Toast.makeText(
|
|
288
|
+
context,
|
|
289
|
+
\"Airplane Mode OFF\",
|
|
290
|
+
Toast.LENGTH_SHORT
|
|
291
|
+
).show();
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
// Main Activity
|
|
298
|
+
MyReceiver receiver = new MyReceiver();
|
|
299
|
+
|
|
300
|
+
IntentFilter filter = new IntentFilter(
|
|
301
|
+
Intent.ACTION_AIRPLANE_MODE_CHANGED
|
|
302
|
+
);
|
|
303
|
+
|
|
304
|
+
ContextCompat.registerReceiver(
|
|
305
|
+
this,
|
|
306
|
+
receiver,
|
|
307
|
+
filter,
|
|
308
|
+
ContextCompat.RECEIVER_NOT_EXPORTED
|
|
309
|
+
);
|
|
310
|
+
|
|
311
|
+
@Override
|
|
312
|
+
protected void onDestroy() {
|
|
313
|
+
super.onDestroy();
|
|
314
|
+
unregisterReceiver(receiver);
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
// System intents
|
|
318
|
+
// Intent.ACTION_AIRPLANE_MODE_CHANGED Airplane Mode is turned ON or OFF
|
|
319
|
+
// Intent.ACTION_BATTERY_CHANGED Battery level or status changes
|
|
320
|
+
// Intent.ACTION_BATTERY_LOW Battery becomes low
|
|
321
|
+
// Intent.ACTION_BATTERY_OKAY Battery returns to normal level
|
|
322
|
+
// Intent.ACTION_POWER_CONNECTED Charger is connected
|
|
323
|
+
// Intent.ACTION_POWER_DISCONNECTED Charger is disconnected
|
|
324
|
+
// Intent.ACTION_BOOT_COMPLETED Device finishes booting
|
|
325
|
+
// Intent.ACTION_SCREEN_ON Screen is turned ON
|
|
326
|
+
// Intent.ACTION_SCREEN_OFF Screen is turned OFF
|
|
327
|
+
// Intent.ACTION_USER_PRESENT User unlocks the device
|
|
328
|
+
// ConnectivityManager.CONNECTIVITY_ACTION Network connectivity changes (deprecated)
|
|
329
|
+
// Intent.ACTION_DATE_CHANGED Device date changes
|
|
330
|
+
// Intent.ACTION_TIME_CHANGED System time changes
|
|
331
|
+
// Intent.ACTION_TIMEZONE_CHANGED Time zone changes
|
|
332
|
+
// Intent.ACTION_PACKAGE_ADDED A new application is installed
|
|
333
|
+
// Intent.ACTION_PACKAGE_REMOVED An application is uninstalled""",
|
|
334
|
+
"alarm-app": """public class MainActivity extends AppCompatActivity {
|
|
335
|
+
Button btnSelectTime;
|
|
336
|
+
int hour;
|
|
337
|
+
int minute;
|
|
338
|
+
|
|
339
|
+
@Override
|
|
340
|
+
protected void onCreate(Bundle savedInstanceState) {
|
|
341
|
+
super.onCreate(savedInstanceState);
|
|
342
|
+
setContentView(R.layout.activity_main);
|
|
343
|
+
btnSelectTime = findViewById(R.id.btnSelectTime);
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
public void showTimePicker(View view) {
|
|
347
|
+
Calendar calendar = Calendar.getInstance();
|
|
348
|
+
TimePickerDialog timePickerDialog = new TimePickerDialog(
|
|
349
|
+
this,
|
|
350
|
+
(timePicker, selectedHour, selectedMinute) -> {
|
|
351
|
+
hour = selectedHour;
|
|
352
|
+
minute = selectedMinute;
|
|
353
|
+
Calendar time = Calendar.getInstance();
|
|
354
|
+
time.set(Calendar.HOUR_OF_DAY, hour);
|
|
355
|
+
time.set(Calendar.MINUTE, minute);
|
|
356
|
+
SimpleDateFormat sdf = new SimpleDateFormat(
|
|
357
|
+
\"hh:mm a\", Locale.getDefault()
|
|
358
|
+
);
|
|
359
|
+
btnSelectTime.setText(sdf.format(time.getTime()));
|
|
360
|
+
},
|
|
361
|
+
calendar.get(Calendar.HOUR_OF_DAY),
|
|
362
|
+
calendar.get(Calendar.MINUTE),
|
|
363
|
+
false
|
|
364
|
+
);
|
|
365
|
+
timePickerDialog.show();
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
public void setAlarm(View view) {
|
|
369
|
+
Calendar calendar = Calendar.getInstance();
|
|
370
|
+
calendar.set(Calendar.HOUR_OF_DAY, hour);
|
|
371
|
+
calendar.set(Calendar.MINUTE, minute);
|
|
372
|
+
calendar.set(Calendar.SECOND, 0);
|
|
373
|
+
|
|
374
|
+
PendingIntent pendingIntent = PendingIntent.getBroadcast(
|
|
375
|
+
this,
|
|
376
|
+
0,
|
|
377
|
+
new Intent(this, AlarmReceiver.class),
|
|
378
|
+
PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE
|
|
379
|
+
);
|
|
380
|
+
AlarmManager alarmManager =
|
|
381
|
+
(AlarmManager) getSystemService(ALARM_SERVICE);
|
|
382
|
+
|
|
383
|
+
if (alarmManager != null) {
|
|
384
|
+
alarmManager.set(
|
|
385
|
+
AlarmManager.RTC_WAKEUP,
|
|
386
|
+
calendar.getTimeInMillis(),
|
|
387
|
+
pendingIntent
|
|
388
|
+
);
|
|
389
|
+
Toast.makeText(
|
|
390
|
+
this,
|
|
391
|
+
\"Alarm Set Successfully\",
|
|
392
|
+
Toast.LENGTH_SHORT
|
|
393
|
+
).show();
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
}""",
|
|
397
|
+
"simple-list-view": """listView = findViewById(R.id.listView);
|
|
398
|
+
|
|
399
|
+
String[] contacts = {
|
|
400
|
+
\"Ashbi: 9863728373\",
|
|
401
|
+
\"Jees: 9765389276\",
|
|
402
|
+
\"Rena: 8795739074\",
|
|
403
|
+
\"Kishore: 9635489207\",
|
|
404
|
+
\"Jasin: 8975378493\",
|
|
405
|
+
\"Diya: 9657893765\",
|
|
406
|
+
\"Merlin: 8976467854\"
|
|
407
|
+
};
|
|
408
|
+
|
|
409
|
+
ArrayAdapter<String> adapter = new ArrayAdapter<>(
|
|
410
|
+
this,
|
|
411
|
+
android.R.layout.simple_list_item_1,
|
|
412
|
+
contacts
|
|
413
|
+
);
|
|
414
|
+
listView.setAdapter(adapter);""",
|
|
415
|
+
"student-list-listview-arrayadapter": """package com.example.studentapp;
|
|
416
|
+
|
|
417
|
+
import android.os.Bundle;
|
|
418
|
+
import android.widget.ArrayAdapter;
|
|
419
|
+
import android.widget.ListView;
|
|
420
|
+
import android.widget.Toast;
|
|
421
|
+
|
|
422
|
+
import androidx.appcompat.app.AppCompatActivity;
|
|
423
|
+
|
|
424
|
+
public class MainActivity extends AppCompatActivity {
|
|
425
|
+
|
|
426
|
+
ListView studentList;
|
|
427
|
+
|
|
428
|
+
String[] names = {
|
|
429
|
+
\"Arun\", \"Rahul\", \"Anjali\", \"Kiran\", \"Meera\"
|
|
430
|
+
};
|
|
431
|
+
|
|
432
|
+
String[] courses = {
|
|
433
|
+
\"MCA\", \"MBA\", \"MCA\", \"BCA\", \"MSc Computer Science\"
|
|
434
|
+
};
|
|
435
|
+
|
|
436
|
+
String[] regNos = {
|
|
437
|
+
\"MCA001\", \"MBA002\", \"MCA003\", \"BCA004\", \"MSC005\"
|
|
438
|
+
};
|
|
439
|
+
|
|
440
|
+
@Override
|
|
441
|
+
protected void onCreate(Bundle savedInstanceState) {
|
|
442
|
+
super.onCreate(savedInstanceState);
|
|
443
|
+
setContentView(R.layout.activity_main);
|
|
444
|
+
|
|
445
|
+
studentList = findViewById(R.id.studentList);
|
|
446
|
+
|
|
447
|
+
ArrayAdapter<String> adapter = new ArrayAdapter<>(
|
|
448
|
+
this,
|
|
449
|
+
android.R.layout.simple_list_item_1,
|
|
450
|
+
names
|
|
451
|
+
);
|
|
452
|
+
studentList.setAdapter(adapter);
|
|
453
|
+
|
|
454
|
+
studentList.setOnItemClickListener((parent, view, position, id) -> {
|
|
455
|
+
String name = names[position];
|
|
456
|
+
String course = courses[position];
|
|
457
|
+
String regNo = regNos[position];
|
|
458
|
+
|
|
459
|
+
String details =
|
|
460
|
+
\"Name: \" + name
|
|
461
|
+
+ \"\\nCourse: \" + course
|
|
462
|
+
+ \"\\nRegister No: \" + regNo;
|
|
463
|
+
|
|
464
|
+
Toast.makeText(
|
|
465
|
+
MainActivity.this,
|
|
466
|
+
details,
|
|
467
|
+
Toast.LENGTH_LONG
|
|
468
|
+
).show();
|
|
469
|
+
});
|
|
470
|
+
}
|
|
471
|
+
}""",
|
|
472
|
+
"database-sample": """public class DatabaseHelper extends SQLiteOpenHelper {
|
|
473
|
+
private static final String DATABASE_NAME = "StuDB";
|
|
474
|
+
private static final int DATABASE_VERSION = 1;
|
|
475
|
+
public static final String TABLE_NAME = "student";
|
|
476
|
+
public static final String COL_ID = "id";
|
|
477
|
+
public static final String COL_NAME = "name";
|
|
478
|
+
public static final String COL_AGE = "age";
|
|
479
|
+
|
|
480
|
+
public DatabaseHelper(Context context) {
|
|
481
|
+
super(context, DATABASE_NAME, null, DATABASE_VERSION);
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
@Override
|
|
485
|
+
public void onCreate(SQLiteDatabase db) {
|
|
486
|
+
String createTable = "CREATE TABLE " + TABLE_NAME + " ("
|
|
487
|
+
+ COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
|
|
488
|
+
+ COL_NAME + " TEXT, "
|
|
489
|
+
+ COL_AGE + " INTEGER)";
|
|
490
|
+
db.execSQL(createTable);
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
@Override
|
|
494
|
+
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
|
|
495
|
+
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
|
|
496
|
+
onCreate(db);
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
public boolean insertStudent(String name, int age) {
|
|
500
|
+
SQLiteDatabase db = this.getWritableDatabase();
|
|
501
|
+
ContentValues contentValues = new ContentValues();
|
|
502
|
+
contentValues.put(COL_NAME, name);
|
|
503
|
+
contentValues.put(COL_AGE, age);
|
|
504
|
+
long result = db.insert(TABLE_NAME, null, contentValues);
|
|
505
|
+
return result != -1;
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
public Cursor getAllStudents() {
|
|
509
|
+
SQLiteDatabase db = this.getWritableDatabase();
|
|
510
|
+
return db.rawQuery("SELECT * FROM " + TABLE_NAME, null);
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
// Main Activity
|
|
515
|
+
private DatabaseHelper dbHelper;
|
|
516
|
+
|
|
517
|
+
@Override
|
|
518
|
+
protected void onCreate(Bundle savedInstanceState) {
|
|
519
|
+
super.onCreate(savedInstanceState);
|
|
520
|
+
setContentView(R.layout.activity_main);
|
|
521
|
+
|
|
522
|
+
dbHelper = new DatabaseHelper(this);
|
|
523
|
+
etName = findViewById(R.id.etName);
|
|
524
|
+
etAge = findViewById(R.id.etAge);
|
|
525
|
+
tvDisplay = findViewById(R.id.tvDisplay);
|
|
526
|
+
Button btnAdd = findViewById(R.id.btnAdd);
|
|
527
|
+
Button btnView = findViewById(R.id.btnView);
|
|
528
|
+
|
|
529
|
+
btnAdd.setOnClickListener(v -> {
|
|
530
|
+
String name = etName.getText().toString().trim();
|
|
531
|
+
String ageStr = etAge.getText().toString().trim();
|
|
532
|
+
if (name.isEmpty() || ageStr.isEmpty()) {
|
|
533
|
+
Toast.makeText(this, "Please enter both Name and Age", Toast.LENGTH_SHORT).show();
|
|
534
|
+
return;
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
int age = Integer.parseInt(ageStr);
|
|
538
|
+
boolean isInserted = dbHelper.insertStudent(name, age);
|
|
539
|
+
if (isInserted) {
|
|
540
|
+
Toast.makeText(this, "Student Added Successfully!", Toast.LENGTH_SHORT).show();
|
|
541
|
+
etName.setText("");
|
|
542
|
+
etAge.setText("");
|
|
543
|
+
} else {
|
|
544
|
+
Toast.makeText(this, "Insertion Failed", Toast.LENGTH_SHORT).show();
|
|
545
|
+
}
|
|
546
|
+
});
|
|
547
|
+
|
|
548
|
+
btnView.setOnClickListener(v -> {
|
|
549
|
+
Cursor cursor = dbHelper.getAllStudents();
|
|
550
|
+
if (cursor.getCount() == 0) {
|
|
551
|
+
cursor.close();
|
|
552
|
+
tvDisplay.setText("The student table is completely empty.");
|
|
553
|
+
return;
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
StringBuilder buffer = new StringBuilder();
|
|
557
|
+
while (cursor.moveToNext()) {
|
|
558
|
+
buffer.append("ID: ").append(cursor.getInt(0)).append("\n");
|
|
559
|
+
buffer.append("Name: ").append(cursor.getString(1)).append("\n");
|
|
560
|
+
buffer.append("Age: ").append(cursor.getInt(2)).append("\n");
|
|
561
|
+
buffer.append("---------------------------------\n");
|
|
562
|
+
}
|
|
563
|
+
cursor.close();
|
|
564
|
+
tvDisplay.setText(buffer.toString());
|
|
565
|
+
});
|
|
566
|
+
}""",
|
|
567
|
+
"database-grid-listview": """// DatabaseHelper.java
|
|
568
|
+
public class DatabaseHelper extends SQLiteOpenHelper {
|
|
569
|
+
private static final String DATABASE_NAME = "ProductCatalog.db";
|
|
570
|
+
private static final int DATABASE_VERSION = 1;
|
|
571
|
+
public static final String TABLE_NAME = "products";
|
|
572
|
+
public static final String COL_ID = "id";
|
|
573
|
+
public static final String COL_NAME = "product_name";
|
|
574
|
+
|
|
575
|
+
public DatabaseHelper(Context context) {
|
|
576
|
+
super(context, DATABASE_NAME, null, DATABASE_VERSION);
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
@Override
|
|
580
|
+
public void onCreate(SQLiteDatabase db) {
|
|
581
|
+
String createTable = "CREATE TABLE " + TABLE_NAME + " ("
|
|
582
|
+
+ COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
|
|
583
|
+
+ COL_NAME + " TEXT)";
|
|
584
|
+
db.execSQL(createTable);
|
|
585
|
+
}
|
|
586
|
+
|
|
587
|
+
@Override
|
|
588
|
+
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
|
|
589
|
+
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
|
|
590
|
+
onCreate(db);
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
public boolean insertProduct(String name) {
|
|
594
|
+
SQLiteDatabase db = this.getWritableDatabase();
|
|
595
|
+
ContentValues contentValues = new ContentValues();
|
|
596
|
+
contentValues.put(COL_NAME, name);
|
|
597
|
+
long result = db.insert(TABLE_NAME, null, contentValues);
|
|
598
|
+
return result != -1;
|
|
599
|
+
}
|
|
600
|
+
|
|
601
|
+
public ArrayList<String> getAllProducts() {
|
|
602
|
+
ArrayList<String> list = new ArrayList<>();
|
|
603
|
+
SQLiteDatabase db = this.getReadableDatabase();
|
|
604
|
+
Cursor cursor = db.rawQuery(
|
|
605
|
+
"SELECT " + COL_NAME + " FROM " + TABLE_NAME,
|
|
606
|
+
null
|
|
607
|
+
);
|
|
608
|
+
if (cursor.moveToFirst()) {
|
|
609
|
+
do {
|
|
610
|
+
list.add(cursor.getString(0));
|
|
611
|
+
} while (cursor.moveToNext());
|
|
612
|
+
}
|
|
613
|
+
cursor.close();
|
|
614
|
+
return list;
|
|
615
|
+
}
|
|
616
|
+
}
|
|
617
|
+
|
|
618
|
+
// MainActivity.java
|
|
619
|
+
public class MainActivity extends AppCompatActivity {
|
|
620
|
+
private EditText etProductName;
|
|
621
|
+
private ListView listViewProducts;
|
|
622
|
+
private GridView gridViewProducts;
|
|
623
|
+
private TextView tvToggleView;
|
|
624
|
+
private DatabaseHelper dbHelper;
|
|
625
|
+
private ArrayList<String> productList;
|
|
626
|
+
private ArrayAdapter<String> listAdapter;
|
|
627
|
+
private ArrayAdapter<String> gridAdapter;
|
|
628
|
+
private boolean isListViewActive = true;
|
|
629
|
+
|
|
630
|
+
@Override
|
|
631
|
+
protected void onCreate(Bundle savedInstanceState) {
|
|
632
|
+
super.onCreate(savedInstanceState);
|
|
633
|
+
setContentView(R.layout.activity_main);
|
|
634
|
+
|
|
635
|
+
dbHelper = new DatabaseHelper(this);
|
|
636
|
+
etProductName = findViewById(R.id.etProductName);
|
|
637
|
+
listViewProducts = findViewById(R.id.listViewProducts);
|
|
638
|
+
gridViewProducts = findViewById(R.id.gridViewProducts);
|
|
639
|
+
tvToggleView = findViewById(R.id.tvToggleView);
|
|
640
|
+
Button btnSaveProduct = findViewById(R.id.btnSaveProduct);
|
|
641
|
+
|
|
642
|
+
productList = dbHelper.getAllProducts();
|
|
643
|
+
listAdapter = new ArrayAdapter<>(
|
|
644
|
+
this,
|
|
645
|
+
android.R.layout.simple_list_item_1,
|
|
646
|
+
productList
|
|
647
|
+
);
|
|
648
|
+
listViewProducts.setAdapter(listAdapter);
|
|
649
|
+
gridAdapter = new ArrayAdapter<>(
|
|
650
|
+
this,
|
|
651
|
+
R.layout.grid_item,
|
|
652
|
+
R.id.tvGridText,
|
|
653
|
+
productList
|
|
654
|
+
);
|
|
655
|
+
gridViewProducts.setAdapter(gridAdapter);
|
|
656
|
+
|
|
657
|
+
btnSaveProduct.setOnClickListener(v -> {
|
|
658
|
+
String prodName = etProductName.getText().toString().trim();
|
|
659
|
+
if (prodName.isEmpty()) {
|
|
660
|
+
Toast.makeText(
|
|
661
|
+
this,
|
|
662
|
+
"Product name cannot be empty",
|
|
663
|
+
Toast.LENGTH_SHORT
|
|
664
|
+
).show();
|
|
665
|
+
return;
|
|
666
|
+
}
|
|
667
|
+
|
|
668
|
+
boolean isInserted = dbHelper.insertProduct(prodName);
|
|
669
|
+
if (isInserted) {
|
|
670
|
+
Toast.makeText(
|
|
671
|
+
this,
|
|
672
|
+
prodName + " added successfully!",
|
|
673
|
+
Toast.LENGTH_SHORT
|
|
674
|
+
).show();
|
|
675
|
+
etProductName.setText("");
|
|
676
|
+
productList.clear();
|
|
677
|
+
productList.addAll(dbHelper.getAllProducts());
|
|
678
|
+
listAdapter.notifyDataSetChanged();
|
|
679
|
+
gridAdapter.notifyDataSetChanged();
|
|
680
|
+
} else {
|
|
681
|
+
Toast.makeText(
|
|
682
|
+
this,
|
|
683
|
+
"Error inserting item.",
|
|
684
|
+
Toast.LENGTH_SHORT
|
|
685
|
+
).show();
|
|
686
|
+
}
|
|
687
|
+
});
|
|
688
|
+
|
|
689
|
+
tvToggleView.setOnClickListener(v -> {
|
|
690
|
+
if (isListViewActive) {
|
|
691
|
+
listViewProducts.setVisibility(View.GONE);
|
|
692
|
+
gridViewProducts.setVisibility(View.VISIBLE);
|
|
693
|
+
tvToggleView.setText("Switch to List View");
|
|
694
|
+
isListViewActive = false;
|
|
695
|
+
} else {
|
|
696
|
+
gridViewProducts.setVisibility(View.GONE);
|
|
697
|
+
listViewProducts.setVisibility(View.VISIBLE);
|
|
698
|
+
tvToggleView.setText("Switch to Grid View");
|
|
699
|
+
isListViewActive = true;
|
|
700
|
+
}
|
|
701
|
+
});
|
|
702
|
+
}
|
|
703
|
+
}""",
|
|
704
|
+
"async-task": """public class MainActivity extends AppCompatActivity {
|
|
705
|
+
private TextView statusTextView;
|
|
706
|
+
private Button startButton;
|
|
707
|
+
private final ExecutorService executorService =
|
|
708
|
+
Executors.newSingleThreadExecutor();
|
|
709
|
+
private final Handler mainThreadHandler =
|
|
710
|
+
new Handler(Looper.getMainLooper());
|
|
711
|
+
|
|
712
|
+
@Override
|
|
713
|
+
protected void onCreate(Bundle savedInstanceState) {
|
|
714
|
+
super.onCreate(savedInstanceState);
|
|
715
|
+
setContentView(R.layout.activity_main);
|
|
716
|
+
|
|
717
|
+
statusTextView = findViewById(R.id.statusTextView);
|
|
718
|
+
startButton = findViewById(R.id.startButton);
|
|
719
|
+
startButton.setOnClickListener(new View.OnClickListener() {
|
|
720
|
+
@Override
|
|
721
|
+
public void onClick(View v) {
|
|
722
|
+
statusTextView.setText("Processing background task...");
|
|
723
|
+
startButton.setEnabled(false);
|
|
724
|
+
executorService.execute(new Runnable() {
|
|
725
|
+
@Override
|
|
726
|
+
public void run() {
|
|
727
|
+
try {
|
|
728
|
+
Thread.sleep(3000);
|
|
729
|
+
} catch (InterruptedException e) {
|
|
730
|
+
Thread.currentThread().interrupt();
|
|
731
|
+
}
|
|
732
|
+
|
|
733
|
+
mainThreadHandler.post(new Runnable() {
|
|
734
|
+
@Override
|
|
735
|
+
public void run() {
|
|
736
|
+
statusTextView.setText(
|
|
737
|
+
"Task completed successfully!"
|
|
738
|
+
);
|
|
739
|
+
startButton.setEnabled(true);
|
|
740
|
+
}
|
|
741
|
+
});
|
|
742
|
+
}
|
|
743
|
+
});
|
|
744
|
+
}
|
|
745
|
+
});
|
|
746
|
+
}
|
|
747
|
+
|
|
748
|
+
@Override
|
|
749
|
+
protected void onDestroy() {
|
|
750
|
+
super.onDestroy();
|
|
751
|
+
executorService.shutdown();
|
|
752
|
+
}
|
|
753
|
+
}""",
|
|
754
|
+
"intent-call": """btnCall.setOnClickListener(v -> {
|
|
755
|
+
String num = etPhone.getText().toString();
|
|
756
|
+
if (!num.isEmpty()) {
|
|
757
|
+
Intent i = new Intent(
|
|
758
|
+
Intent.ACTION_DIAL,
|
|
759
|
+
Uri.parse("tel:" + num)
|
|
760
|
+
);
|
|
761
|
+
startActivity(i);
|
|
762
|
+
} else {
|
|
763
|
+
Toast.makeText(
|
|
764
|
+
this,
|
|
765
|
+
"Enter Phone Number to Continue",
|
|
766
|
+
Toast.LENGTH_SHORT
|
|
767
|
+
).show();
|
|
768
|
+
}
|
|
769
|
+
});""",
|
|
770
|
+
"intent-msg": """btnSms.setOnClickListener(v -> {
|
|
771
|
+
String num = etPhone.getText().toString();
|
|
772
|
+
String msg = etSmsBody.getText().toString();
|
|
773
|
+
|
|
774
|
+
if (!num.isEmpty() && !msg.isEmpty()) {
|
|
775
|
+
Intent sms = new Intent(Intent.ACTION_SENDTO);
|
|
776
|
+
sms.setData(Uri.parse("smsto:" + num));
|
|
777
|
+
sms.putExtra("sms_body", msg);
|
|
778
|
+
|
|
779
|
+
if (sms.resolveActivity(getPackageManager()) != null) {
|
|
780
|
+
startActivity(sms);
|
|
781
|
+
}
|
|
782
|
+
} else {
|
|
783
|
+
Toast.makeText(
|
|
784
|
+
this,
|
|
785
|
+
"Enter Phone Number and Message to Continue",
|
|
786
|
+
Toast.LENGTH_SHORT
|
|
787
|
+
).show();
|
|
788
|
+
}
|
|
789
|
+
});""",
|
|
790
|
+
"intent-mail": """btnEmail.setOnClickListener(v -> {
|
|
791
|
+
String email = etEmailAddress.getText().toString();
|
|
792
|
+
String subject = etEmailSubject.getText().toString();
|
|
793
|
+
String body = etEmailBody.getText().toString();
|
|
794
|
+
|
|
795
|
+
if (!email.isEmpty() && !subject.isEmpty() && !body.isEmpty()) {
|
|
796
|
+
Intent mail = new Intent(Intent.ACTION_SEND);
|
|
797
|
+
mail.setType("message/rfc822");
|
|
798
|
+
mail.putExtra(Intent.EXTRA_EMAIL, email);
|
|
799
|
+
mail.putExtra(Intent.EXTRA_SUBJECT, subject);
|
|
800
|
+
mail.putExtra(Intent.EXTRA_TEXT, body);
|
|
801
|
+
startActivity(Intent.createChooser(mail, "Send Email"));
|
|
802
|
+
} else {
|
|
803
|
+
Toast.makeText(
|
|
804
|
+
this,
|
|
805
|
+
"Enter Mail, Subject and Body to Continue",
|
|
806
|
+
Toast.LENGTH_SHORT
|
|
807
|
+
).show();
|
|
808
|
+
}
|
|
809
|
+
});""",
|
|
810
|
+
}
|
|
811
|
+
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: android-snippets
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: A command-line collection of reusable Android code snippets.
|
|
5
|
+
Author: Android Snippets Contributors
|
|
6
|
+
License: MIT
|
|
7
|
+
Classifier: Programming Language :: Python :: 3
|
|
8
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
9
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
10
|
+
Classifier: Operating System :: OS Independent
|
|
11
|
+
Requires-Python: >=3.10
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
android_snippets/__init__.py,sha256=iSpeEebE2FdF03Ue62IcAh43q53uXPh37kp4cXKZfmo,110
|
|
2
|
+
android_snippets/cli.py,sha256=PLUQ9slB6giLssZYDbE0S55cLs0RzgIfgs-XE0cEz5Q,1183
|
|
3
|
+
android_snippets/code_data.py,sha256=qW4OpSgOZyt4t_wLVmsdYgMc9AGdy3z_f7MhIfL6MCs,26599
|
|
4
|
+
android_snippets-1.0.0.dist-info/METADATA,sha256=p8fTzNYov5cJQqiI0QUYjelJ3XbTwnPPWMoTYXPnGp4,418
|
|
5
|
+
android_snippets-1.0.0.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
|
|
6
|
+
android_snippets-1.0.0.dist-info/entry_points.txt,sha256=MdsRwgQdwG1YCBE94MCvgwiM0UBXb3j2O_fk8oFE-Yk,62
|
|
7
|
+
android_snippets-1.0.0.dist-info/top_level.txt,sha256=QrHtb4UjygEy_klPfewb4-TfHeP_hw-S1rjn8HY7sTM,17
|
|
8
|
+
android_snippets-1.0.0.dist-info/RECORD,,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
android_snippets
|