47478

Android - Dynamic Slide Menu

Question:

Using a <a href="http://www.youtube.com/watch?v=YeR7McJIltk" rel="nofollow">tutorial</a> (source code <a href="https://github.com/jaylamont/AndroidFlyOutMenuDemo/tree/part1" rel="nofollow">here</a>), I was able to create a slide menu in android using a single xml layout for both the menu and the main content.

The problem is I need to make the menu and the content dynamic. I can create a dynamic view, but I don't know how to switch to a second dynamic view using a button\swipe or to create both views and have one slide out upon pressing of a button. The content <em>must</em> be dynamic, the menu will be based upon files from the internet. I've looked up numerous tutorials, but I haven't quite found how to meld all of them together into single coherent project.

So that would mean either making the View menu and View context dynamic from the source code linked above (rather than loading it from the xml), or simply creating two views that I can move around based on button switching.

Here's the non-dynamic version:

MainActivity.java

package com.example.slider; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.View; import com.example.slider.view.viewgroup.FlyOutContainer; public class MainActivity extends Activity{ FlyOutContainer root; @Override protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); this.root = (FlyOutContainer) this.getLayoutInflater().inflate(R.layout.activity_sample, null); this.setContentView(root); } @Override public boolean onCreateOptionsMenu(Menu menu){ getMenuInflater().inflate(R.menu.main, menu); return true; } public void toggleMenu(View v){ this.root.toggleMenu(); } }

Here's FlyOutContainer.java

package com.example.slider.view.viewgroup; import android.annotation.SuppressLint; import android.content.Context; import android.util.AttributeSet; import android.view.View; import android.widget.Button; import android.widget.CheckBox; import android.widget.EditText; import android.widget.LinearLayout; import android.widget.ScrollView; import android.widget.TextView; @SuppressLint("NewApi") public class FlyOutContainer extends LinearLayout { private View menu; private View context; protected static final int menuMargin = 125; public enum MenuState{ Closed, Open }; protected int currentContentOffset = 0; protected MenuState menuCurrentState = MenuState.Closed; public FlyOutContainer(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); // TODO Auto-generated constructor stub } public FlyOutContainer(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub } public FlyOutContainer(Context context) { super(context); // TODO Auto-generated constructor stub } protected void onAttachedToWindow(){ super.onAttachedToWindow(); this.menu = this.getChildAt(0); this.context = this.getChildAt(1); this.menu.setVisibility(View.GONE); } protected void onLayout(boolean changed, int left, int top, int right, int bottom){ if(changed) this.calculateChildDimensions(); this.menu.layout(left, top, right - menuMargin, bottom); this.context.layout(left + this.currentContentOffset, top, right + currentContentOffset, bottom); } public void toggleMenu(){ switch(this.menuCurrentState){ case Closed: this.menu.setVisibility(View.VISIBLE); this.currentContentOffset = this.getMenuWidth(); this.context.offsetLeftAndRight(currentContentOffset); this.menuCurrentState = MenuState.Open; break; case Open: this.context.offsetLeftAndRight(-currentContentOffset); this.currentContentOffset = 0; this.menuCurrentState = MenuState.Closed; this.menu.setVisibility(View.GONE); break; } this.invalidate(); } private int getMenuWidth(){ return this.menu.getLayoutParams().width; } private void calculateChildDimensions(){ this.context.getLayoutParams().height = this.getHeight(); this.context.getLayoutParams().width = this.getWidth(); this.menu.getLayoutParams().height = this.getHeight(); this.menu.getLayoutParams().width = this.getWidth() - menuMargin; } }

Answer1:

There are already some good libraries that you can use. No need to reinvent the wheel.

<ul><li><a href="https://github.com/jfeinstein10/SlidingMenu" rel="nofollow">SlidingMenu</a>#</li> <li><a href="https://github.com/SimonVT/android-menudrawer" rel="nofollow">Android-Menudrawer</a></li> </ul>

Or use the official one:

<ul><li><a href="http://developer.android.com/design/patterns/navigation-drawer.html" rel="nofollow">NavigationDrawer</a></li> <li><a href="http://developer.android.com/training/implementing-navigation/nav-drawer.html" rel="nofollow">How to create a NavigationDrawer</a></li> </ul>

Recommend

  • CSS Bundling and Internet Explorer's Limit
  • Function to check if jquery plugins are already initialized
  • Drag and Drop for imageview not working
  • Price range slider for my website
  • Is the listview recycling early? Or is Universal ImageLoader canceling in error? Or is my Logic wron
  • Keep transition effect on 1 div from moving the div that follows?
  • What is this strange character in chrome's resource css viewer?
  • How to create a new Bundle object?
  • EditText is covered by Keyboard
  • Is there any way to call saveCurrentTurnWithMatchData without sending a push notification?
  • How to Make a Spinner (In a Fragment) That Changes the App's Language?
  • Angular2 - Template reference inside NgSwitch
  • How to get listview position?
  • Syntax error on tokens, AnnotationName expected instead - error on query
  • URLConnection doesn't work since API 10 and higher?
  • Bypass multiple inheritance in Java
  • wxPython: displaying multiple widgets in same frame
  • Connect .sks to skscene.h
  • How to test if a URL from an Eclipse bundle is a directory?
  • Unity3D & Android: Difference between “UnityMain” and “main” threads?
  • How to rebase a series of branches?
  • Initializer list vs. initialization method
  • Control modification in presentation layer
  • Sails.js/waterline: Executing waterline queries in toJSON function of a model?
  • Deleting and Updating values from a cusrsor adapter
  • req.body is undefined - nodejs
  • Fetching methods from BroadcastReceiver to update UI
  • Window Size for Mac application
  • Symfony2: How to get request parameter
  • Why winpcap requires both .lib and .dll to run?
  • Apache 2.4 - remove | delete | uninstall
  • Akka Routing: Reply's send to router ends up as dead letters
  • Return words with double consecutive letters
  • Is there a mandatory requirement to switch app.yaml?
  • Cannot Parse HTML Data Using Android / JSOUP
  • Python: how to group similar lists together in a list of lists?
  • How do you join a server to an Active Directory (domain)?
  • Understanding cpu registers
  • unknown Exception android
  • Busy indicator not showing up in wpf window [duplicate]