I am trying to do a simple task of moving a toolbar up and down on the screen, but it only works once. The toolbar moves on the press of a button to the top, but I cannot get it back to the bottom even though the message “move to bottom” appears. See the code:
boolean toolBarAtBottom = true;
private void moveToolBar(){
LinearLayout toolBar = (LinearLayout) findViewById(R.id.toolBar);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
lp.height = toolBar.getHeight();
if (toolBarAtBottom){
lp.addRule(RelativeLayout.ALIGN_TOP, RelativeLayout.TRUE);
}else{
lp.addRule(RelativeLayout.ALIGN_BOTTOM, RelativeLayout.TRUE);
Toast.makeText(DrawActivity.this, "move to bottom", Toast.LENGTH_SHORT).show();
}
toolBar.setLayoutParams(lp);
toolBarAtBottom = !toolBarAtBottom;
}
Any ideas how to make it do more than once?
You want to use
RelativeLayout.ALIGN_PARENT_BOTTOMinstead ofRelativeLayout.ALIGN_BOTTOM.ALIGN_BOTTOMaligns the bottom of a view with the bottom of another view.ALIGN_PARENT_BOTTOMwill move it to the bottom of its parent container.Here is a link to the docs: http://developer.android.com/reference/android/widget/RelativeLayout.html