Swiping the Fly-in sliding menu android - android

I'm new to programming android, I'm trying to make the effect of sliding menu with your finger, followed several tutorials over the internet but got no success, I found this library on the Internet, it works by clicking on the button, I have the same effect when sliding the finger. Thank you in advance for your help!
LayerStack.java
package com.example.slideoutmenu;
import com.example.slideoutmenu.LayerStack;
import com.example.slideoutmenu.R;
import android.app.Activity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class LayerStack extends Activity {
//Declare
private LinearLayout slidingPanel;
private boolean isExpanded;
private DisplayMetrics metrics;
private ListView listView;
private RelativeLayout headerPanel;
private RelativeLayout menuPanel;
private int panelWidth;
private ImageView menuViewButton;
Button menu1, menu2,menu3,menu4 ;
TextView txtpays;
FrameLayout.LayoutParams menuPanelParameters;
FrameLayout.LayoutParams slidingPanelParameters;
LinearLayout.LayoutParams headerPanelParameters ;
LinearLayout.LayoutParams listViewParameters;
ImageView lampada;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_layer_stack);
//Initialize
metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
panelWidth = (int) ((metrics.widthPixels)*0.80);
headerPanel = (RelativeLayout) findViewById(R.id.header);
headerPanelParameters = (LinearLayout.LayoutParams) headerPanel.getLayoutParams();
headerPanelParameters.width = metrics.widthPixels;
headerPanel.setLayoutParams(headerPanelParameters);
menuPanel = (RelativeLayout) findViewById(R.id.menuPanel);
menuPanelParameters = (FrameLayout.LayoutParams) menuPanel.getLayoutParams();
menuPanelParameters.width = panelWidth;
menuPanel.setLayoutParams(menuPanelParameters);
slidingPanel = (LinearLayout) findViewById(R.id.slidingPanel);
slidingPanelParameters = (FrameLayout.LayoutParams) slidingPanel.getLayoutParams();
slidingPanelParameters.width = metrics.widthPixels;
slidingPanel.setLayoutParams(slidingPanelParameters);
listView = (ListView) findViewById(R.id.listView1);
listViewParameters = (LinearLayout.LayoutParams) listView.getLayoutParams();
listViewParameters.width = metrics.widthPixels;
listView.setLayoutParams(listViewParameters);
//Slide the Panel ;
//lampada = (ImageView) findViewById(R.id.imagelampada);
menu1 = (Button) findViewById(R.id.menu_item_1);
txtpays = (TextView) findViewById(R.id.listepays);
menu2 = (Button) findViewById(R.id.menu_item_2);
menu3 = (Button) findViewById(R.id.menu_item_3);
menu4 = (Button) findViewById(R.id.menu_item_4);
txtpays.setText("Clique acima para acessar o menu");
clicabotao();
//listaClica();
menuViewButton = (ImageView) findViewById(R.id.menuViewButton);
menuViewButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(!isExpanded){
expandMenu();
}else{
expandList();
}
}
});
}
//############################################# FUNÇÃO CRIA MENUS CLICK ##########################################
// função: executar codigo individual de ação ao clicar em botão do menu aposentos
public void clicabotao(){
// CONFIGURA ONCLICK DOS BOTÕES MENU
menu1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
try{
txtpays.setText("Clique acima para acessar o menu");
expandList();
}catch(Exception e){
Log.i("Mensagem ",""+e.getMessage());
}
}
});
menu2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
try{
txtpays.setText("Clique acima para acessar o menu");
expandList();
}catch(Exception e){
Log.i("Mensagem ",""+e.getMessage());
}
}
});
menu3.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
try{
txtpays.setText("Clique acima para acessar o menu");
expandList();
}catch(Exception e){
Log.i("Mensagem ",""+e.getMessage());
}
}
});
menu4.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
try{
txtpays.setText("Clique acima para acessar o menu");
expandList();
}catch(Exception e){
Log.i("Mensagem ",""+e.getMessage());
}
}
});
}
//############################################# FUNÇÃO CRIA ITEM DA LISTVIEW #############################################
//############################################# FUNÇÃO EXPANDE MENU APOSENTOS #############################################
public void expandMenu(){
new ExpandAnimation(slidingPanel, panelWidth,
Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, 0.75f, 0, 0.0f, 0, 0.0f);
isExpanded = true;
}
//############################################# FUNÇÃO EXPANDE A LISTVIEW ############################################
public void expandList(){
new CollapseAnimation(slidingPanel,panelWidth,
TranslateAnimation.RELATIVE_TO_SELF, 0.80f,
TranslateAnimation.RELATIVE_TO_SELF, 0.0f, 0, 0.0f, 0, 0.0f);
isExpanded = false;
}
class ListViewItem{
public int ThumbnailResource;
public String Title;
public String SubTitle;
}
}
ExpandAnimation.java
package com.example.slideoutmenu;
import android.view.Gravity;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.LinearLayout;
import android.widget.FrameLayout.LayoutParams;
public class ExpandAnimation extends TranslateAnimation implements Animation.AnimationListener{
private LinearLayout slidingLayout;
int panelWidth;
public ExpandAnimation(LinearLayout layout, int width, int fromXType, float fromXValue, int toXType,
float toXValue, int fromYType, float fromYValue, int toYType, float toYValue) {
super(fromXType, fromXValue, toXType, toXValue, fromYType, fromYValue, toYType, toYValue);
//Initialize
slidingLayout = layout;
panelWidth = width;
setDuration(400);
setFillAfter( false );
setInterpolator(new AccelerateDecelerateInterpolator());
setAnimationListener(this);
slidingLayout.startAnimation(this);
}
public void onAnimationEnd(Animation arg0) {
//Create margin and align left
LayoutParams params = (LayoutParams) slidingLayout.getLayoutParams();
params.leftMargin = panelWidth;
params.gravity = Gravity.LEFT;
slidingLayout.clearAnimation();
slidingLayout.setLayoutParams(params);
slidingLayout.requestLayout();
}
public void onAnimationRepeat(Animation arg0) {
}
public void onAnimationStart(Animation arg0) {
}
}
CollapseAnimation.java
package com.example.slideoutmenu;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.LinearLayout;
import android.widget.FrameLayout.LayoutParams;
public class CollapseAnimation extends TranslateAnimation implements TranslateAnimation.AnimationListener{
private LinearLayout slidingLayout;
int panelWidth;
public CollapseAnimation(LinearLayout layout, int width, int fromXType, float fromXValue, int toXType,
float toXValue, int fromYType, float fromYValue, int toYType, float toYValue) {
super(fromXType, fromXValue, toXType, toXValue, fromYType, fromYValue, toYType, toYValue);
//Initialize
slidingLayout = layout;
panelWidth = width;
setDuration(400);
setFillAfter( false );
setInterpolator(new AccelerateDecelerateInterpolator());
setAnimationListener(this);
//Clear left and right margins
LayoutParams params = (LayoutParams) slidingLayout.getLayoutParams();
params.rightMargin = 0;
params.leftMargin = 0;
slidingLayout.setLayoutParams(params);
slidingLayout.requestLayout();
slidingLayout.startAnimation(this);
}
public void onAnimationEnd(Animation animation) {
}
public void onAnimationRepeat(Animation animation) {
}
public void onAnimationStart(Animation animation) {
}
}
activity_layer_stack.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#drawable/blue_bg">
<!-- Menu Panel -->
<RelativeLayout
android:id="#+id/menuPanel"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:gravity="right"
android:background="#drawable/agua_menu"
android:orientation="vertical" >
<Button
android:id="#+id/menu_title_1"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_alignParentTop="true"
android:layout_marginLeft="0dp"
android:paddingLeft="15dp"
android:gravity="center_vertical"
android:textColor="#android:color/white"
android:text="#string/menu_title">
</Button>
<Button
android:id="#+id/menu_item_1"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_marginLeft="15dp"
android:gravity="center_vertical"
android:layout_below="#+id/menu_title_1"
android:textColor="#android:color/white"
android:text="Cozinha">
</Button>
<Button
android:id="#+id/menu_item_2"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_below="#+id/menu_item_1"
android:layout_marginLeft="15dp"
android:gravity="center_vertical"
android:textColor="#android:color/white"
android:text="Sala">
</Button>
<Button
android:id="#+id/menu_item_3"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_below="#+id/menu_item_2"
android:layout_marginLeft="15dp"
android:gravity="center_vertical"
android:textColor="#android:color/white"
android:text="Suite">
</Button>
<!-- <View
android:id="#+id/menu_item_divider_4"
android:layout_width="fill_parent"
android:layout_height="0.5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_below="#+id/menu_item_3"
android:background="#353535"/> -->
<Button
android:id="#+id/menu_item_4"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_below="#+id/menu_item_3"
android:layout_marginLeft="15dp"
android:gravity="center_vertical"
android:textColor="#android:color/white"
android:text="Quarto Hospedes">
</Button>
</RelativeLayout>
<!-- Sliding Panel -->
<LinearLayout
android:id="#+id/slidingPanel"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="left"
android:orientation="vertical"
android:background="#drawable/fundo_branco" >
<RelativeLayout
android:id="#+id/header"
android:layout_width="match_parent"
android:layout_height="50dp">
<ImageView
android:id="#+id/menuViewButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="15dp"
android:clickable="true"
android:contentDescription="#string/description"
android:src="#drawable/icon_menu1"
android:visibility="visible" />
</RelativeLayout>
<TextView
android:id="#+id/listepays"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_below="#+id/menu_item_divider_2"
android:layout_marginLeft="15dp"
android:gravity="center_vertical"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#00BFFF"
android:textColorHighlight="#550022" >
</TextView>
<View
android:id="#+id/listaDivide"
android:layout_width="fill_parent"
android:layout_height="0.1dp"
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp"
android:layout_below="#+id/menuViewButton"
android:background="#000000"/>
<View
android:id="#+id/listaDivide2"
android:layout_width="fill_parent"
android:layout_height="0.1dp"
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp"
android:layout_below="#+id/listepays"
android:background="#000000"/>
</LinearLayout>
</FrameLayout>

Related

Surface View and Linear layout and onClick event

I have a problem with adding my surface view to a linear layout, I have tried the already available answers and still cant seem to figure it out. My aim is to render an animation at the bottom of my login screen when my the next button is clicked. I tried adding a linear Layout to the Screen and then adding my gameView object on to that.
This is the loginActivity and game View Class for clarity I've also added the xml.
`package com.example.Combat;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.*;
import android.os.Bundle;
import android.util.Log;
import android.view.*;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MyActivity extends Activity {
/**
* Called when the activity is first created.
*/
public GameView gameView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//setting the font
Typeface myTypeFace = Typeface.createFromAsset(getAssets(),"Typo Oxin free promo.ttf");
TextView myTextView = (TextView) findViewById(R.id.nameTextView);
myTextView.setTypeface(myTypeFace);
gameView = new GameView(this);
// LinearLayout.LayoutParams lp =new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
// lp.gravity= Gravity.BOTTOM;
LinearLayout myLayout = new LinearLayout(this);
myLayout.findViewById(R.id.gameLayoutView);
myLayout.addView(gameView);
}
#Override
public void onResume(){
super.onResume();
gameView.resume();
}
#Override
protected void onPause(){
super.onPause();
gameView.pause();
}
public void beginMotion(View view) {
GameView.isMoving=!GameView.isMoving;
// startActivity(new Intent(getApplicationContext(),transitionActivity.class));
}
}
`
package com.example.Combat;
import android.content.Context;
import android.graphics.*;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
/**
* Created by vmuser on 2017/05/05.
*/
public class GameView extends SurfaceView implements Runnable{
private Thread gameThread;
private SurfaceHolder ourHolder;
public static volatile boolean playing=false;
private Canvas canvas;
private Bitmap bitmapRunningMan;
public static boolean isMoving=false;
private float runSpeedPerSecond = 150;
//LinearLayout screen=(LinearLayout)findViewById(R.id.theScreen);
private float manXPos = 1020, manYPos = 950;
private int frameWidth = 230, frameHeight = 274;
private int frameCount = 6;
private int currentFrame = 0;
private long fps;
private long timeThisFrame;
private long lastFrameChangeTime = 0;
private int frameLengthInMillisecond = 60;
private Rect frameToDraw = new Rect(0, 0, frameWidth, frameHeight);
private RectF whereToDraw = new RectF(manXPos, manYPos, manXPos + frameWidth, frameHeight);
public GameView(Context context) {
super(context);
ourHolder = getHolder();
bitmapRunningMan = BitmapFactory.decodeResource(getResources(),
R.drawable.perfectsoldier);
bitmapRunningMan = Bitmap.createScaledBitmap(bitmapRunningMan,
frameWidth * frameCount, frameHeight, false);
}
#Override
public void run() {
while (playing) {
long startFrameTime = System.currentTimeMillis();
update();
this.draw();
Log.d("theOne","its Happennig");
timeThisFrame = System.currentTimeMillis() - startFrameTime;
if (timeThisFrame >= 1) {
fps = 1000 / timeThisFrame;
}
}
}
public void update() {
if (isMoving) {
manXPos = manXPos - runSpeedPerSecond / fps;
if (manXPos > 0) {
//manYPos += (int) frameHeight;
//manXPos = 10;
isMoving=false;
}
if (manYPos + frameHeight > 0) {
// manYPos = 10;
isMoving=false;
}
}
}
public void manageCurrentFrame() {
long time = System.currentTimeMillis();
if (isMoving) {
if (time > lastFrameChangeTime + frameLengthInMillisecond) {
lastFrameChangeTime = time;
currentFrame++;
if (currentFrame >= frameCount) {
currentFrame = 0;
}
}
}
frameToDraw.left = currentFrame * frameWidth;
frameToDraw.right = frameToDraw.left + frameWidth;
}
public void draw() {
if (ourHolder.getSurface().isValid()) {
canvas = ourHolder.lockCanvas();
canvas.drawColor(Color.WHITE);
whereToDraw.set((int) manXPos, (int) manYPos, (int) manXPos
+ frameWidth, (int) manYPos + frameHeight);
manageCurrentFrame();
canvas.drawBitmap(bitmapRunningMan, frameToDraw, whereToDraw, null);
ourHolder.unlockCanvasAndPost(canvas);
}
}
public void pause() {
playing = false;
try {
gameThread.join();
} catch(InterruptedException e) {
Log.e("ERR", "Joining Thread");
}
}
public void resume() {
playing = true;
gameThread = new Thread(this);
gameThread.start();
}
// public boolean onTouchEvent(MotionEvent event) {
// switch (event.getActionMasked() & MotionEvent.ACTION_MASK) {
// case MotionEvent.ACTION_DOWN :
// isMoving = !isMoving;
// break;
// }
//
// return true;
// }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:focusableInTouchMode="true"
android:color="#000000"
android:weightSum="1" android:background="#ffffff" android:orientation="vertical"
android:id="#+id/theScreen">
<ImageView
android:layout_width="113dp"
android:layout_height="130dp"
android:src="#drawable/thelogo"
android:id="#+id/imageView" android:layout_gravity="center_horizontal"/>
<TextView
android:layout_width="145dp"
android:layout_height="49dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="combat"
android:textSize="30sp"
android:textColor="#000000"
android:id="#+id/nameTextView" android:layout_gravity="right" android:layout_weight="0.06"/>
<EditText
android:layout_width="270dp"
android:layout_height="wrap_content"
android:hint="Username"
android:textColorHint="#808080"
android:textColor="#22272a"
android:id="#+id/userNameEditText" android:layout_gravity="center_horizontal"/>
<Button
android:layout_width="261dp"
android:layout_height="wrap_content"
android:text="Next"
android:drawableRight="#drawable/thearrow"
android:textColor="#000000"
android:onClick="beginMotion"
android:id="#+id/NextButton" android:layout_gravity="center_horizontal" android:background="#638455"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Create an account"
android:id="#+id/textView" android:layout_gravity="center_horizontal" android:layout_weight="0.07"
android:textColor="#22272A"/>
<LinearLayout
android:id="#+id/gameLayoutView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
</LinearLayout>
</LinearLayout>
You haven't attached myLayout to the activity main layout that you earlier set on setContentView(R.layout.main).
Instead of:
LinearLayout myLayout = new LinearLayout(this);
myLayout.findViewById(R.id.gameLayoutView);
myLayout.addView(gameView);
You should write this:
LinearLayout myLayout = (LinearLayout) findViewById(R.id.gameLayoutView);
myLayout.addView(gameView);

How to change a textview from a custom view's class

Let's say I have a custom view inside an activity and a TextView just below that custom view. I would like to change the TextView's text once the custom view was clicked but I seem to get a null pointer when I use findViewById(), so how can I do that?
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.yuvaleliav1gmail.quoridor_ye.MainActivity"
android:background="#dd7d23">
<com.yuvaleliav1gmail.quoridor_ye.ComBoardView
android:layout_width="900px"
android:layout_height="900px"
android:id="#+id/bview"
android:background="#drawable/game_board"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/radioGroup"
android:layout_below="#+id/bview"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Move Pawn"
android:id="#+id/radioPawn"
android:checked="true"
android:onClick="radioButtonClick"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Set VERTICAL Wall"
android:id="#+id/verticalRdio"
android:checked="false"
android:onClick="radioButtonClick"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Set HORIZONTAL Wall"
android:id="#+id/horizontalRdio"
android:checked="false"
android:onClick="radioButtonClick"/>
</RadioGroup>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Your turn"
android:id="#+id/turnText34"
android:layout_gravity="center_horizontal"
android:layout_below="#+id/radioGroup"
android:layout_alignLeft="#+id/bview"
android:layout_alignStart="#+id/bview" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Your walls left: "
android:id="#+id/yourWallsText"
android:layout_gravity="center_horizontal"
android:layout_below="#+id/turnText34"
android:layout_alignLeft="#+id/turnText34"
android:layout_alignStart="#+id/turnText34" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="10"
android:id="#+id/yourNumText"
android:layout_gravity="center_horizontal"
android:layout_alignTop="#+id/yourWallsText"
android:layout_toRightOf="#+id/yourWallsText"
android:layout_toEndOf="#+id/yourWallsText" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Opponent&apos;s walls left:"
android:id="#+id/opWallsText"
android:layout_gravity="center_horizontal"
android:layout_below="#+id/yourWallsText"
android:layout_alignLeft="#+id/yourWallsText"
android:layout_alignStart="#+id/yourWallsText" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="10"
android:id="#+id/opNumText"
android:layout_gravity="center_horizontal"
android:layout_alignTop="#+id/opWallsText"
android:layout_toRightOf="#+id/opWallsText"
android:layout_toEndOf="#+id/opWallsText" />
</RelativeLayout>
comBoardView is the custom view's class
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.os.CountDownTimer;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;
public class ComBoardView extends View {
private static final int ROWS = 9;
private static final int COLUMNS = 9;
public static Context con;
Paint paint;
GameService game;
TextView turns;
public static Point size = new Point();
/*
* constructor
*/
public ComBoardView(Context context, AttributeSet attrs) {
super(context, attrs);
con = context;
paint = new Paint();
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
display.getSize(size);
turns = (TextView) findViewById(R.id.turnText34);
}
/*
*/
public boolean onTouchEvent( MotionEvent event ) {
game = GameService.getInstance();
int x = (int)event.getX() / 100;
int y = (int)event.getY() / 100;
if ( event.getAction() != MotionEvent.ACTION_UP )
return true;
if(game.movePawn){
if(game.turn % 2 == 0){
if(game.isLegalMove(game.ai.MyLocation , y * ROWS + x)){
game.board.ClrPlayer(game.ai);
game.ai.MyLocation = y * ROWS + x;
game.board.SetPlayer(game.ai);
if(turns != null){
turns.setText("white's turn");
}
game.turn++;
}
}
else{
if(game.isLegalMove(game.player.MyLocation , y * ROWS + x)){
game.board.ClrPlayer(game.player);
game.player.MyLocation = y * ROWS + x;
game.board.SetPlayer(game.player);
if(turns != null){
turns.setText("black's turn");
}
game.turn++;
}
}
}
else {
if(((int)event.getX() % 100) < 50) x--;
if(((int)event.getY() % 100) < 50) y--;
if(game.setHWall){
game.board.SetHWall(y,x);
game.turn++;
}
else{
game.board.SetVWall(y,x);
game.turn++;
}
}
return true;
}
public void RestartTimer() {
new CountDownTimer(3500, 1000) {
public void onTick(long millisUntilFinished) {
final Toast toast = Toast.makeText(con, "restarting in: " + millisUntilFinished / 1000, Toast.LENGTH_LONG);
toast.show();
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
toast.cancel();
}
}, 1000);
}
public void onFinish() {
}
}.start();
}
protected void onDraw(Canvas canvas) {
paint.setAntiAlias(true);
paint.setColor(Color.BLACK);
GameService.getInstance().onDraw(canvas, paint);
}
}
and game is the activity's class
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;
import java.util.Timer;
public class Game extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
Timer timer = new Timer();
GameUpdateTimer ut = new GameUpdateTimer();
ut.boardView = (ComBoardView)this.findViewById(R.id.bview);
timer.schedule(ut, 200, 200);
RadioButton pawn = (RadioButton)findViewById(R.id.radioPawn);
RadioButton hWall = (RadioButton)findViewById(R.id.horizontalRdio);
RadioButton vWall = (RadioButton)findViewById(R.id.verticalRdio);
final GameService g = GameService.getInstance();
pawn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
g.movePawn = true;
g.setHWall = false;
}});
hWall.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
g.movePawn = false;
g.setHWall = true;
}
});
vWall.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
g.movePawn = false;
g.setHWall = false;
}
});
}
}
You can't call turns = (TextView) findViewById(R.id.turnText34); from your custom view, it will always return Null because TextView exist in the activity xml and not in ComBoardView.
What you can do, is to instantiate your TextView in the activity, then add a clickListener to your ComBoardView.
TextView turns;
#Override
protected void onCreate(Bundle savedInstanceState) {
//...
turns = (TextView) findViewById(R.id.turnText34);
ut.boardView.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
turns.setText("ComBoardView was clicked!");
}});
//...
}

Custom view is being displayed as nested views in Component Tree

I have created custom view which extends FrameLayout. After adding it into RelativeLayout it's being displayed as two nested views:
Is it normal? It sometimes messes up with wrap_content flags but I couldn't figure out why. When I use View as a base class everything looks normal.
Here is my code:
MainActivity.java
package com.example.app;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.SeekBar;
import com.codersmill.tset.R;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.example.app.RateBar
android:layout_width="match_parent"
android:layout_height="48dp"
android:id="#+id/rateBar"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
</RelativeLayout>
RateBar.java
package com.example.app;
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;
import com.codersmill.tset.R;
public class RateBar extends FrameLayout {
TextView star1, star2, star3, star4, star5;
View dot;
private boolean isAnimating = false;
int radius = 36;
private int step = 100;
private int leftMargin = 50;
private int topMargin = 0;
private int currentPosition = 0;
public RateBar(Context context) {
this(context, null);
}
public RateBar(Context context, AttributeSet attrs) {
super(context, attrs);
View.inflate(context, R.layout.view_rate_bar, this);
star1 = (TextView) this.findViewById(R.id.star1);
star2 = (TextView) this.findViewById(R.id.star2);
star3 = (TextView) this.findViewById(R.id.star3);
star4 = (TextView) this.findViewById(R.id.star4);
star5 = (TextView) this.findViewById(R.id.star5);
star1.setOnClickListener(onClickListener);
star2.setOnClickListener(onClickListener);
star3.setOnClickListener(onClickListener);
star4.setOnClickListener(onClickListener);
star5.setOnClickListener(onClickListener);
dot = this.findViewById(R.id.selector);
}
public RateBar(Context context, AttributeSet attrs, int defStyleAttr) {
this(context, attrs);
}
private OnClickListener onClickListener = new OnClickListener() {
#Override public void onClick(View v) {
if(isAnimating) return;
switch (v.getId()) {
case R.id.star1:
animateToPosition(0);
break;
case R.id.star2:
animateToPosition(1);
break;
case R.id.star3:
animateToPosition(2);
break;
case R.id.star4:
animateToPosition(3);
break;
case R.id.star5:
animateToPosition(4);
break;
}
}
};
#Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
step = (int) (getMeasuredWidth() / 5.0f);
leftMargin = (int) (step / 2.0f - radius);
topMargin = (int) (getMeasuredHeight() / 2.0f - radius);
if(!isAnimating) {
FrameLayout.LayoutParams params = (LayoutParams) dot.getLayoutParams();
params.leftMargin = leftMargin + currentPosition * step;
params.topMargin = topMargin;
dot.setLayoutParams(params);
}
}
private void animateToPosition(final int position) {
final int from = currentPosition*step + leftMargin;
final int to = position*step + leftMargin;
ValueAnimator animation = ValueAnimator.ofInt(from, to);
animation.setDuration(250);
animation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override public void onAnimationUpdate(ValueAnimator animation) {
FrameLayout.LayoutParams params = (LayoutParams) dot.getLayoutParams();
params.leftMargin = (int) animation.getAnimatedValue();
params.topMargin = topMargin;
dot.setLayoutParams(params);
}
});
animation.addListener(new AnimatorListenerAdapter() {
#Override public void onAnimationStart(Animator animation) {
super.onAnimationStart(animation);
isAnimating = true;
FrameLayout.LayoutParams params = (LayoutParams) dot.getLayoutParams();
params.topMargin = topMargin;
params.leftMargin = currentPosition * step + leftMargin;
dot.setLayoutParams(params);
}
#Override public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
isAnimating = false;
currentPosition = position;
FrameLayout.LayoutParams params = (LayoutParams) dot.getLayoutParams();
params.leftMargin = currentPosition * step + leftMargin;
params.topMargin = topMargin;
dot.setLayoutParams(params);
}
});
animation.start();
}
private void updateDotPosition() {
}
}
view_rate_bar.xml
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="wrap_content">
<View
android:id="#+id/selector"
android:layout_width="24dp"
android:layout_height="24dp"
android:background="#drawable/oval"
/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="1"
android:id="#+id/star1"
android:layout_weight="1"
android:gravity="center"
android:padding="12dp"
android:background="#2200ff00"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="2"
android:id="#+id/star2"
android:layout_weight="1"
android:gravity="center"
android:padding="12dp"
android:background="#22ff0000"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="3"
android:id="#+id/star3"
android:layout_weight="1"
android:gravity="center"
android:padding="12dp"
android:background="#2200ff00"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="4"
android:id="#+id/star4"
android:layout_weight="1"
android:gravity="center"
android:padding="12dp"
android:background="#22ff0000"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="5"
android:id="#+id/star5"
android:layout_weight="1"
android:gravity="center"
android:padding="12dp"
android:background="#2200ff00" />
</LinearLayout>
</merge>
even i was facing the same issue, it appears that new version of android studio comes up with two files content_main.xml and activity_mail.xml , when we select Activity_main.xml>Design view everything appears 'Custom View' , instead when we highlight content_main.xml>Design, everything is bac to normal. I dont know why it happens but that;s how i fix mine ( android nooob here )
More can be found here : https://teamtreehouse.com/community/i-cant-drag-widgets-onto-the-phone-mockup-component-tree-shows-customview-instead-of-the-relative-view-help

How to do sliding menu from right to left

I searching some sliding example,
but I still couldn't find for right to left sliding example.
Please can any one give sample project for sliding menu from right to left
package com.slidingmenu.example;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import com.slidingmenu.example.fragments.ColorFragment;
import com.slidingmenu.lib.SlidingMenu;
import com.slidingmenu.lib.SlidingMenu.OnClosedListener;
import com.slidingmenu.lib.SlidingMenu.OnOpenedListener;
public class LeftAndRightActivity extends BaseActivity {
public LeftAndRightActivity() {
super(R.string.left_and_right);
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSlidingMenu().setMode(SlidingMenu.LEFT_RIGHT);
getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
setContentView(R.layout.content_frame);
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.content_frame, new SampleListFragment())
.commit();
getSlidingMenu().setSecondaryMenu(R.layout.menu_frame_two);
getSlidingMenu().setSecondaryShadowDrawable(R.drawable.shadowright);
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.menu_frame_two, new SampleListFragment())
.commit();
}
}
Make sure to get a clean and updated copy of the library as well. Just in case.
Slide menu can be done using animation classes.
Declare inside activity:
private LinearLayout slidingPanel;
private boolean isExpanded;
private DisplayMetrics metrics;
//private ListView listView;
private RelativeLayout headerPanel,menuPanel;
private int panelWidth;
private ImageView menuViewButton;
FrameLayout.LayoutParams menuPanelParameters;
FrameLayout.LayoutParams slidingPanelParameters;
LinearLayout.LayoutParams headerPanelParameters ;
LinearLayout.LayoutParams listViewParameters;
//Initialize inside oncreate
metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
panelWidth = (int) ((metrics.widthPixels)*0.45);
headerPanel = (RelativeLayout) findViewById(R.id.header);
headerPanelParameters = (LinearLayout.LayoutParams) headerPanel.getLayoutParams();
headerPanelParameters.width = metrics.widthPixels;
headerPanel.setLayoutParams(headerPanelParameters);
menuPanel = (RelativeLayout) findViewById(R.id.menuPanel);
menuPanelParameters = (FrameLayout.LayoutParams) menuPanel.getLayoutParams();
menuPanelParameters.width = panelWidth;
menuPanel.setLayoutParams(menuPanelParameters);
slidingPanel = (LinearLayout) findViewById(R.id.slidingPanel);
slidingPanelParameters = (FrameLayout.LayoutParams) slidingPanel.getLayoutParams();
slidingPanelParameters.width = metrics.widthPixels;
slidingPanel.setLayoutParams(slidingPanelParameters);
Slide the Panel:
menuViewButton = (ImageView) findViewById(R.id.menuViewButton);
menuViewButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(!isExpanded){
isExpanded = true;
//Expand
new ExpandAnimation(slidingPanel, panelWidth,
Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, 0.45f, 0, 0.0f, 0, 0.0f);
//Toast.makeText(getApplicationContext(), "expand", 0).show();
}else{
isExpanded = false;
//Collapse
new CollapseAnimation(slidingPanel,panelWidth,
TranslateAnimation.RELATIVE_TO_SELF, 0.45f,
TranslateAnimation.RELATIVE_TO_SELF, 0.0f, 0, 0.0f, 0, 0.0f);
//Toast.makeText(getApplicationContext(), "Collapse", 0).show();
}
}
});
Collapse animation class:
public class CollapseAnimation extends TranslateAnimation implements TranslateAnimation.AnimationListener{
private LinearLayout slidingLayout;
int panelWidth;
public CollapseAnimation(LinearLayout layout, int width, int fromXType, float fromXValue, int toXType,
float toXValue, int fromYType, float fromYValue, int toYType, float toYValue) {
super(fromXType, fromXValue, toXType, toXValue, fromYType, fromYValue, toYType, toYValue);
//Initialize
slidingLayout = layout;
panelWidth = width;
setDuration(400);
setFillAfter( false );
setInterpolator(new AccelerateDecelerateInterpolator());
setAnimationListener(this);
//Clear left and right margins
LayoutParams params = (LayoutParams) slidingLayout.getLayoutParams();
params.rightMargin = 0;
params.leftMargin = 0;
slidingLayout.setLayoutParams(params);
slidingLayout.requestLayout();
slidingLayout.startAnimation(this);
//slidingLayout.setBackgroundColor();
//slidingLayout.setBackgroundColor(R.string.white);
}
#Override
public void onAnimationEnd(Animation arg0) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationRepeat(Animation arg0) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationStart(Animation arg0) {
// TODO Auto-generated method stub
}
}
Expand animation class:
public class ExpandAnimation extends TranslateAnimation implements Animation.AnimationListener{
private LinearLayout slidingLayout;
int panelWidth;
public ExpandAnimation(LinearLayout layout, int width, int fromXType, float fromXValue, int toXType,
float toXValue, int fromYType, float fromYValue, int toYType, float toYValue) {
super(fromXType, fromXValue, toXType, toXValue, fromYType, fromYValue, toYType, toYValue);
//Initialize
slidingLayout = layout;
panelWidth = width;
setDuration(400);
setFillAfter( false );
setInterpolator(new AccelerateDecelerateInterpolator());
setAnimationListener(this);
slidingLayout.startAnimation(this);
//slidingLayout.setBackgroundColor(panelWidth);
}
#Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
//Create margin and align left
LayoutParams params = (LayoutParams) slidingLayout.getLayoutParams();
params.leftMargin = panelWidth;
params.gravity = Gravity.LEFT;
slidingLayout.clearAnimation();
slidingLayout.setLayoutParams(params);
slidingLayout.requestLayout();
}
#Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
}
Here is the xml:
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<!-- Menu Panel -->
<RelativeLayout
android:id="#+id/menuPanel"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#drawable/gray_bg"
android:gravity="right"
android:orientation="vertical" >
<TextView
android:id="#+id/menu_title_1"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_alignParentTop="true"
android:layout_marginLeft="0dp"
android:background="#353535"
android:gravity="center_vertical"
android:paddingLeft="15dp"
android:text="#string/menu_title"
android:textColor="#android:color/white" >
</TextView>
<View
android:id="#+id/menu_item_divider_1"
android:layout_width="fill_parent"
android:layout_height="0.5dp"
android:layout_below="#+id/menu_title_1"
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp"
android:background="#b5b5b5" />
<TextView
android:id="#+id/menu_item_1"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_below="#+id/menu_item_divider_1"
android:layout_marginLeft="15dp"
android:gravity="center_vertical"
android:text="#string/korean" >
</TextView>
<View
android:id="#+id/menu_item_divider_2"
android:layout_width="fill_parent"
android:layout_height="0.5dp"
android:layout_below="#+id/menu_item_1"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:background="#b5b5b5" />
<TextView
android:id="#+id/menu_item_2"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_below="#+id/menu_item_divider_2"
android:layout_marginLeft="15dp"
android:gravity="center_vertical"
android:text="#string/english"
>
</TextView>
<!-- Sliding Panel -->
<LinearLayout
android:id="#+id/slidingPanel"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#android:color/white"
android:gravity="left"
android:orientation="vertical" >
<View
android:id="#+id/dividerHeaderBottom"
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#414141" />
<RelativeLayout
android:id="#+id/header"
android:layout_width="match_parent"
android:layout_height="45dp"
android:background="#color/whitec" >
<Button
android:id="#+id/buttonback"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignBottom="#+id/buttonperson"
android:layout_alignParentTop="true"
android:layout_marginRight="14dp"
android:layout_toLeftOf="#+id/buttonperson"
android:background="#drawable/back" />
<ImageView
android:id="#+id/menuViewButton"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_centerVertical="true"
android:layout_marginLeft="27dp"
android:layout_toRightOf="#+id/buttonyummy"
android:clickable="true"
android:contentDescription="#string/description"
android:src="#drawable/lin"
android:visibility="visible" />
</RelativeLayout>
</LinearLayout>
</FrameLayout>
use this library.. it has right to left sliding drawer also..
in the onCreate() of the activity or wherever suitable you can use following implementation..
rightDrawer = new DrawerBuilder()
.withActivity(this)
.withDisplayBelowStatusBar(true)
.withSavedInstance(savedInstanceState)
.withDrawerGravity(Gravity.END).append(leftDrawer); // or use build() instead of append()
it will add the right to left sliding drawer...

Sliding Menu - From Right to left

I was trying with Sliding menu from the below example.
https://github.com/eddieringle/android-undergarment
But this has the sliding implementation from left to right. How can i change this to Right to left. Can any one please help me to figure this out?
i know you get your answer but if you don't want to use any external library you can use this:
this code exist on gitHub
but this project use for left right menu, with a little change you can open from right to left, i copy code with changes. with following code you can have both side menu.
CollapseAnimation.java
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.FrameLayout.LayoutParams;
import android.widget.LinearLayout;
public class CollapseAnimation extends TranslateAnimation implements TranslateAnimation.AnimationListener{
private LinearLayout slidingLayout;
int panelWidth;
public CollapseAnimation(LinearLayout layout, int width, int fromXType, float fromXValue, int toXType,
float toXValue, int fromYType, float fromYValue, int toYType, float toYValue) {
super(fromXType, fromXValue, toXType, toXValue, fromYType, fromYValue, toYType, toYValue);
//Initialize
slidingLayout = layout;
panelWidth = width;
setDuration(400);
setFillAfter( false );
setInterpolator(new AccelerateDecelerateInterpolator());
setAnimationListener(this);
//Clear left and right margins
LayoutParams params = (LayoutParams) slidingLayout.getLayoutParams();
params.rightMargin = 0;
params.leftMargin = 0;
slidingLayout.setLayoutParams(params);
slidingLayout.requestLayout();
slidingLayout.startAnimation(this);
}
public void onAnimationEnd(Animation animation) {
}
public void onAnimationRepeat(Animation animation) {
}
public void onAnimationStart(Animation animation) {
}
}
ExpandAnimation.java
import android.view.Gravity;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.FrameLayout.LayoutParams;
import android.widget.LinearLayout;
public class ExpandAnimation extends TranslateAnimation implements Animation.AnimationListener{
private LinearLayout slidingLayout;
int panelWidth;
public ExpandAnimation(LinearLayout layout, int width, int fromXType, float fromXValue, int toXType,
float toXValue, int fromYType, float fromYValue, int toYType, float toYValue) {
super(fromXType, fromXValue, toXType, toXValue, fromYType, fromYValue, toYType, toYValue);
//Initialize
slidingLayout = layout;
panelWidth = width;
setDuration(400);
setFillAfter( false );
setInterpolator(new AccelerateDecelerateInterpolator());
setAnimationListener(this);
slidingLayout.startAnimation(this);
}
public void onAnimationEnd(Animation arg0) {
//Create margin and align left
LayoutParams params = (LayoutParams) slidingLayout.getLayoutParams();
params.rightMargin = panelWidth;
params.gravity = Gravity.RIGHT;
slidingLayout.clearAnimation();
slidingLayout.setLayoutParams(params);
slidingLayout.requestLayout();
}
public void onAnimationRepeat(Animation arg0) {
}
public void onAnimationStart(Animation arg0) {
}
}
LayerStack.java
import android.os.Bundle;
import android.app.Activity;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ListView;
public class LayerStack extends Activity {
//Declare
private LinearLayout slidingPanel;
private boolean isExpanded;
private DisplayMetrics metrics;
private ListView listView;
private RelativeLayout headerPanel;
private RelativeLayout menuPanel;
private int panelWidth;
private ImageView menuViewButton;
FrameLayout.LayoutParams menuPanelParameters;
FrameLayout.LayoutParams slidingPanelParameters;
LinearLayout.LayoutParams headerPanelParameters ;
LinearLayout.LayoutParams listViewParameters;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_layer_stack);
//Initialize
metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
panelWidth = (int) ((metrics.widthPixels)*0.33);
headerPanel = (RelativeLayout) findViewById(R.id.header);
headerPanelParameters = (LinearLayout.LayoutParams) headerPanel.getLayoutParams();
headerPanelParameters.width = metrics.widthPixels;
headerPanel.setLayoutParams(headerPanelParameters);
menuPanel = (RelativeLayout) findViewById(R.id.menuPanel);
menuPanelParameters = (FrameLayout.LayoutParams) menuPanel.getLayoutParams();
menuPanelParameters.gravity = Gravity.RIGHT;
menuPanelParameters.width = panelWidth;
menuPanel.setLayoutParams(menuPanelParameters);
slidingPanel = (LinearLayout) findViewById(R.id.slidingPanel);
slidingPanelParameters = (FrameLayout.LayoutParams) slidingPanel.getLayoutParams();
slidingPanelParameters.width = metrics.widthPixels;
slidingPanelParameters.gravity = Gravity.LEFT;
slidingPanel.setLayoutParams(slidingPanelParameters);
listView = (ListView) findViewById(R.id.list);
listViewParameters = (LinearLayout.LayoutParams) listView.getLayoutParams();
listViewParameters.width = metrics.widthPixels;
listView.setLayoutParams(listViewParameters);
//Slide the Panel
menuViewButton = (ImageView) findViewById(R.id.menuViewButton);
menuViewButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(!isExpanded){
isExpanded = true;
//Expand
new ExpandAnimation(slidingPanel, panelWidth,
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, -0.33f, 0, 0.0f, 0, 0.0f);
// if you want left to right just remove ( - ) before 0.33f
}else{
isExpanded = false;
//Collapse
new CollapseAnimation(slidingPanel,panelWidth,
TranslateAnimation.RELATIVE_TO_PARENT,-0.33f,
TranslateAnimation.RELATIVE_TO_PARENT, 0.0f, 0, 0.0f, 0, 0.0f);
// if you want left to right just remove ( - ) before 0.33f
}
}
});
}
}
activity_layer_stack.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#drawable/blue_bg">
<!-- Menu Panel -->
<RelativeLayout
android:id="#+id/menuPanel"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:gravity="right"
android:background="#drawable/gray_bg"
android:orientation="vertical" >
<TextView
android:id="#+id/menu_title_1"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_alignParentTop="true"
android:layout_marginLeft="0dp"
android:paddingLeft="15dp"
android:gravity="center_vertical"
android:background="#353535"
android:textColor="#android:color/white"
android:text="#string/menu_title">
</TextView>
<View
android:id="#+id/menu_item_divider_1"
android:layout_width="fill_parent"
android:layout_height="0.5dp"
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp"
android:layout_below="#+id/menu_title_1"
android:background="#b5b5b5"/>
<TextView
android:id="#+id/menu_item_1"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_marginLeft="15dp"
android:gravity="center_vertical"
android:layout_below="#+id/menu_item_divider_1"
android:text="#string/item_1">
</TextView>
<View
android:id="#+id/menu_item_divider_2"
android:layout_width="fill_parent"
android:layout_height="0.5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_below="#+id/menu_item_1"
android:background="#b5b5b5"/>
<TextView
android:id="#+id/menu_item_2"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_below="#+id/menu_item_divider_2"
android:layout_marginLeft="15dp"
android:gravity="center_vertical"
android:text="#string/item_2">
</TextView>
<View
android:id="#+id/menu_item_divider_3"
android:layout_width="fill_parent"
android:layout_height="0.5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_below="#+id/menu_item_2"
android:background="#b5b5b5" />
</RelativeLayout>
<!-- Sliding Panel -->
<LinearLayout
android:id="#+id/slidingPanel"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="right"
android:orientation="vertical"
android:background="#android:color/white" >
<RelativeLayout
android:id="#+id/header"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#drawable/blue_bg" >
<View
android:id="#+id/header_vertical_divider_1"
android:layout_width="2dp"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/menuViewButton"
android:background="#drawable/engraved_bg" />
<ImageView
android:id="#+id/menuViewButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:clickable="true"
android:contentDescription="#string/description"
android:src="#drawable/icon_menu"
android:visibility="visible" />
</RelativeLayout>
<View
android:id="#+id/dividerHeaderBottom"
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#414141" />
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:divider="#b5b5b5"
android:dividerHeight="0.5dp"
android:background="#android:color/white"
>
</ListView>
</LinearLayout>
</FrameLayout>
https://github.com/jfeinstein10/SlidingMenu
Try this. It has different modes like from right to left and many other customizations

Categories

Resources