I have very little to no experience with developing android apps. My friend asked me to create a small feature for his app. What I'm stuck on is creating a submenu for options, i.e. I have it so that when a button is clicked a menu pops up with a few options and I need it to open a second menu when a specific option is clicked. I'll post my code below (both XML and Java) in hopes that someone can help me. Currently I have my menu show up through Java code and not XML code. I'm not sure if this is bad practice or not but I'll let whoever can help me tell me whether or not it is. Thanks in advance.
XML Code:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_main"
tools:context="com.example.android.postvu.MainActivity">
<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/background"
android:scaleType="centerCrop"
android:src="@drawable/grid" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:text="@string/image_text_editor"
android:textColor="@android:color/black"
android:textSize="28sp"
android:textStyle="bold"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="Image Text Editor" />
<Button
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:background="@mipmap/ic_launcher"
android:scaleType="centerCrop"
android:onClick="myOnClickMethod"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView"
app:layout_constraintVertical_bias="0.98" />
Java Code (relating to the button being clicked and the menu showing up):
public void myOnClickMethod(View v) {
registerForContextMenu(v);
openContextMenu(v);
}
final int CONTEXT_MENU_VIEW = 1;
final int CONTEXT_MENU_EDIT = 2;
final int CONTEXT_MENU_ARCHIVE = 3;
@Override
public void onCreateContextMenu (ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
menu.setHeaderTitle("Options");
menu.add(Menu.NONE, CONTEXT_MENU_VIEW, Menu.NONE, "Take Photo");
menu.add(Menu.NONE, CONTEXT_MENU_EDIT, Menu.NONE, "Photo Album");
menu.add(Menu.NONE, CONTEXT_MENU_ARCHIVE, Menu.NONE, "Plain Image");
}
@Override
public boolean onContextItemSelected (MenuItem item) {
switch (item.getItemId()) {
case CONTEXT_MENU_VIEW: {
}
break;
case CONTEXT_MENU_EDIT: {
}
break;
case CONTEXT_MENU_ARCHIVE: {
}
break;
}
return super.onContextItemSelected(item);
}