-1

I have this layout with 2 buttons but they are attached side by side, How can I put a gap or spacing between them. And how can I make the buttons change color when I click

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="30dp" >
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="20dp" >
    <Button
        android:id="@+id/login"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.40"
        android:background="#FF55FF"
        android:text="Login"
        android:textColor="#FFFFFF" />
    <Button
        android:id="@+id/register"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.40"
        android:background="#FF55FF"
        android:text="Register"
        android:textColor="#FFFFFF" />
</LinearLayout>

3 Answers3

1

using margin (left, right, bottom and top) you can elegantly position your UI elements

Ali Kazmi
  • 1,460
  • 9
  • 22
0

I have just given layout_margin for buttons as 5dp. Choose accordingly to your requirement

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:padding="30dp" >
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="20dp"
            android:paddingTop="20dp" >
            <Button
                android:id="@+id/login"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="0.40"
                android:background="#FF55FF"
                android:text="Login"
                android:layout_margin="5dp"
                android:textColor="#FFFFFF" />
            <Button
                android:id="@+id/register"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="0.40"
                android:background="#FF55FF"
                android:text="Register"
                android:layout_margin="5dp"
                android:textColor="#FFFFFF" />
        </LinearLayout>
        </LinearLayout>
m0rpheu5
  • 600
  • 4
  • 16
0

Reference for your question about color change on click. To change the color of the button on click you need to implement event handlers such as onClickListener in your source file. You can refer to below code for example. Implement this code in onCreate method in your java source file...

//initializing your buttons first
  Button button1 = (Button)findViewById(R.id.button1id);
  button1.setOnClickListener(new OnClickListener() {
  button1.setBackgroundResource(R.color.Red);
  });

The color should already be defined in the values. Create a folder named colors in values and define various colors.