Flutter Engine
The Flutter Engine
NavigationSpinner.java
Go to the documentation of this file.
1package org.skia.jetskidemo;
2
3import android.content.Context;
4import android.content.Intent;
5import android.content.pm.ActivityInfo;
6import android.content.pm.PackageInfo;
7import android.content.pm.PackageManager;
8import android.util.AttributeSet;
9import android.util.Log;
10import android.view.View;
11import android.widget.AdapterView;
12import android.widget.ArrayAdapter;
13import android.widget.Spinner;
14
15import java.util.ArrayList;
16
17public class NavigationSpinner extends Spinner {
18 private final String TAG = "ANDROIDKIT DEMO SPINNER";
19
20 public NavigationSpinner(Context context) {
21 super(context);
22 init(context);
23 }
24 public NavigationSpinner(Context context, AttributeSet attr) {
25 super(context, attr);
26 init(context);
27 }
28
29 private void init(Context context) {
30 ArrayAdapter<String> adapter;
31
32 // populate Spinner
33 ArrayList<String> navigationOptions;
34 try {
35 navigationOptions = getActivityList(context);
36 } catch (Exception e) {
37 return;
38 }
39 adapter = new ArrayAdapter(context, android.R.layout.simple_spinner_item, navigationOptions);
40 adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
41 this.setAdapter(adapter);
42
43 // set Spinner logic
44 this.setOnItemSelectedListener(new OnItemSelectedListener() {
45 @Override
46 public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
47 if (!parent.getItemAtPosition(position).equals("Select Activity")) {
48 String activityName = parent.getItemAtPosition(position).toString();
49 Log.d(TAG, "Navigating to " + activityName);
50 try {
51 Intent myIntent = new Intent(context, Class.forName(activityName));
52 context.startActivity(myIntent);
53 } catch (Exception e) {
54 Log.d(TAG, "Couldn't find selected activity.");
55 }
56 }
57 }
58
59 @Override
60 public void onNothingSelected(AdapterView<?> parent) {
61
62 }
63 });
64 }
65
66 private ArrayList<String> getActivityList(Context c) throws PackageManager.NameNotFoundException {
67 PackageManager pm = c.getPackageManager();
68 PackageInfo info = pm.getPackageInfo(c.getPackageName(), PackageManager.GET_ACTIVITIES);
69 ActivityInfo[] list = info.activities;
70
71 ArrayList<String> activityNames = new ArrayList<>();
72 for (ActivityInfo activity : list) {
73 activityNames.add(activity.name);
74 }
75 activityNames.add(0, "Select Demo");
76 return activityNames;
77 }
78}
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition: DM.cpp:213
NavigationSpinner(Context context, AttributeSet attr)
void Log(const char *format,...) SK_PRINTF_LIKE(1
Definition: TestRunner.cpp:137