Note that there are some explanatory texts on larger screens.

plurals
  1. POOne button cover other button in linear layout in android
    primarykey
    data
    text
    <p>I'm having a problem with adding elements in code. I want to add two buttons to a horizontal linear layout. My code works, but the second button partially covers the first button.</p> <p><strong>Question:</strong> How can I make it so that the second button doesn't cover the first button?</p> <p>This is the code:</p> <pre><code>public class MainActivity extends Activity { Button buttonFirst, buttonSecond; LinearLayout lau; LayoutParams params; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); buttonFirst = new Button(getApplicationContext()); buttonSecond = new Button(getApplicationContext()); lau = (LinearLayout) findViewById(R.id.layoutmadafaka); params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); lp.setMargins(-30, 0, 0, 0); buttonSecond.setLayoutParams(lp); buttonSecond.setBackgroundColor(Color.BLACK); lau.addView(buttonFirst,params); lau.addView(buttonSecond); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.activity_main, menu); return true; } } </code></pre> <p>and xml</p> <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" &gt; &lt;TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:text="@string/hello_world" /&gt; &lt;LinearLayout android:id="@+id/layoutmadafaka" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:orientation="horizontal" &gt; &lt;/LinearLayout&gt; &lt;/RelativeLayout&gt; </code></pre> <p>This must be done programmatically. When I use the bringToFront() method the buttons change positions but I don't want to do that. I want to first button on left side and second button next to first button.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload