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.