0

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

2
  • you want to set the button at bottom center of it's parent? or do you want the button's text at that orientatioN?
    – stinepike
    May 10, 2013 at 20:06
  • I want to set the button at the bottom/center of itself. I don't want to adjust the text.
    – B. Money
    May 10, 2013 at 20:08

3 Answers 3

1

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.

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

            button.setLayoutParams(params);
2
  • Thanks for responding, but my button is in a relative layout so this answer doesn't work.
    – B. Money
    May 10, 2013 at 20:32
  • oh, I think it was not mentioned. anyways try the other answer
    – stinepike
    May 10, 2013 at 20:33
0

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

3
  • Thanks for responding, but this sets the button at the bottom of parent layout. I'm trying to set the the buttons point of reference at the bottom center of itself instead of its top left corner.
    – B. Money
    May 10, 2013 at 20:30
  • I don't understood as well, but think that changing the gravity of the parent layout should solve your problem May 10, 2013 at 20:33
  • I added a picture in Edit. Does it help you understand?
    – B. Money
    May 10, 2013 at 20:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.