The sliding menu was created with DialogFragment
.
It is an image that Dialog Fragment covers on Activity.
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="250"
android:fromXDelta="-100%"
android:toXDelta="0%" />
<alpha
android:duration="250"
android:fromAlpha="0.8"
android:toAlpha="1.0" />
</set>
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="250"
android:fromXDelta="0%"
android:toXDelta="-100%" />
<alpha
android:duration="250"
android:fromAlpha="1.0"
android:toAlpha="0.2" />
</set>
<resources xmlns:tools="http://schemas.android.com/tools">
....
<style name="SlideInMenuAnimation">
<item name="android:windowEnterAnimation">@anim/slide_in</item>
<item name="android:windowExitAnimation">@anim/slide_out</item>
</style>
....
</resources>
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/slide_menu"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
tools:context=".SlideInMenuFragment">
<LinearLayout
android:id="@+id/menu_area"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical">
<!--Space margin at the same height as the Action Bar-->
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/design_default_color_primary"
android:minHeight="?android:attr/actionBarSize" />
<!--Menu part-->
<ListView
android:id="@+id/menu_list"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</FrameLayout>
public class SlideInMenuFragment extends DialogFragment {
public SlideInMenuFragment() {
// Required empty public constructor
}
public static SlideInMenuFragment newInstance() {
SlideInMenuFragment fragment = new SlideInMenuFragment();
Bundle args = new Bundle();
fragment.setArguments(args);
return fragment;
}
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
Dialog dialog = new Dialog(getActivity());
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.fragment_slide_in_menu);
//Full-screen Dialog, left-justified
Window window = dialog.getWindow();
window.getAttributes().windowAnimations = R.style.SlideInMenuAnimation;
window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
window.setGravity(Gravity.TOP | Gravity.START);
window.setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
//Set the root view of Layout
FrameLayout rootView = dialog.findViewById(R.id.slide_menu);
onViewCreated(rootView, savedInstanceState);
return dialog;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
String[] menuArray = {"menu", "menu", "menu"};
//Set the contents of the menu
ListView menu = view.findViewById(R.id.menu_list);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getContext(), android.R.layout.simple_list_item_1, menuArray);
menu.setAdapter(adapter);
}
}
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Set a button on the left edge of the ActionBar
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
//The ↓ icon uses the standard sort icon, but it will be nice to change it to ≡.
actionBar.setHomeAsUpIndicator(android.R.drawable.ic_menu_sort_by_size);
}
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
//Menu display
FragmentManager fragmentManager = getSupportFragmentManager();
SlideInMenuFragment fragment = SlideInMenuFragment.newInstance();
fragment.show(fragmentManager, fragment.getClass().getSimpleName());
return true;
}
return super.onOptionsItemSelected(item);
}
}
Click the icon to the left of the action bar ↓ The menu opens. Tap the transparent part of the background to close the menu.
With this implementation, you can operate gestures and the menu does not pop out. I implemented this method because I had to change the menu myself, but After all it is better to use NavigationView.
Recommended Posts