1

I am trying to create button at runtime.I am getting the coordinate and height,width of button from backend and I have to create button at same location on run time.I have used following code.

Button btn=new Button(getApplicationContext());
btn.setText("CLICK ME");
@SuppressWarnings("deprecation")
AbsoluteLayout.LayoutParams param = new AbsoluteLayout.LayoutParams( 121,  58,  50,  50);
btn.setLayoutParams(param);
mParentView.addView(btn);

My xml is

<FrameLayout
    android:id="@+id/frameView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <ViewFlipper
        android:id="@+id/viewFlipper"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    </ViewFlipper>

    <fragment
        android:id="@+id/header_fragment"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        class="com.sdei.presentationapp.activity.PresentationModeTopBar"
        tools:layout="@layout/presentation_top_bar" />
</FrameLayout>

here the parentview is framelayout.

I am able to create the button but the problem is it is created always at top left corner,no matter what coordinate we pass.Please help. Thanks in advance.

swati
  • 1,263
  • 2
  • 17
  • 27

3 Answers3

2

You cannot set button at your desired position in framelayout only possible in absolute layout. but you can use margin with respect to your left and top which will work like your (x, y) coordinates.

// First create your button:

 Button test_button = new Button(getApplicationContext());
 test_button.setText("test");

// Then create layout params for you buttons.

 FrameLayout.LayoutParams lp = new LayoutParams(100, 100); // Button width and button height.
 lp.leftMargin = 200; // your X coordinate.
 lp.topMargin = 300;  // your Y coordinate.

// Then find layout and add button on it.

 FrameLayout layout = (FrameLayout) findViewById(R.id.FrameLayout1);
 layout.addView(test_button, lp);

Hope this can help you.

Bharat Sharma
  • 3,926
  • 2
  • 17
  • 29
1

I have not compile this code, so change as per your layout.

LinearLayout ll = new LinearLayout(this);
fl .setOrientation(LinearLayout.VERTICAL);

LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
     LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);

layoutParams.setMargins(30, 20, 30, 0);

Button okButton=new Button(this);
okButton.setId(ok_id)
okButton.setText("some text");
  • See one reference link here
Community
  • 1
  • 1
Chintan Khetiya
  • 15,962
  • 9
  • 47
  • 85
0

The absolute layout class is deprecated, you are encouraged to use Frame Layout or Relative layout instead.
The reason of that is it won’t be compatible with all the android phones as they have different screen sizes and resolutions.
Absolute layout lays widgets by specifying their exact X and Y positions. In android the origin (0,0) coordinate is located at the top left of the screen. By default, if you define any control in absolute layout without defining it’s x,y coordinates, it will be placed in the origin point at (x,y)=(0,0);

Thus your defined position for AbsoluteLayout.LayoutParams is not working properly.
As you are using FrameLayout then you can try the following way-

// declare and initialize LayoutParams for the framelayout
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
            FrameLayout.LayoutParams.WRAP_CONTENT,
            FrameLayout.LayoutParams.WRAP_CONTENT);

// decide upon the positioning of the button //
// you will likely need to use the screen size to position the
// button anywhere other than the four corners
params.setMargins(.., .., .., ..);

// use static constants from the Gravity class
params.gravity = Gravity.CENTER_HORIZONTAL;
btn.setLayoutParams(params);
mParentView.addView(btn);
Pradip
  • 3,189
  • 3
  • 22
  • 27