Created
August 26, 2014 18:03
-
-
Save alorma/0a280a3f4c6ad67d2f43 to your computer and use it in GitHub Desktop.
Auto creates fab button at bottom of top view
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<resources> | |
<declare-styleable name="FABCenterLayout"> | |
<attr name="top_id" format="reference" /> | |
</declare-styleable> | |
</resources> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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