android How to add button over other button?
<p>I'm trying to add button on other button exactly on same position with same width,and height.</p>

<p>I thought i can do that like these codes,</p>

<pre><code>public class MainActivity extends AppCompatActivity {

Button b1;
LinearLayout root;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    root = (LinearLayout)findViewById(R.id.activity_main);
    b1 = (Button)findViewById(R.id.b1);

}

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);

    ViewGroup.LayoutParams size = new ViewGroup.LayoutParams(b1.getWidth(),b1.getHeight());
    int[] locations = new int[2];
    b1.getLocationInWindow(locations);

    Button a = new Button(this);
    a.setLayoutParams(size);
    a.setText("new Button");
    a.setX(locations[0]);
    a.setY(locations[1]);
    root.addView(a);
}
</code></pre>

<p>}</p>

<p>and XML</p>

<pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="semo.msoft.myapplication.MainActivity"&gt;
&lt;Button
android:id="@+id/b1"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_marginLeft="50dp"
android:layout_marginTop="50dp"/&gt;

&lt;/LinearLayout&gt;
</code></pre>

<p>but, result was like this,</p>

<p><a href="https://i.stack.imgur.com/T4TGi.png" rel="nofollow">resultImage</a></p>

<p>I thought it should be correct but it seems be wrong, new button had little bit smaller width and height. Additionally , the position of new button was totally wrong.. I don't know why,, someone who know about this, please help</p>