0% found this document useful (0 votes)
31 views3 pages

04 Hands On Act 1

The document shows code for creating a linear layout with 12 buttons and a relative layout with 15 buttons in Android. It includes code to add buttons to the linear layout with vertical orientation and margins between buttons. The relative layout code positions each button below the previous and aligned to the right edge.

Uploaded by

busaing.120201
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views3 pages

04 Hands On Act 1

The document shows code for creating a linear layout with 12 buttons and a relative layout with 15 buttons in Android. It includes code to add buttons to the linear layout with vertical orientation and margins between buttons. The relative layout code positions each button below the previous and aligned to the right edge.

Uploaded by

busaing.120201
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Name: Remedios Mae Busaing

BSIT 603

04 Hands on Activity 1

Linear Layout with buttons:

import android.os.Bundle;
import android.widget.Button;
import android.widget.LinearLayout;

import androidx.appcompat.app.AppCompatActivity;

public class LinearLayoutActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

// Create a LinearLayout with vertical orientation


LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);

// Create and add 12 buttons to the LinearLayout


for (int i = 1; i <= 12; i++) {
Button button = new Button(this);
button.setText("Button " + i);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT
);
layoutParams.setMargins(0, 20, 0, 0); // Set margins between buttons
button.setLayoutParams(layoutParams);
linearLayout.addView(button);
}

setContentView(linearLayout);
}
}

Relative Layout with 15 buttons:


import android.os.Bundle;
import android.widget.Button;
import android.widget.RelativeLayout;

import androidx.appcompat.app.AppCompatActivity;

public class RelativeLayoutActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

// Create a RelativeLayout
RelativeLayout relativeLayout = new RelativeLayout(this);

// Create buttons and add them to the RelativeLayout with relative positioning
for (int i = 1; i <= 15; i++) {
Button button = new Button(this);
button.setId(i); // Set unique IDs for each button
button.setText("Button " + i);

RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(


RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT
);

if (i == 1) {
// Align the first button to the parent top-left corner
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
} else {
// Align subsequent buttons below the previous one
layoutParams.addRule(RelativeLayout.BELOW, i - 1);
// Align buttons to the parent's right edge
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
}

button.setLayoutParams(layoutParams);
relativeLayout.addView(button);
}

setContentView(relativeLayout);
}
}

You might also like