B. Money
B. Money

Reputation: 931

How Do I Position Button From Bottom/Center?

I'm a noob to android and I'm having problems trying to position a button programmatically from a bottom/center point instead of the default top/left. My button has an arrow at the bottom and I want to set the position to the tip of the arrow. Any help is greatly appreciated.

MY CODE

popUpButton = (Button) findViewById(R.id.popUp);
    popUpButton.setOnClickListener(this); 

public void updateMsg(String t_info, float t_x, float t_y, int t_c){
     //infoView.updateInfo(t_info, t_x, t_y, t_c);
     popUpButton.setText(TouchView.touchInfo);       
     popUpButton.setX(t_x);
     popUpButton.setY(t_y);
}

XML

<RelativeLayout

<Button
    android:id="@+id/popUp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/balloon_overlay_bg_selector"
    android:text="Button"
    android:textColor="#000000"
    android:textSize="14dp" />


</RelativeLayout>

enter image description here

Upvotes: 0

Views: 309

Answers (3)

Mr. Bojangles
Mr. Bojangles

Reputation: 348

The best approach may be for you to take your y coordinate and then substract the height of the button using button.getheight and then do the same for your x coordinate.

Upvotes: 1

Leonardo Sapuy
Leonardo Sapuy

Reputation: 2740

You can do it using a relativeLayout as a parent, so check that u're using a Relative Layout...and do it:

RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) popUpButton.getLayoutParams();
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
params.addRule(RelativeLayout.CENTER_VERTICAL);
popUpButton.setLayoutParams(params);

So, there is the way that you can move or change objet's position progrmmatically

Upvotes: 0

stinepike
stinepike

Reputation: 54722

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT);
            params.weight = 1.0f;
            params.gravity=81;

            button.setLayoutParams(params);

Upvotes: 0

Related Questions