1.
LinearLayout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome to LinearLayout Example!"
android:textSize="18sp"
android:layout_gravity="center_horizontal"/>
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter some text"
android:inputType="text" />
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content
android:text="Submit"
android:layout_marginTop="16dp" />
</LinearLayout>
Java file
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity {
private TextView textView;
private EditText editText;
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
// Initialize views
textView = findViewById([Link]);
editText = findViewById([Link]);
button = findViewById([Link]);
// Set button click listener
[Link](new [Link]() {
@Override
public void onClick(View v) {
String inputText = [Link]().toString();
if (![Link]()) {
// Show a toast with the input text
[Link]([Link], "You entered: " + inputText,
Toast.LENGTH_SHORT).show();
} else {
[Link]([Link], "Please enter some text!",
Toast.LENGTH_SHORT).show();
}
}
});
}
}
2 Absolute Layout
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome to AbsoluteLayout!"
android:textSize="20sp"
android:layout_x="50dp"
android:layout_y="100dp" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Me"
android:layout_x="50dp"
android:layout_y="200dp" />
</AbsoluteLayout>
Java file
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity {
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
// Initialize the button
button = findViewById([Link]);
// Set button click listener
[Link](new [Link]() {
@Override
public void onClick(View v) {
// Show a toast message when the button is clicked
[Link]([Link], "Button Clicked", Toast.LENGTH_SHORT).show();
}
});
}
}
3 Table Layout
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">
<!-- Table Row 1 -->
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- TextView in Row 1 -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Username:"
android:textSize="16sp"
android:paddingEnd="8dp" />
<!-- EditText in Row 1 -->
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="Enter your username" />
</TableRow>
<!-- Table Row 2 -->
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- TextView in Row 2 -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Password:"
android:textSize="16sp"
android:paddingEnd="8dp" />
<!-- EditText in Row 2 -->
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="Enter your password"
android:inputType="textPassword" />
</TableRow>
<!-- Table Row 3 -->
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- Button in Row 3 -->
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login"
android:layout_span="2" />
</TableRow>
</TableLayout>
Java File
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity {
private EditText usernameEditText;
private EditText passwordEditText;
private Button loginButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
// Initialize UI elements
usernameEditText = findViewById([Link]);
passwordEditText = findViewById([Link]);
loginButton = findViewById([Link]);
// Set login button click listener
[Link](new [Link]() {
@Override
public void onClick(View v) {
String username = [Link]().toString();
String password = [Link]().toString();
if ([Link]() || [Link]()) {
// Show a toast message if any of the fields are empty
[Link]([Link], "Please enter both username and password",
Toast.LENGTH_SHORT).show();
} else {
// Show a success message
[Link]([Link], "Login successful", Toast.LENGTH_SHORT).show();
}
}
});
}
}
4 Frame Layout
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- First TextView (background) -->
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="This is the first TextView"
android:textSize="18sp"
android:textColor="@android:color/black" />
<!-- Second TextView (overlay) -->
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="This is the second TextView"
android:textSize="18sp"
android:textColor="@android:color/holo_red_light"
android:layout_marginTop="100dp" />
</FrameLayout>
Java File
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity {
private TextView textView1;
private TextView textView2;
private Button toggleButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
// Initialize UI components
textView1 = findViewById([Link].textView1);
textView2 = findViewById([Link].textView2);
toggleButton = findViewById([Link]);
// Set up button to toggle visibility of TextViews
[Link](new [Link]() {
@Override
public void onClick(View v) {
// Toggle visibility of the TextViews
if ([Link]() == [Link]) {
[Link]([Link]);
[Link]([Link]);
} else {
[Link]([Link]);
[Link]([Link]);
}
}
});
}
}
[Link] Layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- TextView centered at the top -->
<TextView
android:id="@+id/helloText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, Welcome!"
android:textSize="20sp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"/>
<!-- EditText below the TextView -->
<EditText
android:id="@+id/inputField"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/helloText"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:hint="Enter something"/>
<!-- Button below the EditText -->
<Button
android:id="@+id/buttonSubmit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
android:layout_below="@id/inputField"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"/>
</RelativeLayout>
Java File
package [Link].relative_layout_example;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity {
private EditText inputField;
private Button buttonSubmit;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
inputField = findViewById([Link]);
buttonSubmit = findViewById([Link]);
[Link](new [Link]() {
@Override
public void onClick(View v) {
// Get text from the EditText
String inputText = [Link]().toString();
// Show a Toast message
if (![Link]()) {
[Link]([Link], "You entered: " + inputText,
Toast.LENGTH_SHORT).show();
} else {
[Link]([Link], "Please enter something",
Toast.LENGTH_SHORT).show();
}
}
});
}
}
Develop an android application by using varios component of android UI
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp"
android:gravity="center">
<!-- TextView displaying a title -->
<TextView
android:id="@+id/titleTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="User Information"
android:textSize="24sp"
android:layout_marginBottom="20dp"
android:gravity="center" />
<!-- EditText for entering the user's name -->
<EditText
android:id="@+id/nameEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter your name"
android:layout_marginBottom="16dp"
android:inputType="textPersonName" />
<!-- RadioGroup for gender selection -->
<RadioGroup
android:id="@+id/genderRadioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginBottom="16dp">
<!-- RadioButton for Male selection -->
<RadioButton
android:id="@+id/maleRadioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Male" />
<!-- RadioButton for Female selection -->
<RadioButton
android:id="@+id/femaleRadioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Female" />
</RadioGroup>
<!-- CheckBox for newsletter subscription -->
<CheckBox
android:id="@+id/newsletterCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Subscribe to newsletter"
android:layout_marginBottom="16dp" />
<!-- Spinner for selecting a country -->
<Spinner
android:id="@+id/countrySpinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp" />
<!-- Button to submit the form -->
<Button
android:id="@+id/submitButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
android:layout_marginBottom="16dp" />
<!-- ImageView to display a picture (e.g., user's profile picture) -->
<ImageView
android:id="@+id/profileImageView"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@android:drawable/ic_menu_camera"
android:layout_marginTop="16dp"
android:layout_centerHorizontal="true" />
</LinearLayout>
Java file
package [Link].ui_components_example;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity {
private EditText nameEditText;
private RadioGroup genderRadioGroup;
private CheckBox newsletterCheckBox;
private Spinner countrySpinner;
private Button submitButton;
private TextView titleTextView;
private ImageView profileImageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
// Initialize the UI components
nameEditText = findViewById([Link]);
genderRadioGroup = findViewById([Link]);
newsletterCheckBox = findViewById([Link]);
countrySpinner = findViewById([Link]);
submitButton = findViewById([Link]);
titleTextView = findViewById([Link]);
profileImageView = findViewById([Link]);
// Set up the submit button click listener
[Link](new [Link]() {
@Override
public void onClick(View v) {
// Get user input from the components
String name = [Link]().toString();
int selectedGenderId = [Link]();
RadioButton selectedGender = findViewById(selectedGenderId);
boolean isSubscribed = [Link]();
String selectedCountry = [Link]().toString();
// Prepare the output message
String gender = selectedGender != null ? [Link]().toString() : "Not
selected";
String subscriptionStatus = isSubscribed ? "Subscribed" : "Not Subscribed";
// Show a toast message with the collected information
String message = "Name: " + name + "\n"
+ "Gender: " + gender + "\n"
+ "Country: " + selectedCountry + "\n"
+ "Newsletter: " + subscriptionStatus;
[Link]([Link], message, Toast.LENGTH_LONG).show();
}
});
}
}