0% found this document useful (0 votes)
9 views17 pages

Programs

The document outlines the implementation of an Android application using various UI components such as LinearLayout, AbsoluteLayout, TableLayout, FrameLayout, and RelativeLayout. Each layout is accompanied by corresponding Java code that handles user interactions, such as displaying Toast messages based on user input. The application demonstrates how to create a user information form with fields for name, gender selection, newsletter subscription, and country selection.
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)
9 views17 pages

Programs

The document outlines the implementation of an Android application using various UI components such as LinearLayout, AbsoluteLayout, TableLayout, FrameLayout, and RelativeLayout. Each layout is accompanied by corresponding Java code that handles user interactions, such as displaying Toast messages based on user input. The application demonstrates how to create a user information form with fields for name, gender selection, newsletter subscription, and country selection.
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/ 17

1.

LinearLayout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
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 com.example.linearlayoutexample;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private TextView textView;
private EditText editText;
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initialize views
textView = findViewById(R.id.textView);
editText = findViewById(R.id.editText);
button = findViewById(R.id.button);
// Set button click listener
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String inputText = editText.getText().toString();
if (!inputText.isEmpty()) {
// Show a toast with the input text
Toast.makeText(MainActivity.this, "You entered: " + inputText,
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "Please enter some text!",
Toast.LENGTH_SHORT).show();
}
}
});
}
}

2 Absolute Layout
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
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 com.example.absolutelayoutexample;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

private Button button;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// Initialize the button


button = findViewById(R.id.button);

// Set button click listener


button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Show a toast message when the button is clicked
Toast.makeText(MainActivity.this, "Button Clicked", Toast.LENGTH_SHORT).show();
}
});
}
}
3 Table Layout
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
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 com.example.tablelayoutexample;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

private EditText usernameEditText;


private EditText passwordEditText;
private Button loginButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// Initialize UI elements
usernameEditText = findViewById(R.id.username);
passwordEditText = findViewById(R.id.password);
loginButton = findViewById(R.id.loginButton);

// Set login button click listener


loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String username = usernameEditText.getText().toString();
String password = passwordEditText.getText().toString();

if (username.isEmpty() || password.isEmpty()) {
// Show a toast message if any of the fields are empty
Toast.makeText(MainActivity.this, "Please enter both username and password",
Toast.LENGTH_SHORT).show();
} else {
// Show a success message
Toast.makeText(MainActivity.this, "Login successful", Toast.LENGTH_SHORT).show();
}
}
});
}
}
4 Frame Layout
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
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 com.example.framelayoutexample;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

private TextView textView1;


private TextView textView2;
private Button toggleButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// Initialize UI components
textView1 = findViewById(R.id.textView1);
textView2 = findViewById(R.id.textView2);
toggleButton = findViewById(R.id.toggleButton);

// Set up button to toggle visibility of TextViews


toggleButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Toggle visibility of the TextViews
if (textView1.getVisibility() == View.VISIBLE) {
textView1.setVisibility(View.GONE);
textView2.setVisibility(View.VISIBLE);
} else {
textView1.setVisibility(View.VISIBLE);
textView2.setVisibility(View.GONE);
}
}
});
}
}
5.Relative Layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
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 com.example.relative_layout_example;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

private EditText inputField;


private Button buttonSubmit;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

inputField = findViewById(R.id.inputField);
buttonSubmit = findViewById(R.id.buttonSubmit);

buttonSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Get text from the EditText
String inputText = inputField.getText().toString();

// Show a Toast message


if (!inputText.isEmpty()) {
Toast.makeText(MainActivity.this, "You entered: " + inputText,
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "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="http://schemas.android.com/apk/res/android"
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 com.example.ui_components_example;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;

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) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// Initialize the UI components


nameEditText = findViewById(R.id.nameEditText);
genderRadioGroup = findViewById(R.id.genderRadioGroup);
newsletterCheckBox = findViewById(R.id.newsletterCheckBox);
countrySpinner = findViewById(R.id.countrySpinner);
submitButton = findViewById(R.id.submitButton);
titleTextView = findViewById(R.id.titleTextView);
profileImageView = findViewById(R.id.profileImageView);

// Set up the submit button click listener


submitButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Get user input from the components
String name = nameEditText.getText().toString();
int selectedGenderId = genderRadioGroup.getCheckedRadioButtonId();
RadioButton selectedGender = findViewById(selectedGenderId);
boolean isSubscribed = newsletterCheckBox.isChecked();
String selectedCountry = countrySpinner.getSelectedItem().toString();

// Prepare the output message


String gender = selectedGender != null ? selectedGender.getText().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;
Toast.makeText(MainActivity.this, message, Toast.LENGTH_LONG).show();
}
});
}
}

You might also like