Skip to content

Instantly share code, notes, and snippets.

@alorma
Created August 26, 2014 18:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alorma/0a280a3f4c6ad67d2f43 to your computer and use it in GitHub Desktop.
Save alorma/0a280a3f4c6ad67d2f43 to your computer and use it in GitHub Desktop.
Auto creates fab button at bottom of top view
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="FABCenterLayout">
<attr name="top_id" format="reference" />
</declare-styleable>
</resources>
package android.widget;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;
import com.alorma.github.R;
/**
* Created by Bernat on 26/08/2014.
*/
public class FABCenterLayout extends LinearLayout {
private View fabView;
private int topId;
public FABCenterLayout(Context context) {
super(context);
init(null, 0);
}
public FABCenterLayout(Context context, AttributeSet attrs) {
super(context, attrs);
init(attrs, 0);
}
public FABCenterLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(attrs, defStyle);
}
private void init(AttributeSet attrs, int defStyle) {
isInEditMode();
if (attrs != null) {
TypedArray attr = getContext().obtainStyledAttributes(attrs, R.styleable.FABCenterLayout, defStyle, 0);
if (attr.hasValue(R.styleable.FABCenterLayout_top_id)) {
topId = attr.getResourceId(R.styleable.FABCenterLayout_top_id, 0);
if (topId != 0) {
fabView = new Button(getContext());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
fabView.setBackground(getResources().getDrawable(R.drawable.fab_inv_button));
} else {
fabView.setBackgroundDrawable(getResources().getDrawable(R.drawable.fab_inv_button));
}
}
}
}
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
if (getChildCount() > 1) {
if (topId != 0 && fabView != null) {
//if (fabView.getParent() == null) {
View topView = findViewById(topId);
if (topView != null) {
int bottom = topView.getHeight();
if (bottom > 0) {
int int56 = getResources().getDimensionPixelOffset(R.dimen.fab);
int int16 = getResources().getDimensionPixelOffset(R.dimen.gapLarge);
fabView.layout(r - int56 - int16, bottom - int56 / 2, r - int16, bottom + int56 / 2);
removeView(fabView);
addView(fabView);
}
}
//}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment