Reputation: 1002
Using a tutorial (source code here), 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 must 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;
}
}
Upvotes: 0
Views: 5117
Reputation: 15919
There are already some good libraries that you can use. No need to reinvent the wheel.
Or use the official one:
Upvotes: 1