[androidkit] add custom view for easy navigation between demos
Change-Id: I1614e105eb274738dc1f9eab6674e28afbbe6698 Reviewed-on: https://skia-review.googlesource.com/c/skia/+/401079 Reviewed-by: Florin Malita <fmalita@chromium.org> Commit-Queue: Jorge Betancourt <jmbetancourt@google.com>
This commit is contained in:
parent
827dab407e
commit
6d970c6033
@ -0,0 +1,78 @@
|
||||
package org.skia.androidkitdemo1;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.ActivityInfo;
|
||||
import android.content.pm.PackageInfo;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.util.AttributeSet;
|
||||
import android.util.Log;
|
||||
import android.view.View;
|
||||
import android.widget.AdapterView;
|
||||
import android.widget.ArrayAdapter;
|
||||
import android.widget.Spinner;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class NavigationSpinner extends Spinner {
|
||||
private final String TAG = "ANDROIDKIT DEMO SPINNER";
|
||||
|
||||
public NavigationSpinner(Context context) {
|
||||
super(context);
|
||||
init(context);
|
||||
}
|
||||
public NavigationSpinner(Context context, AttributeSet attr) {
|
||||
super(context, attr);
|
||||
init(context);
|
||||
}
|
||||
|
||||
private void init(Context context) {
|
||||
ArrayAdapter<String> adapter;
|
||||
|
||||
// populate Spinner
|
||||
ArrayList<String> navigationOptions;
|
||||
try {
|
||||
navigationOptions = getActivityList(context);
|
||||
} catch (Exception e) {
|
||||
return;
|
||||
}
|
||||
adapter = new ArrayAdapter(context, android.R.layout.simple_spinner_item, navigationOptions);
|
||||
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
|
||||
this.setAdapter(adapter);
|
||||
|
||||
// set Spinner logic
|
||||
this.setOnItemSelectedListener(new OnItemSelectedListener() {
|
||||
@Override
|
||||
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
|
||||
if (!parent.getItemAtPosition(position).equals("Select Activity")) {
|
||||
String activityName = parent.getItemAtPosition(position).toString();
|
||||
Log.d(TAG, "Navigating to " + activityName);
|
||||
try {
|
||||
Intent myIntent = new Intent(context, Class.forName(activityName));
|
||||
context.startActivity(myIntent);
|
||||
} catch (Exception e) {
|
||||
Log.d(TAG, "Couldn't find selected activity.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onNothingSelected(AdapterView<?> parent) {
|
||||
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private ArrayList<String> getActivityList(Context c) throws PackageManager.NameNotFoundException {
|
||||
PackageManager pm = c.getPackageManager();
|
||||
PackageInfo info = pm.getPackageInfo(c.getPackageName(), PackageManager.GET_ACTIVITIES);
|
||||
ActivityInfo[] list = info.activities;
|
||||
|
||||
ArrayList<String> activityNames = new ArrayList<>();
|
||||
for (ActivityInfo activity : list) {
|
||||
activityNames.add(activity.name);
|
||||
}
|
||||
activityNames.add(0, "Select Demo");
|
||||
return activityNames;
|
||||
}
|
||||
}
|
@ -11,4 +11,10 @@
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:layout_weight="1"/>
|
||||
|
||||
<org.skia.androidkitdemo1.NavigationSpinner
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
app:layout_constraintBottom_toBottomOf="parent">
|
||||
</org.skia.androidkitdemo1.NavigationSpinner>
|
||||
</android.support.constraint.ConstraintLayout>
|
@ -26,8 +26,12 @@
|
||||
android:layout_width="200px"
|
||||
android:layout_height="200px"
|
||||
app:layout_constraintLeft_toLeftOf="parent"
|
||||
app:layout_constraintTop_toBottomOf="@+id/image"
|
||||
>
|
||||
app:layout_constraintTop_toBottomOf="@+id/image">
|
||||
</SurfaceView>
|
||||
<org.skia.androidkitdemo1.NavigationSpinner
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
app:layout_constraintBottom_toBottomOf="parent">
|
||||
</org.skia.androidkitdemo1.NavigationSpinner>
|
||||
|
||||
</android.support.constraint.ConstraintLayout>
|
Loading…
Reference in New Issue
Block a user