0

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.

Question: How can I make it so that the second button doesn't cover the first button?

This is the 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;
    }

}

and xml

<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" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/hello_world" />

    <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" >
    </LinearLayout>

</RelativeLayout>

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.

Garzahd
  • 5,739
  • 3
  • 18
  • 19
user1302569
  • 7,131
  • 13
  • 46
  • 66

2 Answers2

1

I think its because of the negative margin you set :

lp.setMargins(-30, 0, 0, 0);

remove it or set them at 0 (its equivalent since 0 is the default value):

lp.setMargins(0, 0, 0, 0);

thoses negativbe margins displace the button2 to the left so its behind the other button.

You can't manage overlapping in LinearLayout (that's why a bringToFront action change order in the line and not overlapping order). you need to use a RelativeLayout, set the button 2 to be on the right of button1, let the margin to -30 and call bringToFront on the second button it shoulc work ;)

Guian
  • 4,563
  • 4
  • 34
  • 54
0

You can set for your @id/layoutmadafaka the property android:weightSum=1.0 in xml. And when you add the buttons, set for each layout_weight=0.5, layout_width=0.

Example here: Linear Layout and weight in Android

Community
  • 1
  • 1
agamov
  • 4,407
  • 1
  • 27
  • 31