Android : Calculate margins from coordinates in android screen

on Sunday, August 31, 2014


i have a library to insert floating buttons in a layout (http://prolificinteractive.com/blog/2014/07/24/android-floating-action-button-aka-fab/)


Then, i have this layout:



<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:fontawesometext="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginTop="32dp"
android:layout_marginBottom="16dp"
android:layout_marginRight="16dp">

<com.beardedhen.androidbootstrap.FontAwesomeText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="4dp"
android:textSize="45sp"
android:textColor="#737373"
android:id="@+id/faIcon"
fontawesometext:fa_icon="fa-question" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text='"texto de ejemplo de pregunta"'
android:id="@+id/tvQuestion"
android:textColor="@color/primary"
android:textStyle="bold"
android:textSize="28sp"
android:typeface="monospace"
android:layout_marginLeft="16dp"
android:layout_marginTop="32dp"
android:layout_marginBottom="16sp" />
</LinearLayout>

<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/frameConainter"
android:background="@color/dark_grey" />

</LinearLayout>


And i need to set the center of the button in the middle of framelayout top, like as the left image:


http://tabtec.com/wp-content/uploads/2014/06/andl3.jpg


I have this in a custom view:



public class YesNoReplyView extends ReplyParentView {

private FloatingActionButton buttonYes;
private FloatingActionButton buttonNo;

public YesNoReplyView(Context context) {
super(context);
}

public YesNoReplyView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public YesNoReplyView(Context context, AttributeSet attrs) {
super(context, attrs);
}

@Override
public void initializeComponents() {

int[] position = new int[2];
getLocationOnScreen(position);

buttonYes = new FloatingActionButton.Builder((Activity)getContext())
.withDrawable(getResources().getDrawable(R.drawable.ic_plus))
.withButtonColor(getResources().getColor(R.color.green_success))
.withGravity(Gravity.BOTTOM | Gravity.RIGHT)
.withMargins(0, 0, 16, 16)
.create();

buttonNo = new FloatingActionButton.Builder((Activity)getContext())
.withDrawable(getResources().getDrawable(R.drawable.ic_plus))
.withButtonColor(getResources().getColor(R.color.primary))
.withGravity(Gravity.BOTTOM | Gravity.RIGHT)
.withMargins(0, 0, 26, 26)
.create();

}


@Override
public String getResult() {
String result = "";
return result;
}
}


But i dont know how can i calculate the margins from the coordinates to set the view in the position i want..


Anyone knows any way better to do it this?(I cant use a XML layout..)


0 comments:

Post a Comment